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>

fapps
  • 1,799
  • 3
  • 15
  • 18

3 Answers3

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://stackoverflow.com/a/13200774/2911458 for more details on this distinction.

Community
  • 1
  • 1
stkent
  • 19,772
  • 14
  • 85
  • 111
  • 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?) ? – android developer May 16 '15 at 17:02
  • 3
    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](http://tools.android.com/tech-docs/new-build-system): "You can now use resValue ,, on build types and product flavors the same way you can use buildConfigField." – stkent May 16 '15 at 17:07
  • 1
    As for your second question; I'm not sure. I tried looking through the [Android Plugin DSL docs](https://developer.android.com/tools/building/plugin-for-gradle.html), 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! – stkent May 16 '15 at 17:11
  • 2
    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) ? – android developer May 16 '15 at 17:13
  • I also don't get how to point to resources that don't exist. Or do they become exist when I compile? – android developer May 16 '15 at 17:16
  • 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). – stkent May 16 '15 at 17:19
  • 1
    R.e. translation - that's a good one, not sure - probably worth a separate question. – stkent May 16 '15 at 17:19
  • ok, thank you for trying to help. I've created a new question here: http://stackoverflow.com/q/30279304/878126 – android developer May 16 '15 at 18:44
  • the answer posted by @crubio is much better in my opinion, if you want to cater for localization – Bruce Aug 29 '16 at 02:27
  • 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. – Hermandroid Dec 30 '16 at 20:18
  • @Hermandroid omg, thanks buddy. I spent hours trying to figure this out... So glad I found this! – Chris Mar 09 '23 at 17:41
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>
crubio
  • 6,394
  • 4
  • 30
  • 33
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"
jk7
  • 1,958
  • 1
  • 22
  • 31