android gradle - 如何创建新的产品风味但重用其他风味的值?

时间:2017-08-26 16:02:19

标签: android android-gradle

我想在build.gradle文件中创建一个新的味道,但它与另一种味道完全重复。我正在进行测试。只有applicationid后缀会有所不同。这种味道我想被称为beta。但我希望有一种不必重复代码的方法。因为它具有与我的分期味道相同的设置,有没有办法让我可以告诉gradle复制相同的设置,这些设置处于分期味道。我的口味定义如下:

    productFlavors {

        def STRING = "String"
        def BOOLEAN = "boolean"
        def TRUE = "true"
        def FALSE = "false"
        def FLAVOR = "FLAVOR"
        def RETROFIT_LOG_ALL = "RETROFIT_LOG_ALL"
        def BASE_ENDPOINT = "BASE_ENDPOINT"


        staging {
            buildConfigField STRING, BASE_ENDPOINT, '"https://pepsi-staging-api.pmlo.co"'
            buildConfigField STRING, BASE_IMGIX_ENDPOINT, '"myappfashion.imgix.net"'
            buildConfigField STRING, BASE_ENDPOINT_WEB, '"www.pepsi-staging.pmlo.co"'
            buildConfigField BOOLEAN, RETROFIT_LOG_ALL, TRUE
            manifestPlaceholders = [appDrawerName: "myapp-Staging"]
            ext.enableCrashlytics = false
            versionNameSuffix '-STAGING'
            applicationIdSuffix 'staging'
        }


//why do i have to copy the same settings again, is there a way to just over ride from the staging flavor the things i want changed ?
        beta {
            buildConfigField STRING, BASE_ENDPOINT, '"https://pepsi-staging-api.pmlo.co"'
            buildConfigField STRING, BASE_IMGIX_ENDPOINT, '"myappfashion.imgix.net"'
            buildConfigField STRING, BASE_ENDPOINT_WEB, '"www.pepsi-staging.pmlo.co"'
            buildConfigField BOOLEAN, RETROFIT_LOG_ALL, TRUE
            manifestPlaceholders = [appDrawerName: "myapp-Staging"]
            ext.enableCrashlytics = false
            versionNameSuffix '-beta' //only these two thigns are different
            applicationIdSuffix 'beta'
        }


        prod {
            // applicationId "com.mobile.myapp"
            buildConfigField STRING, BASE_ENDPOINT, '"https://api.myappfashion.com"'
            buildConfigField STRING, BASE_IMGIX_ENDPOINT, '"myappfashion.imgix.net"'
            buildConfigField STRING, BASE_ENDPOINT_WEB, '"www.myappfashion.com"'
            buildConfigField BOOLEAN, RETROFIT_LOG_ALL, FALSE
            ext.enableCrashlytics = true
            manifestPlaceholders = [appDrawerName: "myapp"]
        }

}

    i just want to override versionNameSuffix & applicationIdSuffix only in beta and copy all values from staging. is there a way not to duplicate code like this example ?

1 个答案:

答案 0 :(得分:0)

在我们的应用程序中,我们使用不同版本的不同口味,使用一个主要包装,两种口味都使用。

我们正在gradle中定义它:

    productFlavors {
    mock {
        applicationIdSuffix = ".mock"
    }
    prod {

    }
    flavour1 {
        applicationIdSuffix = ".flavour1"
        versionCode = 1
        versionName "1.0." + versionCode
    }
    flavour2 {
        applicationIdSuffix = ".flavour2"
        versionCode = 2
        versionName "1.0." + versionCode
    }
}

使用包:

-> src
---> flavour1
-----> java
-----> res
---> flavour2
-----> java
-----> res
---> main
-----> assets
-----> java
-----> res
-----> AndroidManifest.xml

基本活动/片段是主要的',风味是共享片段/活动名称。我们也使用泛型,它们使用准备好的可扩展主类。

端点字符串位于res / strings中,每个字符串都是独立的。