Add suffix to generated string resource

1.6k views Asked by At

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)

2

There are 2 answers

0
cyroxis On BEST ANSWER

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"
    }
}
0
mikep On

Using getManifestPlaceholders().get("...") I always get null, so I was looking for another solution... and maybe is better because I can get correct order in app name...

// build.gradle
def appNameBase = "My App"
buildTypes {
    release {
        manifestPlaceholders = [ appNameBase: appNameBase, appNameSuffix: "" ]
    }
    debug {
        manifestPlaceholders = [ appNameBase: appNameBase, appNameSuffix: " Debug" ]
        applicationIdSuffix ".debug"
    }
}
productFlavors {
    one {
        manifestPlaceholders = [ appNameFlavor: "One" ]
    }
    two {
        manifestPlaceholders = [ appNameFlavor: "Two" ]
    }
}
// AndroidManifest.xml
<application android:label="${appNameBase} ${appNameFlavor}${appNameSuffix}" ...>

In my case I wanted for prod just "My App One" and "My App Two" ad for debug "My App One Debug" and "My App Two Debug" and code above works great.