NDK, OpenGL - loading resources and assets from native code

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

NDK, OpenGL - loading resources and assets from native code

Postby julienr » Fri Mar 12, 2010 4:23 pm

Hello,
I wanted to develop a game using android NDK and I was looking for some way to be able to load the resources from the APK using only my C/C++ code (didn't want to call java from native and so on). So I came up with the following code which I think might be useful for some others.

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[1] and libzip[2] 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.

[1]http://www.libpng.org/pub/png/libpng.html
[2]http://nih.at/libzip/
Attachments
android-ndk-assets.zip
Source
(837.53 KiB) Downloaded 8649 times
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Top

Re: NDK, OpenGL - loading resources and assets from native c

Postby m4rvin » Sat Jul 24, 2010 5:00 am

hi,

this is a very useful post. i'm curious why nobody replied.
but i just want to thank you.. my app doesn't have any need for permissions like reading sdcard because of this code.

Munchy Solitaire ;) --- message me so i can send you a full version.
coded with opengl es, ndk

marvin
m4rvin
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 14, 2010 11:04 am

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Sat Jul 24, 2010 4:17 pm

Hey thanks :)
Great that it has been useful.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby aanrudolph2 » Wed Aug 11, 2010 4:09 pm

Thank you SOOOOO much! This has been beyond helpful! I very much appreciate it!
aanrudolph2
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Aug 11, 2010 4:08 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby Jug6ernaut » Tue Oct 12, 2010 2:47 pm

Awesome work, using libzip how hard would to be to extract a file and save it?
Jug6ernaut
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Jun 17, 2010 7:00 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Tue Oct 12, 2010 8:46 pm

Well if you want to write to the sdcard, you would need the permission for that. I guess that if you specify the permissions for writing on SDcard in your AndroidManifest, then your NDK code would also "inherit" this permission. The NDK code is running in the same process as the java code after all.
But that's really only a guess, so you would have to test.

But apart from that potential permission problem, it's really easy from a programming point of view. Just open your zip and read the files you want to extract and then use the standard C library to write them to the SDcard.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Top

Re: NDK, OpenGL - loading resources and assets from native c

Postby Jug6ernaut » Thu Oct 14, 2010 2:32 am

Yep i got it working :), its writting to normal memory no permissions needed. toke me a few hours to get it(sad i know xD) but i got it!

Huge thanks for your code here, HUGE help ;).
Jug6ernaut
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Jun 17, 2010 7:00 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Thu Oct 14, 2010 10:29 am

You're welcome :) Nice to know it's working.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby legendlau » Fri Oct 15, 2010 1:53 pm

hi julienr,
i'm a newer to android ndk, when i build c code as follows:
make APP=android-ndk-assets
however, i got error "cc1plus: error: unrecognized command line option "-Wno-psabi" ",
plz help me how to build this project...thanks very much!
legendlau
Freshman
Freshman
 
Posts: 2
Joined: Fri Oct 15, 2010 1:45 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Fri Oct 15, 2010 5:22 pm

In android-ndk-assets/project/jni/moob/Android.mk, remove "-Wno-psabi" form the line.
Code: Select all
LOCAL_CFLAGS := -DANDROID_NDK -Wno-psabi

Just out of curiosity, which NDK version are you using ? I guess the included GCC doesn't recognize -Wno-psabi anymore.
By the way, the "-Wno-psabi" is just to get rid of some warnings message about va_list and such. So maybe you'll get a more verbose output, but this will still compile fine.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby legendlau » Mon Oct 18, 2010 3:46 am

thanks for your reply, now i can build successful,and generate libmoob.so
my ndk version is android-ndk-1.6_r1.

however, another problem is coming, when i build my project with eclipse,i got warning "Error launching external scanner info generator (gcc -E -P -v -dD D:/android/workspace/.metadata/.plugins/org.eclipse.cdt.make.core/specs.cpp)"," Errors running builder 'Integrated External Tool Builder' on project 'Moob-NDK'.
", i don't know how to use .externalToolBuilders, plz help me..

BR
Devin Lau
legendlau
Freshman
Freshman
 
Posts: 2
Joined: Fri Oct 15, 2010 1:45 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Mon Oct 18, 2010 10:11 am

In eclipse, go to project Properties->Builders and then you should be able to edit the external builder. You can specify whatever you want to run in the builder, most likely set the working directory to your project dir and then "make APP=android-ndk-assets" as the command.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Sun Dec 05, 2010 6:34 pm

For those interested in NDK development, I've published my ports of libpng, libzip and libenet (a c networking library) on github.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby andreasv » Wed Feb 02, 2011 11:17 am

Hi,

Thank you very much for sharing this!!

I am currently new to android and android ndk but I want to do something similar to your project. I made a lot of search and your approach is the only one that I think it can be helpful to me!!!

I want to read an xml file from native code and package it in the apk as your texture image in the assets folder. My worries is that as far as I understand, if I place my xml file in assets folder it will be compressed except if I change it's extension and put something like .mp3. How does it affect me if it is compressed? will I need extra libraries like the libzip?

How am I supposed to read this xml file? Is it the same way as you read the png file? will the libzip be necessary?

I am sorry if all these sound stupid, but I am a little confused...

Thanks in advance for your time
andreasv
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 02, 2011 11:03 am

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Thu Feb 03, 2011 4:34 pm

Hi,
With version 5 of the NDK, Google has finally included support to access APK from native code without using my "hack" anymore.
I haven't experimented with it yet, but I would recommend looking into it :
http://developer.android.com/sdk/ndk/index.html
http://developer.android.com/reference/ ... ivity.html

(Also see the docs/CHANGES.HTML included in the ndk).

Now, to answer your question, the point of using libzip is not to worry about compression. So you could read your XML file using the same code that I use to read my textures (libzip is used to decompress on-the-fly).

But really, you'd better do it using the official NDK way.

Hope this helps.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Top
Next

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests