Search code examples
androidgradleandroid-gradle-pluginandroid-productflavors

Add suffix to generated string resource


I have a gradle config as setup below. To allow for side by side installs of different builds/flavors

buildTypes {
    release {
    }
    debug {
        applicationIdSuffix ".debug"
        // Somehow add debug suffix to app ID?
    }
}
productFlavors {
    ci {
        applicationId "com.myapp.ci"
        ext.betaDistributionGroupAliases = "mobileworkforce.ci"
        resValue "string", "app_name", "AppName.CI"
    }

    staging {
        applicationId "com.myapp.staging"
        resValue "string", "app_name", "AppName.Staging"
    }

    production {
        applicationId "com.myapp"
        resValue "string", "app_name", "AppName"
    }

The issue is that I cannot figure out how to update the app_name string resource to have the suffix "Debug" to the app_name string resource (used as the label for the application)


Solution

  • I was able to create a viable solution using manifestPlaceholders instead of generating string resources. It is not ideal because the result is AppName.Debug.CI instead of AppName.CI.Debug but it works.

    buildTypes {
        release {
             manifestPlaceholders = [ activityLabel:"AppName"]
        }
        debug {
            applicationIdSuffix ".debug"
                         manifestPlaceholders = [ activityLabel:"AppName.Debug"]
        }
    }
    productFlavors {
        def addActivityLabelSuffix = { placeholders, suffix ->
            def appName = placeholders.get("activityLabel")
            placeholders.put("activityLabel", appName + suffix);
        }
        ci {
            applicationId "com.myapp.ci"
            ext.betaDistributionGroupAliases = "mobileworkforce.ci"
            addActivityLabelSuffix getManifestPlaceholders(), ".CI"
        }
    
        staging {
            applicationId "com.myapp.staging"
            addActivityLabelSuffix getManifestPlaceholders(), ".Staging"
        }
    
        production {
            applicationId "com.myapp"
            resValue "string", "app_name", "AppName"
        }
    }