We will now attempt to go through these steps. Assuming you already have environment ready to start Android and NDK development.
Create Android project
Create an Android project called “MyFirstJNI“, with package “com.example.myfirstjni“.
|To create new project, right click on File ->New -> Android Application Project. Give project name and select an API. Here in this case I have, Application Name as “My First JNI”, Project name as “MYFirstJNI” and package name “com.example.myfirstjni”.|
Create java source
For this Paper, we will use simple c program – Palindrome Checker – that accepts string from user and returns boolean, True if string is Palindrome and False if string is not Palindrome.
We will start by defining C function prototype as Java methods(can be called as native methods wrapper). This class will be used to load library and expose native methods.
In this example we will create Java class JNIWrapper. This class will expose isPalindrome native method.
Make JNI folder
Create a folder named “jni” in the Eclipse project’s root directory. (Right-click on the project -> New -> Folder). Create a subfolder “include” under “jni” for storing the header files.
Create C/C++ Header file using “javah”
Now, we will create header file using “javah” utility.
Open terminal(cmd in windows). Navigate to include folder that we created in last step.
- -classpath: In our case, we need JNIWrapper.class which is under “../../bin/classes/”.
- -o: to set file output name, in our case it’s Palindrome.h
|You need to use the fully-qualified class name (including package) and not the (.class) extension.|
This will create header called “Palindrome.h” under include folder. Refresh your eclipse project.
Header file contains prototype function,
The native method “isPalindrome(String inString)” mapped in the above header in the native code.
Write C code
Now, this is the time to implement functionality in C source.
Right-click on jni folder -> New -> File. Give name “Palindrome.c” to the file. This will create C source file called “Palindrome.c”.
This native code gets string and returns true if string is palindrome and false if string is not palindrome.
Create an Android makefile called “Android.mk” under “jni” directory. Right-click on “jni” folder -> New -> File and give name “Android.mk”. This file is used for Android build tools.
For our Paper, we will have following in our makefile.
|There are number of sample Android.mk files in the samples/ directory of the NDK. It’s easiest to copy the “Android.mk” file from another (sample) project.|
Start terminal(CMD in windows), change directory to project root directory. Run “ndk-build” command.
|The command “ndk-build” comes from the NDK’s installation directory. So easiest way is to add this directory in PATH.|
Note: To remove generated libraries, run
Run the android app
Refresh eclipse project and you will see library “libPalindromeChecker.so” is generated under libs->armeabi.
We are almost done with the native side of the code. Now, call “checkPalindrome(String inString)” method anywhere from your Android project. And it will call native method to return whether string is palindrome or not.
|Check logcat to confirm that the shared library “libpalindromechecker.so” is loaded.|
Complexities integrating NDK
- Environment set up is not trivial. You will need C/C++ setup as well. Also, integration with eclipse is confusing too. ie. Once you make changes in the C files and generate new libraries, the eclipse project will have to be manually refreshed.
- JNI is not for the faint of heart. So if you’re not familiar with JNI, it can take some time to get things right.
- Adding JNI to project increases the complexity in an application.
- Memory-management in NDK can be painful. Is is important to incorporate elegant memory management techniques.
- Debugging native code is complex and could be time consuming.
NDK integration can be painless if you follow all the steps carefully. However, using NDK as a solution is not always ideal. Before going down the path of using NDK, try to optimize the Java code to achieve the desired results. While using NDK, you need to weigh the benefits achieved against the complexities. By performing tests on your application, you can ascertain the gain that will be achieved by using NDK.
Some rules of thumb:
NDK should be used for:
- Developing components with complicated calculations (game physics, AI, pattern recognitions, data encryption and compression, images/video/audio processing);
- Making “a clone” of a C++ desktop application for Android.
NDK should not be used for:
- Creating user interfaces;
- Working with static data in bulk.