How to use flavors with different app names in Android studio?

筅森魡賤 提交于 2019-11-29 23:37:30
stkent

Remove the app_name string from your existing strings.xml file, then you can do this:

productFlavors {
    originalFlavour{
        resValue "string", "app_name", "Original Flavor Name"
    }

    freeFlavour{
        resValue "string", "app_name", "Free Flavor Name"
    }
}

Then the existing reference to @string/app_name in your manifest will refer to a generated string resource, which is based on the flavor selected.

Note that if you specify a label for your launch Activity (by defining the android:label xml tag in the <activity> element), that value will be used in many user-visible places rather than your application's label. To overcome this for your case, you can just remove the label from your <activity> element altogether. See https://stackoverflow.com/a/13200774/2911458 for more details on this distinction.

You could use a string resource in AndroidManifest.xml:

<application android:label="@string/app_name">

And then use different values resource files for each flavor in ../app/src/ folder:

The key of this solution is that you could create localized strings.xml files inside each values folder for each flavor: /res/values/, /res/values-pt/, /res/values-es/, etc.

Each string.xml could have app name localized, the default (/res/values/) for Free Flavor Name could be:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Free Flavor Name</string>
</resources>

Since you already have a separate manifest for the free flavor, why not assign its label to a different string resource value?

android:label="@string/free_app_name"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!