首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android.mk和Gradle:每个构建类型的不同Android.mk

Android.mk和Gradle:每个构建类型的不同Android.mk
EN

Stack Overflow用户
提问于 2015-03-05 13:13:09
回答 1查看 4.3K关注 0票数 5

我的本机库包含我希望在编译时删除的日志。通过在ENABLE_DEBUG中定义预处理宏LOCAL_CFLAGS来显示日志,如下所示:

代码语言:javascript
复制
include $(CLEAR_VARS)
LOCAL_MODULE    := native-stuff
LOCAL_SRC_FILES := Native.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DENABLE_DEBUG
include $(BUILD_SHARED_LIBRARY)

我正在通过Android构建这个带有Gradle的应用程序,我希望有另一个没有LOCAL_CFLAGS := -DENABLE_DEBUGLOCAL_CFLAGS := -DENABLE_DEBUG文件用于发布版本,从而有效地禁用日志记录。

我试图通过在src下创建文件夹src,并在没有CFLAGS的情况下放置Android.mk副本。它成功地构建和部署,但我仍然看到日志。这是我的build.gradle

代码语言:javascript
复制
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'
}

我的项目结构如下:

代码语言:javascript
复制
src/
   main/
       jni/
          Android.mk
   release/
       jni/
          Android.mk

能做我想做的事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 15:59:44

我想我已经找到解决办法了。它不像我所希望的那样漂亮(尤指)。因为它涉及创建一个临时文件),但是我已经对它进行了测试,并且它正在按预期工作。

我编写这篇文章是为了使用另一个用于调试构建的Application.mk,名为Application-调试器to。你可以把APP_CFLAGS := -DENABLE_DEBUG放在里面去做你想做的事。

关于其工作方式的注释在代码中。如果是用于应用程序构建文件,则使用applicationVariants.all而不是libraryVariants.all

代码语言:javascript
复制
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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28878689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档