Creating a simple Hello World application Using Android NDK with C++
Prerequisites
- I am using
GNU/Linux - My IDE is
IntelliJ IDEA - The project is
Antbased. - My working directory is
$JNI - My C++ IDE is
QtCreator(I'll mention the build steps inline). - NDK should be installed and
ndk-buildshould be available in$PATH javahshould be available in$PATH
Step 1 - Create The Android Project
- Create and Empty Project Named
JNI(at location $JNI) - Create a new Android application module named
JNIHelloWorld. - Package Name -
com.jnisample.JNIHelloWorld - Create one activity
JNIActivityand define one native functiongetNativeMessage() - Build the project
Step 2 - Create the JNI Header file
- Create a folder
jniinside$JNI/JNIHelloWorld - In Terminal(command line) move to
$JNI/out/production/JNIHelloWorlddirectory (This is where the java files get compiled to class files by IDEA; Verify the directory structure is same as the package name com/jnisample/JNIHelloWorld/JNIActivity.class) - Create the header for the native function using
javah
$ javah $JNI/JNIHelloWorld/jni com.jnisample.JNIHelloWorld.JNIActivity
The Above command will generate a header file with name
com_jnisample_JNIHelloWorld_JNIActivity.h inside the jnidirectory.Step 3 - Add a C++ Class
- Create a C++ class with name
HelloWorld - Create define a member function
getNativeString()which returns a string
Step 4 - Implement JNI Entry (or Entries)
Implement the entry method
com_jnisample_JNIHelloWorld_JNIActivity_getNativeMessage defined in header com_jnisample_JNIHelloWorld_JNIActivity.hStep 5 - Configure make
Define
Android.mk and Application.mkStep 6 - Build Shared Object
- In Terminal, move to directory
$JNI/JNIHelloWorld - Build the Shared Object using the command
ndk-build
$ ndk-build
On successful build, the Shared Object file
libmynative.so will be copied to $JNI/JNIHelloWorld/libs/armeabi
Comments
Post a Comment