4

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:

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Does not work.Invalid resource reference - { is not a valid resource name character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.