简体   繁体   English

Android NDK和Gradle:每种构建类型不同的Android.mk

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

My native library contains logs which I would like to remove at compile time. 我的本机库包含我想在编译时删除的日志。 Logs are shown by defining the pre-processor macro ENABLE_DEBUG in LOCAL_CFLAGS like so: 通过在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)

I'm building the app with Gradle via Android Studio and I would like to have another Android.mk file without LOCAL_CFLAGS := -DENABLE_DEBUG for release builds, effectively disabling logging. 我正在使用Gradle通过Android Studio构建应用程序,我希望有另一个没有LOCAL_CFLAGS := -DENABLE_DEBUG Android.mk文件LOCAL_CFLAGS := -DENABLE_DEBUG用于发布版本,有效地禁用日志记录。

I tried to do it by creating the folders release/jni under src and put a copy of Android.mk without the CFLAGS. 我尝试通过在src下创建文件夹release/jni并在没有CFLAGS的情况下放置Android.mk的副本来实现。 It builds and deploys successfully, but I still see the logs. 它成功构建和部署,但我仍然看到日志。 Here is my build.gradle : 这是我的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'
}

My project structure looks like this: 我的项目结构如下所示:

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

Is it possible to do what I want? 有可能做我想要的吗?

I think I've got a solution here. 我想我在这里有一个解决方案。 It's not as pretty as I would have liked it to be (esp. as it involves creating a temporary file), but I've tested it and it's working as intended. 它并不像我希望的那样漂亮(特别是因为它涉及创建一个临时文件),但我已经测试了它并且它按预期工作。

I've written this to use another Application.mk for debug builds, called Application-debug.mk . 我写这个是为了使用另一个Application.mk进行调试构建,名为Application-debug.mk You can put APP_CFLAGS := -DENABLE_DEBUG inside to do what you want. 你可以把APP_CFLAGS := -DENABLE_DEBUG放在里面做你想要的。

Comments on how it works are in the code. 关于它如何工作的评论在代码中。 If it's for an application build file, use applicationVariants.all instead of libraryVariants.all : 如果是应用程序构建文件,请使用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
}

I've pushed this code as gist as well: https://gist.github.com/ph0b/92f1f664c453869636f8 我也把这个代码作为要点: https//gist.github.com/ph0b/92f1f664c453869636f8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM