Pages

Get it free, Try now

Free Cell Phones

Tuesday, August 17, 2010

How to build shared/static library (.so file) on android.

Build Environment
1. Install Cygwin
2. Have javac which is already available in JDK package.
3. Install Android NDK(Native development Kit)
4. Make sure both cygwin and NDK are in same location such as
C:\android-ndk-r4\ and C:\cygwin

Now create a android project using eclipse or use samples folder instead C:\android-ndk-r4\samples

1.now, run through cmd, javac on .java file which has definition of native method and a static system method which loads library
//speaktoc.java
public class SpeaktoC {
public  native String showstring(String astr);
static{
System.loadLibrary("CallCodeasLIB");
}
}
i;e in my case
C:\android-ndk-r4\samples\JnihelloJni\src\ex\jnihellojni>javac SpeaktoC.java

results in SpeaktoC.class.

2. move to bin folder and execute javah command i;e
C:\android-ndk-r4\samples\JnihelloJni\bin>javah -jni ex.jnihellojni.SpeaktoC

resutls in  ex_jnihellojni_SpeaktoC.h

3. Create jni folder in project and copy the above generated header file.

4. Create a .c file and implement the methods generated in the .h file.
5. Dont forget to write android.mk file(check out this for Android.mk structure)
6. Now launch Cywgin and change the current directory to project folder i,e
type the following commands
$ cd /cygwin/c/android-ndk-r4/samples/JnihelloJni
and issue Build command
$ ../../ndk-build.
resuls
SharedLibrary : libcallcodeasLib.so in JnihelloJni/libs/armeabi

7. now call the exported method in the application as shown  below
SpeaktoC c = new SpeaktoC();
String str = c.showstring(); // methos is in Library

No comments:

Post a Comment