1.MainActivity.java
package com.yutao.jni; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println(stringFromJNI()); } public native String stringFromJNI(); static { System.loadLibrary("TestJNI01"); } }
进入到目录/workspace/TestJNI01/bin/classes
在终端中输入:javah -jni com.yutao.jni.MainActivity,就会生成com_yutao_jni_MainActivity.h文件
3.TestJNI01.c
#include <jni.h> jstring Java_com_yutao_jni_MainActivity_stringFromJNI(JNIEnv *env, jobject object) { return (*env)->NewStringUTF(env, "Hello from JNI !"); }
4.Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := TestJNI01 LOCAL_SRC_FILES := TestJNI01.c include $(BUILD_SHARED_LIBRARY)
新建jni文件夹,把TestJNI01.c和Android.mk放到这个目录下,执行ndk-build命令,就会在libs/armeabi/libTestJNI01.so
现在可以运行app程序,并能够看到log的输出。结束