有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知道其它方法,还望不吝不吝赐教。
比如,在androidproject里有两个activity,各自是activity1和activity2。(能够进入project文件夹bin/classes路径下查看有哪些)。在这两个activity里都有调用jni,过程例如以下:
1、 在activity1和activity2里分别声明native
c
比如:activity1.java里
private native String
StringFromActivity1Jni();
static
{
System.loadLibrary("activity1");
}
Activity2.java里
private native String
StringFromActivity2Jni();
static
{
System.loadLibrary("activity2");
}
2、 生成 .h文件
用命令:javah -classpath bin/classes/ -jni com.myprj.uitest.activity1和javah
-classpath bin/classes/ -jnicom.myprj.uitest.activity2 生成activity1和activity2里的头文件
须要特别注意的是红色部分。一定要存在。能够进入project文件夹bin/classes路径下查看。假设不能在jni下生成,那就mv吧。
3、 编写Android.mk文件:
LOCAL_PATH := $(call my-dir) //仅仅能有一个这个路径
include $(CLEAR_VARS)
LOCAL_MODULE := activity1
LOCAL_C_INCLUDES += $(LOCAL_PATH) \
$(LOCAL_PATH)/android
LOCAL_SRC_FILES := activity1.c
LOCAL_LDLIBS := -L$(LOCAL_PATH) -lm -lz -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := activity2
LOCAL_C_INCLUDES += $(LOCAL_PATH) \
$(LOCAL_PATH)/android
LOCAL_SRC_FILES := activity2.c
LOCAL_LDLIBS := -L$(LOCAL_PATH) -lm -lz -llog
include $(BUILD_SHARED_LIBRARY)
须要注意的是:include $(BUILD_SHARED_LIBRARY)是生成动态库,也能够生成静态库include
$(PREBUILT_STATIC_LIBRARY)
4、 编译:
ndk-build