Android JNI基础简介--源码中编译JNI

 

1、在对应app工程目录下新建jni目录

    mkdir jni

LINUX/android/packages/apps/Dialer$ ls
AndroidManifest.xml  Android.mk  assets  java  jni  LICENSE

2、CallTimeNvWriter.java文件中加载so库文件,并声明了native方法

public class CallTimeNvWriter {
    ...
    static {
        System.loadLibrary("nvwriter_jni");
    }
    private native String nativeReadflagNV();
    private native void nativeWriteflagNV(int index,char result);

    public static CallTimeNvWriter getInstance() {
        if (sInstance == null) {
            sInstance = new CallTimeNvWriter();
        }
        return sInstance;
    }

    ...
}

3、javac编译class文件

LINUX/android/packages/apps/Dialer/java/com/android/dialer/app/settings$ javac CallTimeNvWriter.java

4、javah编译生成对应的.h文件

LINUX/android/packages/apps/Dialer/java$ javah -classpath com.android.dialer.app.settings.CallTimeNvWriter

5、将.h文件拷贝到jni目录,并新建实现对应的.c文件

6、在jni目录下创建Android.mk文件

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS    := -lm -llog
LOCAL_MODULE_TAGS := optional
LOCAL_CPP_EXTENSION := .cpp
LOCAL_MODULE    := libnvwriter_jni
LOCAL_SRC_FILES := com_android_dialer_app_settings_CallTimeNvWriter.cpp

LOCAL_C_INCLUDES += \
    vendor/qcom/proprietary/fastmmi/libmmi \
    external/libcxx/include \
    external/skia/include/core \
    external/libxml2/include \
    external/icu/icu4c/source/common \
    $(QC_PROP_ROOT)/diag/include \
    $(QC_PROP_ROOT)/diag/src/ \
    $(TARGET_OUT_HEADERS)/common/inc

LOCAL_SHARED_LIBRARIES := \
    libutils \
    libcutils \
    libc \
    libdiag \
    libmmi


LOCAL_MODULE_SUFFIX := .so
LOCAL_PROPRIETARY_MODULE := true
LOCAL_MODULE_CLASS := SHARED_LIBRARIES

include $(BUILD_SHARED_LIBRARY)

7、在app工程目录的Android.mk中添加 ,通过mm或mmm编译Dialer即可生成对应的so库文件。

  LOCAL_JNI_SHARED_LIBRARIES  和 include $(call all-makefiles-under, $(LOCAL_PATH))

...
LOCAL_JNI_SHARED_LIBRARIES := libnvwriter_jni
...
include $(BUILD_PREBUILT)

include $(call all-makefiles-under, $(LOCAL_PATH))
include $(CLEAR_VARS)

 产生so库文件路径:out/target/product/工程project/system/lib/

 

你可能感兴趣的:(JNI)