加载静态库到android,静态库的提供方式有2种,
a. 通过源文件来编译静态库
b. 加载已经编译好的静态库
首先我们来看,通过源文件来编译静态库,工程目录如下
第一步:我们来看我们的jni目录,目录下包含以下4个文件
Android.mk --- 编译文件
first.c ,first.h --- 外部需要引用的文件
second.c ---- 我们的jni转换文件
首先我们简单的看下源码#include "first.h"
int first(int x, int y)
{
return x + y;
}first.c里面简单的定义了一个加法的方法,然后申明了头文件
second.c : 把first.c的方法转换为jni可以识别的方法。#include "first.h"
#include
jint
Java_com_example_twolibs_TwoLibs_add( JNIEnv* env,
jobject this,
jint x,
jint y )
{
return first(x, y);
}
第二步是重点,我们来分析下mk文件,看编译文件是怎样生成first.c对于的静态文件,并在编译second.c的时候加载静态文件
1:LOCAL_PATH:= $(call my-dir)2:3:# first lib, which will be built statically4:#5:include $(CLEAR_VARS)6:7:LOCAL_MODULE := libtwolib-first8:LOCAL_SRC_FILES := first.c9:10:include $(BUILD_STATIC_LIBRARY)11:12:# second lib, which will depend on and include the first one13:#14:include $(CLEAR_VARS)15:16:LOCAL_MODULE := libtwolib-second17:LOCAL_SRC_FILES := second.c18:19:LOCAL_STATIC_LIBRARIES := libtwolib-first20:21:include $(BUILD_SHARED_LIBRARY)
如上&