目录结构
src/|-- main| |--java| |--jni| | |--lib| | | |--arm64| | | | |--libmodpdfium.so| | | || | | |--armeabi-v7a| | | | |--libmodpdfium.so| | | || | | |--armeabi-v7a| | | | |--libmodpdfium.so| | | || | | |--x86| | | |--libmodpdfium.so| | | | | |--mainJNILib.cpp| | |--util.hpp| | | |--jniLibs| |--res|
Project的build.gradle文件
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' classpath 'com.android.tools.build:gradle-experimental:0.8.2' // 增加 classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' }}allprojects { repositories { jcenter() }}
Module的build.gradle文件
apply plugin: 'com.android.model.library'model{ android { compileSdkVersion = 24 buildToolsVersion = "24.0.2" defaultConfig { minSdkVersion.apiLevel 9 targetSdkVersion.apiLevel 22 versionCode 1 versionName "1.4.0" } } android.ndk{ moduleName "jniPdfium" //生成的so名字 abiFilters.addAll([ "arm64", "armeabi","armeabi-v7a", "mips", "x86", "x86_64"]) //会生成指定体系结构下的so库 ldLibs.addAll(["log", "android", "jnigraphics"]) stl "stlport_static" cppFlags.add("-fexceptions") cppFlags.add("-DHAVE_PTHREADS") } android.buildTypes { release { minifyEnabled false// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles.add(file("proguard-rules.txt")) } } // c文件依赖的so库 repositories { prebuilt(PrebuiltLibraries) { libmodpdfium{ // so库文件名 headers.srcDir "src/main/jni" // so库的头文件 binaries.withType(SharedLibraryBinary) { sharedLibraryFile = file("src/main/jni/lib/${targetPlatform.getName()}/libmodpdfium.so") // so库的位置 } } } } android.sources { main{ jni { dependencies { // c文件需要调用so库的函数 library "libmodpdfium" } source { // c文件的位置 srcDirs "src/main/jni" } } jniLibs { source { // jniLibs位置 srcDir "src/main/jniLibs" } } } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:support-v4:24.2.1'}
参考:
Experimental Plugin User Guidehttp://tools.android.com/tech-docs/new-build-system/gradle-experimental在Android Studio中使用NDK/JNI - 实验版插件用户指南https://my.oschina.net/wolfcs/blog/550677