changing resValue in variant
Asked Answered
Y

2

9

I have two product flavors and three build types.

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        signingConfig signingConfigs.release
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
    qa {
        applicationIdSuffix ".qa"
        signingConfig signingConfigs.release
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

productFlavors {
    old {
        applicationId "com.companyname.old"
        buildConfigField "String", "contentProvider", '"com.companyname.android.mobile.contentprovider"'
        resValue "string", "content_provider_authority", '"com.companyname.android.mobile.contentprovider"'

    }
    new {
        applicationId "com.companyname.new"
        buildConfigField "String", "contentProvider", '"' + applicationId + '.contentprovider"'
        resValue "string", "content_provider_authority", '"' + applicationId + '.contentprovider"'
    }
}

Is there a way I can change the resValue of content_provider_authority based not on product flavor, but for the variant? I want all builds for the product flavor new to use the application id + ".contentprovider" but for our product flavor old, use the hardcoded string if debug or release like shown above, but enhance it to have a different hardcoded string for qa build type.

Yevetteyew answered 14/1, 2015 at 23:12 Comment(4)
try something like this android.applicationVariants.all { variant -> variant.resValue "color", "colorPrimary", "#" + theme.colorPrimary } variant has properties that you need(like fx variant.name) ... put this into build.gradle without "parent" (fx.: at the end of file)Hanes
i'm using it to generete colors to theme like this gist.github.com/SelvinPL/f89476b6585690cb252b it creates gist.github.com/SelvinPL/8e673e38c5fed8e2132b AXX states for alpha X%Hanes
Where can I get more documentation on applicationVariants? Does that loop over them all? I only care about one specific case. I guess I could loop over them all and have some kind of "if" statement in there to just go into that code change for that one variant.Yevetteyew
tools.android.com/tech-docs/new-build-system/user-guide , yes all variants(build types x product flavor), and yes if is possible ... but still you have to add "content_provider_authority" to all variants, so you can always put "com.companyname.android.mobile.contentprovider" there and put somthing else if it is a specific variantHanes
S
6

This code is working for me, thanks to the comment of Selvin

productFlavors {
        red {
            ext {
                googleMapsKey = [debug: "AIza4115643435", release: "AIzaXXXXXXXXXX"]
            }
        }

        blue {
            ext {
                googleMapsKey = [debug: "AIza6363474341", release: "AIzaXXXXXXXXXX"]
            }
        }

        applicationVariants.all { variant ->
            def flavor = variant.productFlavors[0]
            variant.resValue  "string", "google_maps_key", "\"${flavor.ext.googleMapsKey[variant.buildType.name]}\""
        }
    }
Sands answered 22/1, 2019 at 12:15 Comment(0)
F
2

Be aware you cannot have the same string name on your main/res/values/strings.xml and resValue on your build.gradle product flavor definition, otherwise the build will complain about duplicate resources.

TL; DR;

Let's suppose you want to change the app_name string resource for your different product flavors. You may insert/edit the following code on your app/build.gradle.

flavorDimensions "default"

productFlavors{

    production {
        dimension "roadmap"
        applicationId "com.yourbundleid"
        versionCode 1
        versionName "0.1.0"
        resValue "string", "app_name", 'YourAwesomeApp'
    }

    beta {
        dimension "roadmap"
        applicationIdSuffix ".beta"
        versionNameSuffix "-beta"
        versionCode 1
        versionName "0.1.0"
        resValue "string", "app_name", 'YourAwesomeApp BETA'
    }

    dev {
        dimension "roadmap"
        applicationIdSuffix ".dev"
        versionNameSuffix "-dev"
        versionCode 1
        versionName "0.1.0"
        resValue "string", "app_name", 'YourAwesomeApp DEV'
    }
}

Factitious answered 2/3, 2020 at 0:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.