PDA

View Full Version : How to load native library in Android


SynPad
11th December 2008, 01:05 AM
Can someone give advice in how to load native library and make native function calls in android application? I tried with android 1.0 and my result is below:

Result:
1. I added the following code to android application Lunarlander.java
//native method declaration
native void hello(String name);

static {
System.out.println("libhello.so loaded");
System.load("/data/app/libhello.so");
}

I added my libhellp.so to lunalander.apk and then uploaded the apk to /data/app. The application launches ok.

2. I added the following code to android application Lunarlander.java
//native method declaration
native void hello(String name);
static {
System.out.println("libhello.so loaded");
System.load("/data/app/libhello.so");
}


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//test loading hello lib and call hello function
LunarLander myLunarLander = new LunarLander();
myLunarLander.hello("Hi there!");

}

I added my libhellp.so to lunalander.apk and then uploaded the apk to /data/app. The application launches was forced to close and the error message is “The application Lunar lander has stopped unexpectedly. Please try again”.

Not sure why it failed to invoke the native function.

Thanks,

SynPad

joel2009
11th December 2008, 01:57 AM
Please post Questions in the Q&A section. Dev and Hacking Section is for contributers (new programs themes ect)

I'll go ahead and have the thread moved to Q&A for you this time ;)


Thanks for your cooperation,

Joel
Q&A Section Team


EDIT: you may also want to try the g1 section, pm me if you would like it moved there instead.

www.rzr.online.fr
27th July 2009, 11:38 AM
Check the samples w/ NDK

BTW, do you know how to ship the lib along the apk :
there is a "native library location:" entry on project settings in eclipse plugin

Later