简体   繁体   English

如何在参数化的Gradle构建中添加特定任务的风味

[英]How do I do flavor specific tasks in a parameterized gradle build

I have a flavored android build and I am trying to run a task only when building a particular flavor. 我有一个增值的android构建,并且我试图仅在构建特定的增值时运行任务。

in my Gradle I have 在我的摇篮中,我有

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 70
        versionName "4.0.1"
        multiDexEnabled = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    productFlavors {
        def versionNumber = "4.0"
        unitTest
        up {
            applicationId "com.livelys.up"
        }
        down {
             applicationId "com.livelys.down"
        }
        strange {
            applicationId "com.livelys.strange"
        }

    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/res']
        }
        strange {
            java.srcDirs = ['src/strange/java']
            resources.srcDirs = ['src/strange/res']
        }
        unitTest {
            java.srcDirs = ['src/test/java']
            assets {
                srcDirs = ['src/test/assets']
            }
        }
    }

}
def buildAliases = [
        'default'             : ['clean', 'assemble', 'testUnitTestDebug'],
        'up'          : ['clean', 'assembleUp', 'testUnitTestDebug'],
        'down'          : ['clean', 'assembleDown', 'testUnitTestDebug'],
        'strange'             : ['clean', 'doMagic', 'assembleStrange']
]

task magic << {
    File srcFile = file('src/partner/res/values/magic.xml')
    def content = srcFile.text
    def pcode = System.getProperty('Param1')
    def defaultLocation = System.getProperty('Param2')
    srcFile.text = content.replaceAll("%%%P1%%%","${pcode}").replaceAll("%%%P2%%%","${defaultLocation}")
}

I want to run Magic, if and only if I am building the strange flavor. 当且仅当我正在构建奇怪的味道时,我才想运行Magic。 The params are fed from a Jenkins build script and are used to rewrite custom versions of the app. 这些参数是从Jenkins构建脚本中获取的,用于重写应用程序的自定义版本。 I don't want to maintain a custom flavor for each custom version since that will become a maintenance nightmare. 我不想为每个自定义版本维护自定义样式,因为这将成为维护的噩梦。

Yo should go to Run -> Edit configurations..., click on add -> gradle 哟应该去运行->编辑配置...,单击添加-> gradle

Gradle project: Enter your project name ( ctrl + space to see hint). Gradle项目:输入项目名称( Ctrl + 空格以查看提示)。 Like project:hello-world project:hello-world
Tasks: Enter :ProjectName:magic , there's no hint. 任务:输入:ProjectName:magic ,没有提示。 Like :hello-world:magic (Don't forget the colon at first!) 就像:hello-world:magic (一开始不要忘了冒号!)

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

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