簡體   English   中英

Android NDK和Gradle:每種構建類型不同的Android.mk

[英]Android NDK and Gradle: Different Android.mk per build type

我的本機庫包含我想在編譯時刪除的日志。 通過在LOCAL_CFLAGS定義預處理器宏ENABLE_DEBUG來顯示日志,如下所示:

include $(CLEAR_VARS)
LOCAL_MODULE    := native-stuff
LOCAL_SRC_FILES := Native.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DENABLE_DEBUG
include $(BUILD_SHARED_LIBRARY)

我正在使用Gradle通過Android Studio構建應用程序,我希望有另一個沒有LOCAL_CFLAGS := -DENABLE_DEBUG Android.mk文件LOCAL_CFLAGS := -DENABLE_DEBUG用於發布版本,有效地禁用日志記錄。

我嘗試通過在src下創建文件夾release/jni並在沒有CFLAGS的情況下放置Android.mk的副本來實現。 它成功構建和部署,但我仍然看到日志。 這是我的build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            //Tell Gradle where to put the compiled shared library
            jniLibs.srcDir 'src/main/libs'

            //disable automatic ndk-build call
            jni.srcDirs = [];
        }
        release {
            jni.srcDirs = [];
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // Tell Gradle the run the ndkBuild task when compiling
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }

    // This task utilizes the Android.mk file defined in src/main/jni so that you
    // have more control over the build parameters (like library inclusion)
    // The system must define property 'androidNdkHome' in ~/.gradle/gradle.properties file
    // to point to NDK path
    task ndkBuild(type: Exec) {
        commandLine "$androidNdkHome/ndk-build", '-C', file('src/main/jni').absolutePath
    }
}

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

我的項目結構如下所示:

src/
   main/
       jni/
          Android.mk
   release/
       jni/
          Android.mk

有可能做我想要的嗎?

我想我在這里有一個解決方案。 它並不像我希望的那樣漂亮(特別是因為它涉及創建一個臨時文件),但我已經測試了它並且它按預期工作。

我寫這個是為了使用另一個Application.mk進行調試構建,名為Application-debug.mk 你可以把APP_CFLAGS := -DENABLE_DEBUG放在里面做你想要的。

關於它如何工作的評論在代碼中。 如果是應用程序構建文件,請使用applicationVariants.all而不是libraryVariants.all

android.libraryVariants.all { variant ->  // executes this block for each variant, current and futures
    def task = tasks.create(name: "ndk${variant.buildType.name.capitalize()}Build") { //create ndkReleaseBuild and ndkDebugBuild tasks
        doLast {
            exec { //execute all this block when the task is executed. (A Build task gets only the commandLine to be executed, so here we're using doLast.exec instead)
                def extraParameter = ""
                def rebuildFlag = ""

                if (variant.buildType.name == "debug")
                    extraParameter = "NDK_APPLICATION_MK=" + file('src/main/jni/Application-debug.mk').absolutePath //reference to another Application.mk to use when doing a debug build.

                def lastBuildFile = file('src/main/jni/.lastbuild.tmp') //save the last build type to a temp file, to avoid triggering rebuilds all the time.
                if (!lastBuildFile.exists())
                    lastBuildFile.createNewFile()

                def lastBuildType = lastBuildFile.text
                if (lastBuildType != variant.buildType.name) {
                    println "== ndk build variant has changed, triggering full rebuild. =="
                    rebuildFlag = "-B"
                }

                if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                    commandLine 'ndk-build.cmd', rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
                } else {
                    commandLine 'ndk-build', rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
                }

                lastBuildFile.write(variant.buildType.name)
            }
        }
    }
    variant.javaCompile.dependsOn task
}

我也把這個代碼作為要點: https//gist.github.com/ph0b/92f1f664c453869636f8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM