3

Precursor: I want to localize the App name(using string resources)
and at the same time add build type suffix("DEBUG") from gradle file.

Here I am Trying to concatenate string resource "app_name" and gradle variable "AppNameSuffix"

Expected app name :
for product flavor "all" - "My application DEBUG"
for product flavor "china" - "My Chinese application DEBUG"
and subsequent build type suffixed for "RELEASE" and "CANARY"


build.gradle:

buildTypes {
    debug {
        manifestPlaceholders = [AppNameSuffix: "DEBUG"]
        ...
    }
    release{
        manifestPlaceholders = [AppNameSuffix: "RELEASE"]
        ...
    }
    canary{
        manifestPlaceholders = [AppNameSuffix: "CANARY"]
    }
}
productFlavors {
    china {
        applicationId "com.myapplication.china"
    }
    all {
        //default
        applicationId "com.myapplication.all"
    }
}

Manifest :

<application
    ...
    android:label="@{@string/app_name(${AppNameSuffix})}">
    ...
</application>

error at "$" symbol while evaluating gradle variable


main/res/string.xml :

<string name="app_name">My application %s</string>

china/res/string.xml :

<string name="app_name">My Chinese application %s</string>

References:

Anup
  • 4,024
  • 1
  • 18
  • 27

1 Answers1

0

In your gradle file, use lowercase so that the resource name is valid:

buildTypes {
    debug {
        manifestPlaceholders = [AppNameSuffix: "_debug"]
        ...
    }
    release{
        manifestPlaceholders = [AppNameSuffix: "_release"]
        ...
    }
    canary{
        manifestPlaceholders = [AppNameSuffix: "_canary"]
    }
}

Modify your manifest: there are parenthesis to remove, and maybe the tools:replace to add if not already there:

<application
    ...
    android:label="@string/app_name${AppNameSuffix}"
    tools:replace="android:label"
    ...
</application>

And then you need to add string resources matching your suffix:

<string name="app_name">My application</string>
<string name="app_name_debug">My application DEBUG</string>
<string name="app_name_release">My application RELEASE</string>
<string name="app_name_canary">My application CANARY</string>

You could also play with flavors and put specific strings.xml file in the folders matching your flavors.

Arnaud SmartFun
  • 1,573
  • 16
  • 21