How to use flavors with different app names in And

2020-02-17 08:49发布

In my app-level build.gradle I have the following flavors:

productFlavors {
    originalFlavour{
    }

    freeFlavour{
    }
}

The thing is building both flavors I get the same App Name. Instead I would like to have different app names for each flavors. Just adding a suffix would be useful. Hope someone could help me.

Thanks in advance

EDIT:

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="z.fncapps.etsiipod"  >

    <uses-permission
        android:name="android.permission.CALL_PHONE"
        android:required="false" />

    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- actvities declared here -->
    </application>
</manifest>

app/src/freeFlavour/AndroidManifest.xml

<uses-permission 
    android:name="android.permission.CALL_PHONE"
    android:required="false" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-feature
   android:name="android.hardware.telephony"
   android:required="false" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <!-- actvities declared here -->
</application>

3条回答
看我几分像从前
2楼-- · 2020-02-17 09:21

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"
查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-17 09:28

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.

查看更多
我命由我不由天
4楼-- · 2020-02-17 09:32

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:

../app/src/

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>
查看更多
登录 后发表回答