4

I'm creating different flavors using gradle for 2 small android apps ,i wanna just know if i can edit app name on the xml file in the build.gradle , for my different flavors .

wissem46
  • 393
  • 3
  • 5
  • 14
  • I have answered [here](https://stackoverflow.com/questions/24785270/how-to-change-app-name-per-gradle-build-type/63449478#63449478), you can go through my answer, it might help you. – Amir Raza Aug 17 '20 at 11:01

5 Answers5

15

What do you mean by app name? the application package name in the manifest or the application name as it appears in the launcher?

If the former, do:

android {
  productFlavors {
    flavor1 {
      packageName 'com.example.flavor1'
    }
    flavor2 {
      packageName 'com.example.flavor2'
    }
  }
}

It's possible to override the app name as well but you'd have to provide a flavor overlay resource instead.

So create the following files:

  • src/flavor1/res/values/strings.xml
  • src/flavor2/res/values/strings.xml

And in them just override the string resource that contains your app name (the one that your manifest use for the main activity label through something like @string/app_name). You can also provide different translations as needed.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • exactly , i'm looking for a solution to override app name , i used copy method , as a result i have a new folder containing a new string.xml modified , containing the new app name ! the problem is how can i avoid this creation of that new file. i like just to replace it. – wissem46 Mar 24 '14 at 16:17
  • This is the best way to do it. Hacking around to edit the file in place is not a good idea as it'll break detection of incremental builds. – Xavier Ducrohet Mar 24 '14 at 16:34
  • find app/src/freefacebook/res/values/strings.xml -type f -exec perl -pi -w -e 's/My_Application/free_facebook/g' {} \; – wissem46 Mar 25 '14 at 09:55
  • 1
    Update: the Android gradle plugin now can define a resource like: resValue ,, to provide the string from your gradle script. – Hassan Ibraheem Aug 20 '14 at 09:09
  • @HassanIbraheem document reference please? – Prakash Nadar Sep 08 '14 at 05:28
  • @PrakashNadar you can find it on this page: http://tools.android.com/tech-docs/new-build-system under release "0.8.1" – Hassan Ibraheem Sep 08 '14 at 09:39
11

You can use resValue, eg.

debug {
    resValue 'string', 'app_name', '"MyApp (Debug)"'`
}
release {
    resValue 'string', 'app_name', '"MyApp"'
}

Make sure your AndroidManifest uses android:label="@string/app_name" for the application, and remove app_name from strings.xml as it will conflict with gradle's generated strings.xml when it tries to merge them.

Tom
  • 6,946
  • 2
  • 47
  • 63
  • 1
    This is actually a very good answer. I don't know why people are voting up based answers based on manually creating contents of `strings.xml`, but resValue should be the right way to go since you can put the actual values in `gradle.properties`. – superarts.org Nov 30 '15 at 04:13
  • In my case, I hade to add parentheses after resValue (I used resValue as a function). – clemsciences Apr 08 '22 at 14:33
4

Actually... For a more definitive explanation;

In main build.gradle :

ext {
    APP_NAME = "My Fabulous App"
    APP_NAME_DEBUG = "My Fabulous App debug"
}

In app build.gradle :

android {
    buildTypes {
        debug {
            manifestPlaceholders = [appName: APP_NAME_DEBUG]
        }
        release {
            manifestPlaceholders = [appName: APP_NAME]
        }
    }
}

so in AndroidManifest.xml

<application
    ...
    android:label="${appName}"
    >

is possible. Voilà! you have different application names for release and debug.

RaGe
  • 22,696
  • 11
  • 72
  • 104
ergunkocak
  • 3,334
  • 1
  • 32
  • 31
  • 1
    In my opinion, this is the best approach, because it allows to have a localized app name for release builds (`manifestPlaceholders = [appName: "@string/app_name"]`), and a non localized one for debug builds. – mtotschnig Mar 19 '16 at 18:26
0

This answer is based on Tom's, it works the best and you can work with gradle.properties to allow further animation in build process.

In build.gradle:

debug {
  resValue 'string', 'app_name', APP_NAME
}

In gradle.properties:

APP_NAME="Template 1"
MasterAM
  • 16,283
  • 6
  • 45
  • 66
superarts.org
  • 7,009
  • 1
  • 58
  • 44
0

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.myapp"> 


<application
    tools:replace="android:label"
    android:label="${appName}"
    android:theme="@style/AppTheme">

 <activity
    android:name=".MainActivity"      
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:label="${appName}"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

Gradle

buildTypes {
    release {
         minifyEnabled enableProguardInReleaseBuilds
         proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
         signingConfig signingConfigs.release
//       manifestPlaceholders = [appName: appConfig.appName]
         manifestPlaceholders = [appName: "Your app Name"]

    }

    debug {
         signingConfig signingConfigs.release
//       manifestPlaceholders = [appName: appConfig.appName]
         manifestPlaceholders = [appName: "Your app Name"] 

    }
}
Tuan Nguyen
  • 2,542
  • 19
  • 29