JNI ndk android example

1. create Android Project in Eclipse

2. create jni folder inside the project name
workspace/
…|_hello-jni/
……|_bin/
……|_src/
……|_res/
……|_AndroidManifest.xml
……|_jni/
………|_Android.mk
………|_Application.mk
………|_hello-jni.c

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni # module name; In this case, after running ndk-build the name will be built to hello-jni/libs/armeabi-v7a/libhello-jni.so
LOCAL_SRC_FILES := hello-jni.c # source file

include $(BUILD_SHARED_LIBRARY)

NOTE that we can add CFLAGS
i.e. LOCAL_CFLAGS := -Werror

or LDLIBS
i.e. LOCAL_LDLIBS := -llog
or
LOCAL_LDLIBS += -llog -ldl

See more option of Android.mk in ndk docs : android-ndk-r8/docs/ANDROID-MK.html

3. Build

dsin@dsin-laptop: hello-jni$ ndk-build

NOTE : ndk-build is in ndk package, hence the path must be added beforehand.

export PATH=$PATH:/path/to/android-ndk-r8

TIPS to run ndk-build in eclipse :

NOTE : to select ndk-build PATH in eclipse, goto Preference → Android → NDK and choose your ndk location

Eclipse threats the project as a pure Java project. We have to convert the project into mixed Java & C/C++ project.

by

New > Convert to a C/C++ Project

NOTE :

– jni.h also can be found in /usr/lib/jvm/java-6-openjdk-i386/include/

or we can use locate jni.h to find it

– If you got `/android-ndk-r8/prebuilt/linux-x86/bin/make: not found `, extract ndk again using `tar jxf filename.tar.bz2`

4. Refresh the Project Explorer View in Eclipse

5. Rebuild Project

HelloJni.java

package com.example.hellojni;

public class HelloJni extends Activity{
    public native ReturnTypeIEvoidString FunctionName(){
    }
    static {
        System.loadLibrary("hello-jni");
    }
}

hello-jni.c
In hello-jni.c, we have to

#include <jni.h>

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3View_FindFeatures(JNIEnv* env, jobject obj){

}

NOTE : this function,
– use macro JNIEXPORT
– return void, if this function have return it will not be void. i.e. return 4; should be int
– use macro JNICALL

Name :
Java_[org_opencv_samples : package name ]_[ name of class that function will be part of ]_[ name of function ]

passing parameter

jintArray

JNIEXPORT void JNICALL Java_org_xxx_samples_tutorial3_Sample3View_findFeatures(JNIEnv* env, jobject obj, jint width, jint height, jbyteArray yuv, jintArray bgra){
//jsize a_len = (*env)->GetArrayLength(env, bgra);
jint *a = (*env)->GetIntArrayElements(env, bgra, 0);
findFeatures(width, height, yuv, bgra);
}

The method declaration in Java Activity will be

public native void findFeatures(int [] bgra)

return array

JNIEXPORT void JNICALL Java_org_xxx_samples_tutorial3_Sample3View_findFeatures(JNIEnv* env, jobject obj, jint width, jint height, jbyteArray yuv, jintArray bgra){
    //jsize a_len = (*env)->GetArrayLength(env, bgra);
    jint *a = (*env)->GetIntArrayElements(env, bgra, 0);

    findFeatures(width, height, yuv, bgra);

    // new jintArray
    jint size=20;
    jintArray graphic;
    graphic = (*env)->NewIntArray(env, size);
    LOGE("test");
}

build problem
Installation failed due to invalid APK file! uid disk settings

solution : choose the android version that is compatable with the one that is tested

 

NOTE: we can choose architecture of the phone ( arm, x86, or mip ) by setting in

jni/Application.mk

APP_ABI := x86
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s