Thursday, 12 December 2013

Build sample Android NDK on Linux

1. Download Android-NDK (i'm using android-ndk-r8-crystax-1)
2. Create a simple Java wrapper file named

 * @author khanh
public class NativeLib {
    static {

     * Adds two integers, returning their sum
    public native int add(int v1, int v2);

     * Returns Hello World string
    public native String hello();
3. Create C header file by running javah -jni
javah -jni
mv mv com_pt_NativeLib.h ../jni/
4. Write implement C file for com_pt_NativeLib.h
 #include "com_pt_NativeLib.h"

JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
  (JNIEnv * env, jobject obj) {
        return (*env)->NewStringUTF(env, "Hello World!");

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
  (JNIEnv * env, jobject obj, jint value1, jint value2) {
        return (value1 + value2);
5. To build library, create file to lead compilers how to build
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := ndk_sample
LOCAL_SRC_FILES := ndk_sample.c

6. Create folder in /apps/ndk_sample/ and create
APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES      := ndk_sample
7. Create symbol link to the project by typing
ln -s ~/home/Android/NDKSampleProj /apps/ndk_demo/project
8. Finally, run make APP=ndk_sample that will generate libs/armeabi/
9. Enjoy the coding now with native code.

No comments:

Post a Comment