Home > Android Dev > How to build shared/static library (.so file) on android.

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

Advertisements
Categories: Android Dev
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: