Sunday, September 8, 2013

Working with Android NDK applications

I wanted to try some native applications on Android recently and it was a mystery to me for several days. Something was missing. Finally, I clearly understood the way to compile and run JNI based Android applications. So, here's the procedure I followed to try a sample native application on Android. I assume that we already have Android SDK, and also installed the Android ADT plagin for Eclipse.

First of all we need C/C++ support on Eclipse IDE. To install it on Eclipse, we goto the menu bar.
Help -> Install New Software...
In the pop up window, enter the name as CDT. For the USR, add the following URL.
Now Eclipse can compile C/C++ applications.

Second step is we need Android NDK. So, download it from the following place.
Extract the downloaded compressed directory and save the folder somewhere most probably in the same directory where your Android SDK folder is. Now we are ready to try some example native application. There are some good native application samples in the downloaded NDK directory. We are going to use the native-activity example in the "android-ndk-r9/samples/native-activity" directory.

Move into that directory from the terminal. We need to run a tool called ndk-build within this directory. This tool is in the "android-ndk-r9/ndk-build" directory. So while in the native app directory, we run that tool by providing the absolute or relative path.


When this tool is running, it will print some output on the screen and then it will exit.
This tool is compile and build the C based codes of the native app. Now we can use this app on the Eclipse IDE. So, go back to Eclipse IDE.

File -> Import... -> Existing Android code Into Workspace -> Next

Now browse and select the "android-ndk-r9/samples/native-activity" directory. Put the tick for "Copy projects into the workspace". Then  click "Finish". 
Now right-click on the project folder in navigator of eclipse and "Run As" android application.