1. java native
package com.demo; public class DemoNative { public static native void getData(String key); static{ System.loadLibrary("demojni"); } }
2. Android.mk
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := com_demo.DemoNamtive.cpp LOCAL_C_INCLUDES += \ $(JNI_H_INCLUDE) LOCAL_SHARED_LIBRARIES := \ libnativehelper \ libcutils \ libutils LOCAL_MODULE:= libdemojni LOCAL_MODULE_TAGS := optional LOCAL_PRELINK_MODULE := false include $(BUILD_SHARED_LIBRARY)
3. com_demo.DemoNamtive.cpp
namespace android { static void com_demo_DemoNative_getData(JNIEnv* env, jobject obj, jstring data) { LOGD("get data: %s", data); } static JNINativeMethod gDemoNativeMethods[] = { {"getData", "(Ljava/lang/String;)V", (void*)com_demo_DemoNative_getData} }; int register_Native(JNIEnv* env) { int res = jniRegisterNativeMethods(env, "com/demo/DemoNative", gDemoNativeMethods, NELEM(gDemoNativeMethods)); LOG_FATAL_IF(res < 0, "Unable to register native methods."); return 0; } extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void **)&env, JNI_VERSION_1_4)!= JNI_OK) { LOGE("GetEnv failed!"); return result; } register_Native(env); return JNI_VERSION_1_4; } } // namespace android
4. Android.mk
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := DemoNative LOCAL_CERTIFICATE := platform LOCAL_REQUIRED_MODULES := libdemojni LOCAL_JNI_SHARED_LIBRARIES := libdemojni include $(BUILD_PACKAGE) include $(call all-makefiles-under,$(LOCAL_PATH))
5. Copy to Androidsrc
6. mm
7. adb install out/target/product/generic/system/app/DemoNative.apk