之前在eclipse中加载jni并且jni中使用到了第三方库的时候很简单只要在Android.mk文件中配置就可以了
例如:
对应的Android.mk文件:
LOCAL_PATH := $(call my-dir)
local_c_includes := \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include \
local_src_files:= \
com_protocol_OpensslProxy.cpp \
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE :=crypto
LOCAL_SRC_FILES :=libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE :=ssl
LOCAL_SRC_FILES :=libssl.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES += $(local_src_files)
LOCAL_C_INCLUDES += $(local_c_includes)
LOCAL_LDLIBS +=-llog -lz
LOCAL_STATIC_LIBRARIES += crypto ssl
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE:= libprotohandler
include $(BUILD_SHARED_LIBRARY)
使用时倒入这三个生成的库文件就可以了,度娘那有很多 就不再过多的赘述了。
现在我用Android studio开发了,之前的代码自然要移植到studio上边来,这时候问题就来了。开始移植的时候以为studio兼容eclipse的使用方式,直接弄过来了,一运行果然没有成功。因为Android.mk文件根本没有被调用。
然后又度娘了不少studio的jni调用,然后发现网上的大多都是简单的调用,只是写几句c/c++代码。然后gradle中配置下系统依赖库和生成的名字,就完事儿了,并没有我想要的使用第三方的库,而且使用一下使用两个的方法。然后坑了不少时间。后来发现要使用gradle-experimental插件才能解决这个问题。现在记录一下以备不时之需
gradle文件配置:
项目:
app的gradle:
apply plugin: 'com.android.model.application'
def lib_distribution_root = '../distribution'
model {
repositories {
libs(PrebuiltLibraries) {
gmath {
headers.srcDir "${lib_distribution_root}/gmath/include"
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("${lib_distribution_root}/gmath/lib/${targetPlatform.getName()}/libgmath.so")
}
}
gperf {
headers.srcDir "${lib_distribution_root}/gperf/include"
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("${lib_distribution_root}/gperf/lib/${targetPlatform.getName()}/libgperf.so")
}
}
}
}
android {
compileSdkVersion = 23
buildToolsVersion = '23.0.0'
defaultConfig {
applicationId = 'com.example.hellolibs'
minSdkVersion.apiLevel = 13
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = '1.0'
}
ndk {
platformVersion = 21
moduleName = 'hello-libs'
toolchain = 'clang'
stl = 'gnustl_static'
cppFlags.addAll(['-std=c++11'])
ldLibs.addAll(['android', 'log'])
}
sources {
main {
jni {
dependencies {
library 'gmath' linkage 'shared'
library 'gperf' linkage 'shared'
}
}
jniLibs {
source {
srcDir "${lib_distribution_root}/gmath/lib"
srcDir "${lib_distribution_root}/gperf/lib"
}
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles.add(file('proguard-android.txt'))
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
}
然后就运行成功了!