Gradle获取当前构建类型

时间:2014-10-09 20:11:24

标签: android intellij-idea gradle android-studio

所有!

我正在尝试让Gradle为发布和调试版本生成不同的文件(Android.mk和Application.mk)。默认方式,gradle android插件这样做并不适合我,因为它不允许修改Application.mk,我想。主要问题是我无法识别当前的构建类型。 我尝试了以下方法:

android {
    ...
    defaultConfig {
        project['CONFIGURATION_FLAGS'] = ''

        project['APP_ABI'] = ''
        project['APP_PLATFORM'] = 'android-9'
        project['APP_STL'] = 'gnustl_static'
        project['NDK_TOOLCHAIN_VERSION'] = 'clang'
    }
    buildTypes {
        release {
            project['CONFIGURATION_FLAGS'] = '-fvisibility=hidden'
            project['APP_ABI'] = 'armeabi x86'
        }
        debug {
            project['CONFIGURATION_FLAGS'] = '-g -DDEBUG -DENABLE_LOG'
            project['APP_ABI'] = 'armeabi'
        }
    }
}
task processTemplates {
    def templatesDir = System.getProperty('user.dir') + '/app/templates'
    def jniDir = System.getProperty('user.dir') + '/app/src/main/jni'

    // Android.mk
    def configFlags = project['CONFIGURATION_FLAGS']

    def androidMk = file(templatesDir + '/Android.mk').text
    androidMk = androidMk.replaceAll '<CONFIGURATION_FLAGS>', configFlags
    def newAndroidMk = new File(jniDir + '/Android.mk')
    newAndroidMk.text = androidMk

    // Application.mk
    def appAbi = project['APP_ABI']
    def appPlatform = project['APP_PLATFORM']
    def appStl = project['APP_STL']
    def toolchain = project['NDK_TOOLCHAIN_VERSION']

    def applicationMk = file(templatesDir + '/Application.mk').text
    applicationMk = applicationMk.replaceAll '<APP_ABI>', appAbi
    applicationMk = applicationMk.replaceAll '<APP_PLATFORM>', appPlatform
    applicationMk = applicationMk.replaceAll '<APP_STL>', appStl
    applicationMk = applicationMk.replaceAll '<NDK_TOOLCHAIN_VERSION>', toolchain
    def newApplicationMk = new File(jniDir + '/Application.mk')
    newApplicationMk.text = applicationMk
}

但是发现参数的设置执行了2次,也就是说,对于每种类型的构建,无论当前的构建类型如何。这导致了对于任何类型的构建它设置调试选项的事实。谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

添加一个任务,该任务取决于调用后的每个assembleXxx任务和属性设置

查看我的answer类似问题。