简体   繁体   中英

android gradle - how to create a new product flavor but reuse values from another flavor?

i want to make a new flavor in my build.gradle file but its a complete duplicate of another flavor. im making it for testing purposes. Only the applicationid suffix will be different. this flavor i want to be called beta. But i was hoping for a way to not have to duplicate code. since it has all the same settings as my staging flavor, is there a way i can just tell gradle to copy the same settings that are in staging flavor. My flavors are defined like this:

    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 ?

In our app we are using different flavours for different builds with one main package which is used by both flavours.

We are defining it in gradle like this:

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

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

With packages:

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

The base activities/fragments are in 'main', flavours are sharing fragments/activities names. We use also generics which are using prepared extendable main classes for that.

Endpoint strings are in res/strings which are separate for each flavour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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