Android studio配置jni开发环境,主要配置是两个build文件,以及新建一个jni文件,放c代码。
代码如下1:
apply plugin: 'com.android.model.application' model { android { compileSdkVersion = 23 buildToolsVersion = "23.0.2" defaultConfig.with { applicationId = "me.chunsheng.jni" minSdkVersion.apiLevel = 9 targetSdkVersion.apiLevel = 23 } } /* * native build settings */ android.ndk { moduleName = "hello-world" /* moduleName = "hello-jni" * Other ndk flags configurable here are * cppFlags.add("-fno-rtti") * cppFlags.add("-fno-exceptions") * ldLibs.addAll(["android", "log"]) * stl = "system" */ } android.buildTypes { release { minifyEnabled = false multiDexEnabled = true proguardFiles.add(file("proguard-rules.pro")) } } android.productFlavors { // for detailed abiFilter descriptions, refer to "Supported ABIs" @ // https://developer.android.com/ndk/guides/abis.html#sa create("arm") { ndk.abiFilters.add("armeabi") } create("arm7") { ndk.abiFilters.add("armeabi-v7a") } create("arm8") { ndk.abiFilters.add("arm64-v8a") } create("x86") { ndk.abiFilters.add("x86") } create("x86-64") { ndk.abiFilters.add("x86_64") } create("mips") { ndk.abiFilters.add("mips") } create("mips-64") { ndk.abiFilters.add("mips64") } // To include all cpu architectures, leaves abiFilters empty create("all") } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.0' }
代码如下2:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle-experimental:0.4.0' } } allprojects { repositories { jcenter() } }
直接编译,运行即可调用c代码了,本次Android studio版本 2.0—beta 7.以及配置文件如上。
由于版本更新,gradle的语法的改变,build文件可能会发生变化,时刻关注官网
http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-0.2.x---0.4.0