這裡以hello-Jni為範例,紀錄如何呼叫原生方法,宣告原生方法,載入原生模組,在c/c++實作原生方法。
以下為 HelloJni.java

package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.hellojni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("hello-jni");
    }
    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();
    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String  unimplementedStringFromJNI();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }
}

第16~18行為載入原生模組的動作,注意使用 System.loadLibrary(“hello-jni”)指的是載入 java.library.path變數所包含路徑的 hello-jni,不必包含擴展名稱(lib)(.so),
而”hello-jni”必須與 Android.mk 裡的 LOCAL_MODULE 變數值(hello-jni)相同,另外要注意是原生模組的名稱要避免重複。
第24行宣告原生方法 stringFromJni()
第36行宣告原生方法unimplementedStringFromJni()
 
接著來看看 hello-jni.c 的內容。

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <string.h>
#include <jni.h>
/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
 */
jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}

第26~30行即為原生方法stringFromJNI()的實作,注意

Java_com_example_hellojni_HelloJni_stringFromJNI

除了開頭的 Java_ 以外,原生方法的名稱必須完全對應於java層的位置以及名稱,e.g.
com_example_hellojni :路徑
HelloJni:類別名稱
stringFromJNI:方法名稱


 
而回傳值 jstring 對應於 java層的 String
參數部份java層不帶參數,而對應的原生方法必須帶入 JNIEnv* env , jobject thiz 2個參數。
JNIEnv* env 必須為原生方法的第一個參數,透過 env 可以使用虛擬機的許多方法(ref:http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html)。
注意幾點:
1.在 C 中,使用env的方式必須為 (*env)->method,
而 C++ 則是 env->method。
 
2.靜態方法和實體方法,在java層中如果是實體方法,JNI層第2個參數為 jobject thiz,
在java層中如果是靜態方法,JNI層第2個參數則為 jclass clazz。