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

3

43

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>

Mauser answered 11/2, 2015 at 22:30 Comment(0)
P
83

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://mcmap.net/q/73894/-how-to-change-an-android-app-39-s-name for more details on this distinction.

Paramagnetic answered 11/2, 2015 at 22:36 Comment(11)
Is it also possible to set it for debug flavor vs release flavor? Also, where can I find more info about "resValue" ? For example, what kinds of values I can put there (styles are possible?) ?Gander
To answer your first question: yes, this works for build types (debug, release etc.) too. From the release notes for v0.8.1 of the gradle plugin: "You can now use resValue <type>,<name>,<value> on build types and product flavors the same way you can use buildConfigField."Paramagnetic
As for your second question; I'm not sure. I tried looking through the Android Plugin DSL docs, but could not find any mention of the resValue or buildConfigField methods in the relevant Types (ProductFlavor/GroupableProductFlavor, BuildType). It's possible the docs are not up-to-date. I would suggest some experiments to figure out if non-String values are accepted!Paramagnetic
Nice. I've now tested the "app_name" , but Android-Studio claims I already have this ("duplicate resources..." ). How do I avoid this? Is it because, as you wrote, I need to remove the "app_name" from the "strings.xml" file? How can I avoid this deletion (it's translated) ?Gander
I also don't get how to point to resources that don't exist. Or do they become exist when I compile?Gander
Syncing with your build.gradle file should regenerate the BuildConfig and resources files based on your buildConfigFields and resValues (and the build variant Android Studio is currently set to build).Paramagnetic
R.e. translation - that's a good one, not sure - probably worth a separate question.Paramagnetic
ok, thank you for trying to help. I've created a new question here: https://mcmap.net/q/390346/-how-to-use-build-types-debug-vs-release-to-set-different-styles-and-app-names/878126Gander
the answer posted by @crubio is much better in my opinion, if you want to cater for localizationTiaratibbetts
Thank you!!!. Please note that resValue lines MUST be at the beginning of each flavor declaration, I had an issue when I put the resValue at the end of the flavor.Extensive
@Extensive omg, thanks buddy. I spent hours trying to figure this out... So glad I found this!Vanny
K
27

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>
Kayseri answered 29/10, 2015 at 14:3 Comment(3)
this is a better approach in my opinion for localizationTiaratibbetts
Agreed if you're localizing the app name; I think it's a bit more unnecessary machinery and indirection otherwise.Paramagnetic
We can also use ManifestPlaceholder for it.Dyson
X
0

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"
Xe answered 9/3, 2017 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.