AS使用jni并且c++中使用第三方so库

来源:互联网 发布:java成员变量 默认 编辑:程序博客网 时间:2024/06/16 23:25

之前在eclipse中加载jni并且jni中使用到了第三方库的时候很简单只要在Android.mk文件中配置就可以了
例如:
项目jni目录下的文件结构
对应的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 :=cryptoLOCAL_SRC_FILES :=libcrypto.ainclude $(PREBUILT_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE :=sslLOCAL_SRC_FILES :=libssl.ainclude $(PREBUILT_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_SRC_FILES += $(local_src_files)LOCAL_C_INCLUDES += $(local_c_includes)LOCAL_LDLIBS +=-llog -lzLOCAL_STATIC_LIBRARIES += crypto sslLOCAL_MODULE_TAGS := optionalLOCAL_MODULE:= libprotohandlerinclude $(BUILD_SHARED_LIBRARY)

使用时倒入这三个生成的库文件就可以了,度娘那有很多 就不再过多的赘述了。

现在我用Android studio开发了,之前的代码自然要移植到studio上边来,这时候问题就来了。开始移植的时候以为studio兼容eclipse的使用方式,直接弄过来了,一运行果然没有成功。因为Android.mk文件根本没有被调用。
然后又度娘了不少studio的jni调用,然后发现网上的大多都是简单的调用,只是写几句c/c++代码。然后gradle中配置下系统依赖库和生成的名字,就完事儿了,并没有我想要的使用第三方的库,而且使用一下使用两个的方法。然后坑了不少时间。后来发现要使用gradle-experimental插件才能解决这个问题。现在记录一下以备不时之需

目录结构

gradle文件配置:
项目:
项目的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'}

然后就运行成功了!

0 0
原创粉丝点击