NDK : loading resources and assets from native code
27 Aug 2010 | Programming
One of the first problem I ran into when I started developing Zoob with the Android NDK is to load resources (mainly textures to be used by OpenGL) from the APK. And here is a solution to this problem.
This sample code consists of a very simple Android Java App that basically delegate all the rendering to native code (using NDK). Additionally (and I think that’s the interesting part), I’ve added libzip and libpng to the NDK code so you can load you application APK from the C/C++ code and load resources from there (example include loading a PNG file using libpng and libzip).
The code is probably not the most efficient nor bugfree, but it works.
The archive file contains a whole ndk “project” (to put under NDK_HOME/apps).
In the project/jni directory, there are 3 modules. libpng and libzip are stripped-down version of the original libs (retaining only c sources basically). The “moob” project contain the main C++ code used for rendering and loading texture. Just dive in it, it’s pretty small and the function names should be understandable.
To load the APK and read resources from it, the APK path is sent to the nativeInit method, which then load the APK using libzip. libzip provides fread like functionnality for reading compressed files so that’s pretty easy then.
There is an eclipse Java/C++ project included in the code. You might have to tweak the configuration to work with your NDK_ROOT configuration. This code was tested on the emulator and on a HTC Magic.