android studio1.4还没有稳定的支持ndk开发,配置环境时候遇到了些问题,在这里做个记录。
一、环境介绍:
android studio 1.4
gradle 2.5
android-ndk-r10e
二、注意事项:
1. gradle配置
这个版本的android studio默认的是gradle 2.4,首先将项目gradle升至2.5。
打开project structure,将Project选项中Gradle version属性改为2.5,ok后会自动下载升级到2.5版。
如果下载速度太慢可以百度gradle下载,然后下载gradle-2.5-all解压到自己的软件安装目录里。然后在Settings设置use local gradle distribution,选择gradle的解压目录。
然后修改项目下gradle/wrapper/gradle-wrapper.properties文件,主要修改distributionUrl属性,其他默认即可。
#Mon Nov 02 15:45:56 CST 2015 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https://services.gradle.org/distributions/gradle-2.5-all.zip
修改项目的build.gradle,把android studio的gradle tools改为实验版的gradle-experimental:0.2.0
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle-experimental:0.2.0' } } allprojects { repositories { jcenter() } }
重要的是修改模块app的build.gradle,2.5版gradle的语法改变很大,具体变化可查看文档
apply plugin: 'com.android.model.application' model { android { compileSdkVersion = 23 buildToolsVersion = '23.0.2' defaultConfig.with { applicationId = "com.exc.zhen.testdemo" minSdkVersion.apiLevel = 19 targetSdkVersion.apiLevel = 23 versionCode = 1 versionName = "1.0" } } android.buildTypes { release { // 是否进行混淆 minifyEnabled = false // 混淆文件的位置 proguardFiles += file('proguard-rules.txt') } } compileOptions.with { sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_7 } android.ndk { //jni liabrary名字 moduleName = "MyJni" /*C++ runtime support library system -> Use the default minimal system C++ runtime library. gabi++_static -> Use the GAbi++ runtime as a static library. gabi++_shared -> Use the GAbi++ runtime as a shared library. stlport_static -> Use the STLport runtime as a static library. stlport_shared -> Use the STLport runtime as a shared library. gnustl_static -> Use the GNU STL as a static library. gnustl_shared -> Use the GNU STL as a shared library. c++_static -> Use the LLVM libc++ as a static library. c++_shared -> Use the LLVM libc++ as a shared library.*/ // Note that CFlags has a capital C, which is inconsistent with // the naming convention of other properties. This is a // technical limitation that will be resolved //CFlags += "-DCUSTOM_DEFINE" //cppFlags += "-DCUSTOM_DEFINE" //ldFlags += "-L/custom/lib/path" stl = "stlport_static"//以静态库方式调用android-ndk-r10esourcescxx-stlstlport下的c++标准库 ldLibs = ["android", "log"] } android.productFlavors { // for detailed abiFilter descriptions, refer to "Supported ABIs" @ // https://developer.android.com/ndk/guides/abis.html#sa //各种cpu架构的编译方案,只用一个即可 // create("arm") { // ndk.abiFilters += "armeabi" // } // create("arm7") { // ndk.abiFilters += "armeabi-v7a" // } // create("arm8") { // ndk.abiFilters += "arm64-v8a" // } // create("x86") { // ndk.abiFilters += "x86" // } // create("x86-64") { // ndk.abiFilters += "x86_64" // } // create("mips") { // ndk.abiFilters += "mips" // } // create("mips-64") { // ndk.abiFilters += "mips64" // } // 所有cpu架构都编译的话就用这句就可以了,不进行任何过滤 //create("all") // 要编译armeabi和armeabi-v7a时这样设置,其他方案也可以类似设置 create("arms") { ndk.abiFilters = ["armeabi", "armeabi-v7a"] } } //添加c++源文件位置,我没有设置也可以 // android.sources { // main { // jni { // source { // srcDirs 'src/main/jni' // } // } // } // } //打包时要去除的文件 android.packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt' exclude 'META-INF/NOTICE' } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') //我这里加testCompile会提示错误,就去掉了 //testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' }
2. ndk配置
下载android-ndk-r10e解压到自己的安装目录,我放在D:android-ndk-r10e中,将该目录加入系统环境变量path中。
在project structure的SDK Location中选择Android NDK location为自己的ndk目录。
检查下项目的local.properties文件配置,一般会自动生成sdk目录和ndk目录的配置信息
以上就是让我折腾了好几天的部分,主要还是软件版本支持问题和gradle2.5新语法问题。有了以上的基础,就可以愉快的进行jni开发了,生成调用so库等在android studio里容易了很多,不需要自己写mk文件了,具体过程网上有很多资料。
以下是我编译后生成so库的结果,会默认在自己设置的库名加lib前缀。
参考资料:
http://www.bubuko.com/infodetail-1006521.html
http://www.shangxueba.com/jingyan/2901837.html
http://tools.android.com/tech-docs/new-build-system/gradle-experimental