how to change android app name in the build gradle

2019-02-15 08:44发布

问题:

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 .

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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"


回答5:

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"] 

    }
}