Pages

2012/07/27

How to create Android JNI

This article teaches how to create Android JNI applicatoin.

Step1:Open Android workspace in Eclipse.The workspace we create for the JNI development.Refer to this article How to install Android NDK on windows.

Step2:Create a new Android Project.Eclipse toolbar->New->Android Project.Create a new project directory in the Cygwin directory.





















Clicking finish button. A new project in the workspace.





















Step3:Manually create 2 directories in the project directory, JNI and libs.
Step4:Creating Andoird.mk file in the JNI directory.Android.mk is just a text file.
Android.mk file contents:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := first-jni
LOCAL_SRC_FILES := first-jni.cpp
include $(BUILD_SHARED_LIBRARY)
Step5:Creating  your jni C/C++ file in the JNI directory.Aftercompile the jni code successfully . There is a dynamatic link library(libfirst-jni.so) will be created in the libs directory we create manually before.
The jni code has two different type, C and C++.The difference is C++ file must export it's JNI function to C.
The code explains it self.

/*
*  first-jni.c
*/
#include <string.h> 
#include <jni.h> 
 
jstring
Java_my_project_MyFirstJNI_MyFirstJNI_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "My first JNI!");
}
<

/*
*  first-jni.cpp
*/
#include <string.h> 
#include <jni.h> 
 
extern "C"
jstring
Java_my_project_MyFirstJNI_MyFirstJNI_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return env->NewStringUTF("My first JNI!");
}
Step6:The JNI function name has rules.Refer to this link Naming_Functions.

Java_{Package name Replace dot with underline}_{Class name}_{function name}() ,
In my example will be the "Java_my_project_MyFirstJNI_MyFirstJNI_stringFromJNI",
"my_project_MyFirstJNI" is the package name, "package  my.project.MyFirstJNI;"
"MyFirstJNI" is my jni file class name.
"stringFromJNI" is the jni function name for JAVA to call.

Step7:Now we had create our own jni function.Press F5 to refresh the Eclipse.





















Step8:Setup the Auto-build configuration, Please flow the steps in this article How to install Android NDK on windows. Starting from Step11.

Step9:Modify the JAVA code to call our JNI function.
package my.project.MyFirstJNI;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class MyFirstJNI extends Activity 
{
    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(R.layout.main);
    }
    // A native method that is implemented by the
    // 'first-jni' native library, which is packaged
    // with this application.
    //
 
    public native String  stringFromJNI();
     
    // this is used to load the 'first-jni' library on application
    // startup. The library has already been unpacked into
    // /data/data/my.project.MyFirstJNI/libs/libfirst-jni.so at
    // installation time by the package manager.
    //
    static
    {
        System.loadLibrary("first-jni");
    }
}
Final Step:Run the project in emulator, the screen will show the string "My first JNI!".

2 comments:

  1. Simple and nice... step by step... and up to date!
    Wish I had the time to compose such posts... :)

    ReplyDelete
  2. This is great help, thanks a ton.

    ReplyDelete