androidstudio下NDK正确姿势

用androidstudio开发的小伙伴,应该知道自身的ndk自动编译就是鸡肋!
这里说2种方法:
1> 进入到工程jni目录运行ndk-build
如何快速复制jni路径

jni路径

右键Copy Path或者按快捷键Ctrl+Shift+C

cd /home/wangxiong/Documents/Github/libraries/blur/src/main/jni
~/Soft/android-ndk-r10e/ndk-build

编译完成就会在libs生成各个平台的so文件

result

2> 第2种方式,脚本配置
首先要把as自动编译关掉

sourceSets.main {
        jniLibs.srcDirs 'src/main/libs'
        jni.srcDirs = [] // This prevents the auto generation of Android.mk
}

看代码,这里借鉴了Facebook的Fresco图片框架的gradle相关配置写法
传送门:https://github.com/facebook/fresco/blob/master/imagepipeline/build.gradle
可以研究下脚本的相关写法

import org.apache.tools.ant.taskdefs.condition.Os
//导入Os包,方便下面判断系统平台Linux \ windows
apply plugin: 'com.android.library'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
}


def getNdkBuildName() {//NDK编译工具名称,区别Linux和windows
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        return "ndk-build.cmd"
    } else {
        return "ndk-build"
    }
}

def getNdkBuildFullPath() {//NDK全路径
    File propFile = project.rootProject.file('local.properties')
    if (!propFile.exists()) {
        return getNdkBuildName()
    }
    Properties properties = new Properties()
    properties.load(propFile.newDataInputStream())
    def ndkCommand = properties.getProperty('ndk.command')
    if (ndkCommand != null) {
        return ndkCommand
    }
    def path = null
    def ndkPath = properties.getProperty('ndk.path')
    if (ndkPath != null) {
        path = ndkPath
    } else {
        def ndkDir = properties.getProperty('ndk.dir')
        if (ndkDir != null) {
            path = ndkDir
        }
    }
    if (path != null) {
        if (!path.endsWith(File.separator)) {
            path += File.separator
        }
        return path + getNdkBuildName()
    } else {
        // if none of above is provided, we assume ndk-build is already in $PATH
        return getNdkBuildName()
    }
}

android {
    compileSdkVersion = 23
    buildToolsVersion = "23.0.3"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode = 200
        versionName = "2.0.0"
    }

    buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.pro'))
        }
    }

    sourceSets.main {
        jni.srcDirs = []//关掉自动编译
        jniLibs.srcDirs 'src/main/libs'
    }// This prevents the auto generation of Android.mk
}

//编译任务注意type: Exec
task hello_ndk_build(type: Exec) {
    commandLine getNdkBuildFullPath(),
            'NDK_APPLICATION_MK=Application.mk',
            'NDK_OUT=' + temporaryDir,
            "NDK_LIBS_OUT=" + file("src/main/libs").absolutePath,
            '-C', file("src/main/jni").absolutePath,
            '--jobs', Runtime.getRuntime().availableProcessors()
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn "hello_ndk_build"//与task任务名相同
}

task hello_ndk_clean(type: Exec) {
    commandLine getNdkBuildFullPath(),
            'clean',
            '-C', file("src/main/jni").absolutePath
}

clean.dependsOn 'hello_ndk_clean'//clean依赖上面定义的任务

参数ps:
@NDK_PROJECT_PATH
指定NDK编译的代码路径为当前目录,如果不配置,则必须把工程代码放Android工程的jni目录下
@NDK_APP_APPLICATION_MK
指定NDK编译使用的application.mk文件
@clean
清除所有编译出来的临时文件和目标文件
@NDK_OUT
指定编译生成的文件的存放位置
@NDK_LIBS_OUT
编译后最终的lib目录

这里写图片描述

注意观察build目录下生成的一些文件,和编译配置时的关联!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值