androidstudio下NDK正確姿勢(shì)

用androidstudio開(kāi)發(fā)的小伙伴,應(yīng)該知道自身的ndk自動(dòng)編譯就是雞肋!
這里說(shuō)2種方法:
1> 進(jìn)入到工程jni目錄運(yùn)行ndk-build 如何快速?gòu)?fù)制jni路徑

jni路徑

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

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

編譯完成就會(huì)在libs生成各個(gè)平臺(tái)的so文件

2> 第2種方式,腳本配置首先要把a(bǔ)s自動(dòng)編譯關(guān)掉

import org.apache.tools.ant.taskdefs.condition.Os
//導(dǎo)入Os包,方便下面判斷系統(tǒng)平臺(tái)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編譯工具名稱,區(qū)別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 = [] //關(guān)掉自動(dòng)編譯
        jniLibs.srcDirs 'src/main/libs'
    }// This prevents the auto generation of Android.mk
}

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"http://與task任務(wù)名相同
}

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

clean.dependsOn 'hello_ndk_clean'//clean依賴上面定義的任務(wù)


參數(shù)ps:
@NDK_PROJECT_PATH
指定NDK編譯的代碼路徑為當(dāng)前目錄,如果不配置,則必須把工程代碼放Android工程的jni目錄下
@NDK_APP_APPLICATION_MK
指定NDK編譯使用的application.mk文件 @clean清除所有編譯出來(lái)的臨時(shí)文件和目標(biāo)文件
@NDK_OUT
指定編譯生成的文件的存放位置
@NDK_LIBS_OUT
編譯后最終的lib目錄

注意觀察build目錄下生成的一些文件,和編譯配置時(shí)的關(guān)聯(lián)!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容