ndk 开发android,NDK开发 - Android Studio环境搭建

Android studio在很早版本已经开始支持NDK开发了,但是好像一直没有出正式版的gradle插件,现在最新的版本插件版本是0.6.0-alpha5。之前使用好像有点问题,具体忘记了。所以一直使用0.4.0版本开发。

0.4.0是Experimental Plugin版本所以在使用的时候和正式版本的配置还是有挺大的差别的。http://tools.android.com/tech-docs/new-build-system/gradle-experimental/0-4-0是官方文档,上面给出了Experimental Plugin版本使用说明,但是实际使用中还是发现了不少问题,比如签名配置。

Change Config

主要修改包括三个文件

./app/build.gradle

./build.gradle

./gradle/wrapper/gradle-wrapper.properties

./gradle/wrapper/gradle-wrapper.properties

每个插件版本都对应着一个gradle版本

Plugin Version

Gragle Version

0.1.0

2.5

0.2.0

2.5

0.3.0-alpha3

2.6

0.4.0

2.8

0.6.0-alpha1

2.8

0.6.0-alpha5

2.10

因为我使用的是0.4.0版本所以需要将./gradle/wrapper/gradle-wrapper.properties中配置改为

#Wed Oct 21 11:34:03 PDT 2015

distributionBase=GRADLE_USER_HOME

distributionPath=wrapper/dists

zipStoreBase=GRADLE_USER_HOME

zipStorePath=wrapper/dists

distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip

./build.gradle

修改classpath为当前的0.4.0版本插件

buildscript {

repositories {

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle-experimental:0.4.0'

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

jcenter()

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

./app/build.gradle

./app/build.gradle文件需要改动的东西比较多,很多配置与正式版本不一样。

用'com.android.model.application'替换'com.android.application'如果是library模块改为com.android.model.library

在android外层添加根节点model { }

需要添加=给属性赋值

添加元素需要用add函数

下面这个是我自己项目的配置:

apply plugin: 'com.android.model.application'

model {

def signConf

android {

compileSdkVersion = 23

buildToolsVersion = "23.0.2"

defaultConfig.with {

applicationId = "com.example.gnaix.ndk"

minSdkVersion.apiLevel = 15

targetSdkVersion.apiLevel = 22

versionCode = 1

versionName = "1.0"

buildConfigFields.with {

create() {

type = "int"

name = "VALUE"

value = "1"

}

}

}

}

//编译设置

android.buildTypes {

release {

minifyEnabled = true

proguardFiles.add(file('proguard-rules.pro'))

signingConfig = signConf

ndk.with {

debuggable = false

}

}

debug{

minifyEnabled = false

proguardFiles.add(file('proguard-rules.pro'))

ndk.with {

debuggable = true

}

}

}

//签名设置

android.signingConfigs {

create("myConfig") {

keyAlias = 'gnaix'

keyPassword = '990828785'

storeFile = new File('/Users/xiangqing/Documents/workspace/android/gnaix.jks')

storePassword = '990828785'

storeType = "jks"

signConf = it

}

}

//ndk定义

android.ndk {

moduleName = "native" //设置库(so)文件名称

//CFlags.add("-DCUSTOM_DEFINE")

ldLibs.addAll(["log", "android", "EGL", "GLESv1_CM"])

//ldFlags.add("-L/custom/lib/path")

stl = "stlport_static"

}

//ndk编译参数

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")

}

// 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'

}

Signing Config

下面这段代码是官方文档给出的签名配置,但是在实际编译中报错了。改成上面的配置则可以了。

PS:之前我都是新建gradle.properties文件配置签名信息,方便多项目应用,可是在这里好像不行。

apply plugin: "com.android.model.application"

model {

android {

compileSdkVersion = 23

buildToolsVersion = "23.0.2"

}

android.buildTypes {

release {

signingConfig = $("android.signingConfigs.myConfig")

}

}

android.signingConfigs {

create("myConfig") {

storeFile = new File("/path/to/debug.keystore")

storePassword = "android"

keyAlias = "androiddebugkey"

keyPassword = "android"

storeType = "jks"

}

}

}

Source Set

默认情况下C/C++文件是存放在src/main/jni目录下。可以配置android.sources节点改变路径。不过我还是喜欢默认路径。

model {

android {

compileSdkVersion = 22

buildToolsVersion = "22.0.1"

}

android.ndk {

moduleName = "native"

}

android.sources {

main {

jni {

source {

srcDir "src"

}

}

}

}

}

如果有已经编译好的SO包要添加引用:

android.sources {

main {

jniLibs {

dependencies {

library file("./libs/armeabi/libnative.so") abi "armeabi"

library file("./libs/armeabi-v7a/libnative.so") abi "armeabi-v7a"

library file("./libs/x86/libnative.so") abi "x86"

library file("./libs/mips/libnative.so") abi "mips"

}

}

}

}

注意这里需要写SO包命全称,加上lib。

Other Config

文档中还介绍了将SO作为一个独立的lib编译的方式,在实际操作中我也失败了,当时项目时间紧也就没有研究了=_=#。

文档示例/lib/build.gradle:

apply plugin: "com.android.model.native"

model {

android {

compileSdkVersion = 21

}

android.ndk {

moduleName = "hello"

}

android.sources {

main {

jni {

exportedHeaders {

srcDir "src/main/headers"

}

}

}

}

}

文档中还提到其他配置的使用方法,因为项目中没有用到,我也没试过。

示例项目地址https://github.com/gnaix92/as-ndk

下一篇将会介绍SO开发的JNI调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值