Android NDK is a toolset that allows developers to implement parts of the application using native-code languages such as C and C++ in order to reuse existing code libraries written in these languages.
The purpose of this tutorial is to show you how to use this toolset to create an Android Project that makes a simple call to native C/C++ code.
Until you proceed the steps for installing the Android NDK on your machine please check if you have installed the following features:
– Java (JDK) (otherwise visit http://www.oracle.com/technetwork/java/index.html and get the latest JDK and install it to your machine)
– IDE (for this tutorial we are using Eclipse – if you do not have an IDE installed on you machine please go to http://www.eclipse.org/downloads/ and get the latest version on Eclipse )
– Android SDK (if you do not have it installed on your machine you can get it from the official Android site http://android.com)
– ADT (Android Plugin for Eclipse – read this article for configuration)
Step 1 : Installing C/C++ support for Eclipse
Open Eclipse and choose “Help” item from the menu. Look for “Install New Software” option and click it to open the Install Window. In the “Work with” section choose “Kepler” as update site (I use Eclipse Kepler , in case you have another version please look for the right site’s name that match your IDE version’s name) and wait until the item tree loads. You should be able to see “Programming Language” branch. Check “Eclipse C/C++ Development Tools” and press “Next “ for the installation part:
Cygwin is a set of software that emulates Unix environment on Windows. Go to cygwin.com and download the installer.
After you download the Cygwin’s setup.exe you need to follow the next steps:
run the installer and choose Install from Internet option and click Next
– choose the installation directory(be sure to choose a directory path that contains no spaces in it) and press Next
– at this moment Cygwin will connect to its central site and a list of mirrors sites will be shown . Choose one that is geographically close to you and press Next
– after you choose the mirror site Cygwin will download the available packages . Some packages will be downloaded and installed by default but there are others that you would to install for different purposes. For this tutorial I suggest you to install the entire Devel package. Click once the Devel package and wait until “Default” changes to “Install” then press next. This install part will take some time so put it in the background and let the installation to proceed.
After the installation succeeds choose the option to create an icon on desktop and click Finish. You will be able to see Cygwin icon on your desktop.
Open Cygwin and run the following command : make -v .If Cygwin was installed correctly you should be able to see some information about the installed version listed in the console :
Step3:Installing the Android NDK
Go to the official Android site and download the NDK zip for windows. Extract the file into a directory that contains no spaces in the path name.For example I use D:\ so the path is D:\android-ndk-r10.
Now we have the proper environment for building our first NDK application.
2.Create a Hello-World application with Android NDK
Open your IDE and create a simple Android Project.
After your project is created add a Class under project src folder.
Under this class we load the library using JNI (Java Native Interface) and expose any native method that we want to use directly. JNI defines conventions and mechanisms used for the interaction of Java code with C/C++ code. So put the following code under this class.
The“getString()” is the name of the native method that we will use later in the application and “androidndk” is the name of the binary library (it will be specified later in the Android.mk file).
Generate C Header File using “javah” command
Open the Windows console and execute “javac” command to compile the class inside which we have defined the native C method and specified that we want to load a binary library:
-move to your project root folder
In the project created by me I’ve used:
After that we will execute “javah” command with the parameter “-jni” to create the header file:
go inside the “bin/classes” (there is where you will find the compiled class) and execute
javah -jni <class-package>.<class-name>
In the project created by me I’ve used:
javah -jni com.android.ndk.example.NativeCode
-after this step you will be able to see the header file placed under the “bin/classes” folder.
Now create a new folder , call it “jni” and put it under the project root folder. Take the header file and place it under the “jni” folder.
The header file contains the native method that we will use in our android project.
Now we have to create the C class and implement the method generated in the header file. Under the “jni” folder create a .c file and put the following content:
I hope that you have noticed that the name of the C method is not random it depends on the name of the class you have created in the previous steps. It is also somehow specified name of the package under that contain the class. Just look inside the header file and you will find the right name of the method that must be implemented.
Next we have to build Android.mk file. This file is important for the NDK building process because it contains you NDK modules names. In my example I’ve named the module “androidndk” and told the build tool that it consists of one source file named “androidndk.c”. So you have to create a new file named it Android.mk , put it under “jni” folder and add the following lines :
After that we are now able to create the binary library from the C source that we have created in the previous steps. For this purpose we will use a combination of Cygwin and Android NDK tools. First open Cygwin console and point to the project root folder using the following command:
Now that we’ve changed workspace path we have to run ndk-build command to be able to get the binary library.Execute the following command:
(<android-ndk-path> is the installation directory path of the Android NDK)
When the ndk-build command finishes you will be able to see under libs/armeabi the binary library (.so file).
Run the application
Create the Activity class , put the following code and run the application:
If everything was done correctly you will be able to see and alert dialog containing a hello message in the simulator: