环境
android studio 2.0
安装
File>Other Settings>Default Project Structure…
注意,这里有可能你会发现你的路经不能点击,那是因为被墙了,破墙下载
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle-experimental:0.4.0'
} }
allprojects { repositories { jcenter() } }
|
注意自己写时,要添加=
、.apiLevel
,写法不一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| apply plugin: 'com.android.model.application'
model { android { compileSdkVersion = 23 buildToolsVersion = "23.0.2"
defaultConfig.with { applicationId = "com.xuie.jnidemo" minSdkVersion.apiLevel = 15 targetSdkVersion.apiLevel = 23 versionCode = 1 versionName = "1.0" } }
compileOptions.with { sourceCompatibility=JavaVersion.VERSION_1_7 targetCompatibility=JavaVersion.VERSION_1_7 }
android.ndk { moduleName = "hello-jni"
} android.buildTypes { release { minifyEnabled = false proguardFiles.add(file('proguard-rules.txt')) } } android.productFlavors { 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") } create("all") } }
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' }
|
编译
1 2
| JniInterface jniInterface = new JniInterface() ((TextView)findViewById(R.id.tv)).setText(jniInterface.stringFromJNI())
|
- 他人调用
我们本身NDK调用,是不希望自己的工程里出现C/C++,那就直接可以调用.so
,我以armeabi-v7a
架构为例进行调用(因为我手机是这个架构),拷贝目录build/intermediates/binaries/debug/all/lib/armeabi-v7a/
下的libhello-jni.so
到所用工程main/jniLibs/armeabi-v7a
下,调用时java目录下同样需要添加JniInterface.java
,注意包名要根原来一模一样,然后再调用,实际项目中要将所有架构下的.so
都拷贝进去1 2
| JniInterface jniInterface = new JniInterface(); ((TextView)findViewById(R.id.tv)).setText("[Use]" + jniInterface.stringFromJNI());
|