The Android NDK complements the Android SDK and allows us to embed native machine code compiled from C\C++ into our application APK file. The Dalvik VM allows our Java code to use JNI in order to call native methods that were implemented in C\C++ as part of the native machine code the APK file includes.

The Android NDK includes a simple demo (hello-jni) for embedding native code as part of our APK file. You can find it within the ‘apps folder. I believe it is the simplest code sample to start with. It includes the definition of a native method (using the ‘native’ keyword) in order to indicate that it was implemented in C\C++. The native method implementation should be saved within a native shared separated library named in accordance with the standard Unix conventions (lib<…>.so). The native shared separated library should be part of the APK file. The Java source code of this simple demo includes the code that loads the library by calling the System.loadLibrary(“libname.so”) method. The library itself isn’t included and we will need to create it on our own.

I chose to write down this post in order to assist all windows based developers with running that code sample. Assuming that you are using a Windows based operating system, the following are the steps you should follow in order to install the NDK on your desktop and run the ‘hello-jni’ sample.

Step 1 – Install CygWin
Work on a windows based platform you should start with installing the CygWin. It is a Linux like environment that should allow you running C\C++ code that was developed for Linux. You can download CygWin at www.cygwin.com. You can find a good video clip that explains how to install it at http://www.youtube.com/watch?v=2-YUD5r4gUw. Make sure you install the GNU Make as well.

Please note that you can execute the setup executable file at any point after the installation was completed in order to add more comonents to your CygWin environment.

Step 2 – Execute The host-setup.sh File
Double click the Cygwin batch file that was installed on your desktop. You should get a shell command line where you can verify that GNU Make was installed propertly by typing ‘make –version’.

Once you verify that GNU Make was installed properly you can move forward and execute the host-setup.sh file located within the <ndk-folder>/build. Make sure to execute that file while been outside of the build directory, as shown in the following screen shot.

Step 3 – Open The hello-jni Project
When starting a new android project we can select the ‘Create project from existing source’ option and select the hello-jni project located within <ndk-folder>/apps directory.

Once the hello-jni project directory was selected we can easily load it into our Eclipse Pulsar IDE.

Step 4 – Build the Native Code Library
Move into the <ndk-folder> and type-in the ‘make APP=hello-jni V=1’ command. The result would be the creation of the ‘libhello-jni.so’ file within the ‘jni’ folder in your project.

If you refresh the Eclipse package browser you will get to see the new ‘libs’ directory.

Step 5 – Running The ‘hello-jni’ Code Sample
The next step would be building and running the project. Running an android application that includes native code is as running any other android application.

Leave a Reply

Your email address will not be published. Required fields are marked *