6

I have an android app and I want to change the app label for the debug and other buildTypes. I don´t have any flavors!

Here is the setup that I believe looks like it should work: Android Studio Setup

-src
   |-debug
      |-res
         |-values
            |-strings.xml
   |-main
      |-res
         |-values
            |-strings.xml
      |-java
      [...]

I have no custom sourcesets just a debug buildType:

buildTypes {
    debug {
        applicationIdSuffix ".debug"

    }
}

so I though

sourceSets.debug.res.srcDirs = ['src/debug/res'] 

would to the trick. But it doesn't. Any ideas?

How to change app name per Gradle build type does not work anymore...

Community
  • 1
  • 1
volkersfreunde
  • 1,095
  • 1
  • 12
  • 22

8 Answers8

24

I found another sweet solution to this, using manifest placeholders:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
    android:label="${applicationLabel}">
    <activity
        android:label="${applicationLabel}">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

and in your gradle file:

android {
    defaultConfig {
        manifestPlaceholders = [ applicationLabel:"@string/app_name"]
    }
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            manifestPlaceholders = [ applicationLabel:"MyApp Debug"]
        }
    }
}
volkersfreunde
  • 1,095
  • 1
  • 12
  • 22
  • 1
    for anyone who sees merge error in gradle build, simple add ````tools:replace="android:label" tools:node="replace"```` inside – Nicholas Ng Aug 08 '15 at 07:10
13
buildTypes {
    release {
        resValue 'string', 'APP_NAME', '"My App Release"'
    }
    debug {
        resValue 'string', 'APP_NAME', '"My App Debug"'
    }
}

value\strings.xml

< string name="app_name" >@string/APP_NAME< /string>

and use app_name everywhere

Paulo Abreu
  • 536
  • 4
  • 10
  • 1
    Thanks! This works great. However I use 'app_name' instead of 'APP_NAME' – Maris B. Aug 25 '17 at 11:59
  • 1
    Actually you do not need to create that resource in strings.xml. When you define that in `buildTypes`, that string resources already exists, you can directly reference it from layout xml as well. – Sira Lam May 27 '19 at 03:52
  • For me this _method_ worked `resValue('string', 'app_name', 'my app name')` instead of `resValue 'string' 'app_name' 'my app name'` – John Dec 19 '19 at 09:38
4

You have to use

   |-debug
      |-res
         |-values
            |-strings.xml

In your picture you have debug/res/strings.xml

Also you doens't need it (because it is the standard, but the issue isn't here).

sourceSets.debug.res.srcDirs = ['src/debug/res'] 
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
4

Forget the string.xml files. All can be easily configured in build.gradle.

First of all, you should maintain the string pointer "app_name" in AndroidManifest file, and delete all instances of "app_name" in string's resource files:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
    android:label="@string/app_name">
    <activity
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

Secondly, the resource value of @string/app_name is currently not defined. So we must apply its default definition in build.gradle:

defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 123423432
        versionName "1.0.0"
        resValue 'string', 'app_name', '"My app label"'
    }

Currently, app_name is defined for all build types. By assuming you want to change the app label for the buildTypes, each build type must be defined with a string value in the same build.gradle branch:

buildTypes {
    release {
        resValue 'string', 'app_name', '"My app label Release"'
    }
    debug {
        resValue 'string', 'app_name', '"My app label Debug"'
    }
}

Since this resource value is set programmatically, we also need to add a certain translations lint ignore in case of a Release build:

lintOptions { disable 'MissingTranslation' }

In case you want to change it accordingly with a set of defined Flavours(dev, qua or prd) add the resValues definitions in productFlavours instead of buildTypes:

productFlavors {
    dev {
        applicationId "com.example.myapp.dev"
        resValue 'string', 'app_name', '"My app label Dev"'
    }
    qua {
        applicationId "com.example.myapp.qua"
        resValue 'string', 'app_name', '"My app label Qua"'
    }
    prd {
        applicationId "com.example.myapp.prd"
        resValue 'string', 'app_name', '"My app label Prd"'
    }
}
ReVs
  • 57
  • 5
2

You can create a string in gradle that will be available in xml too:

buildTypes {
    debug {
        buildConfigField "String", "app_name", "AppDebug"
    }
    release {
        buildConfigField "String", "app_name", "AppRelease"
    }

And then use it in xml:

android:label="@string/app_name"

Just make sure app_name not specified in your strings.xml.

Simas
  • 43,548
  • 10
  • 88
  • 116
0

Try this.

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
    sourceSets.debug.resources.srcDirs = ['src/debug/res']
}
chanil
  • 351
  • 1
  • 11
0

Have you remembered to get rid of the directory listing inside your app's build.gradle?

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDir 'src'
//      res.srcDir 'res' <--- This line should be removed
        assets.srcDir 'assets'
Andy M
  • 309
  • 2
  • 9
0

I know this is quite an old question, but I've had the same problem and just solved it.

You should update these codes if you have one in your build.gradle for your app.

debug.setRoot('build-types/debug')
release.setRoot('build-types/release')

These codes automatically generate your AppName.iml and set default debug and release directory to /build-types/debug/res, which is different from src/debug/res.

Lee Han Kyeol
  • 2,371
  • 2
  • 29
  • 44