How to Change App Name Per Gradle Build Type

How to change app name per Gradle build type

If by "app name", you mean android:label on <application>, the simplest solution is to have that point at a string resource (e.g., android:label="@string/app_name"), then have a different version of that string resource in a src/debug/ sourceset.

You can see that in this sample project, where I have a replacement for app_name in src/debug/res/values/strings.xml, which will be applied for debug builds. release builds will use the version of app_name in src/main/.

Gradle-only solution to modify App name based on Build Variant

The first try was a closer solution to the right answer than the updated code.

Further refactoring could possibly done by moving all the manifestPlaceholders code inside the applicationVariants.all section; however, this is a working copy of a semi-clean, gradle-only solution...

android {
ext {
APP_NAME = "App Name"
HUB_NAME = "Hub"
}
defaultConfig {
manifestPlaceholders = [ applicationLabel: APP_NAME ]
}
productFlavors {
entity_1 {
versionNameSuffix ' - Entity_1'
applicationIdSuffix 'entity_1'
}

...

entity_n {
versionNameSuffix ' - Entity_n'
applicationIdSuffix 'entity_n'
}

hub {
versionNameSuffix ' - Hub'
applicationIdSuffix 'hub'
manifestPlaceholders = [ applicationLabel: HUB_NAME ]
}
}

applicationVariants.all { variant ->
// Don't modify the release build or the hub flavor. They are good already.
if (variant.buildType.name == "release" || variant.flavorName == "hub") return
variant.mergedFlavor.manifestPlaceholders = [applicationLabel: APP_NAME + variant.mergedFlavor.versionNameSuffix]
}

Notes:

BEFORE the applicationVariants.all { ... } code runs, this is what all the applicationLabel look like. We are close, but need to ADD to them...

Flavor     Debug App Name             Release App Name
-------- -------------- ----------------
entity_1 App Name App Name
entity_2 App Name App Name
... ... ...
entity_n App Name App Name
hub Hub Hub

AFTER the applicationVariants.all { ... } code runs, this is what all the applicationLabel look like. We are done!

Flavor     Debug App Name             Release App Name
-------- -------------- ----------------
entity_1 App Name - Entity_1_name App Name
entity_2 App Name - Entity_2_name App Name
... ... ...
entity_n App Name - Entity_n_name App Name
hub Hub Hub

Also...

defaultConfig does not have a way of accessing information within the individual productFlavors. Although defaultConfig is a Flavor kind of, only the specified Flavors can read information from within the defaultConfig. There are no mechanism to go the other way (that I am aware of). So you need to set the most generic type in the defaultConfig

Any information within the buildTypes block will get the final say, and the code within applicationVariants.all will not override that. In order to overcome that, you have to remove the needed code from the buildType block and move it within the applicationVariants.all block (with the correct logic statements)

Different app names for different build flavors in Android?

Just add the string

<string name="app_name">app_name_string</string>

in the folders:

app/src/photographer_prod/res/values/strings.xml
app/src/agent_prod/res/values/strings.xml

You can also add resource values directly to your build.gradle file

productFlavors { 
photographer_prod {
resValue "string", "app_name", "the app name.."
}
agent_prod {
resValue "string", "app_name", "the app name.."
}
}

How to change apk name by using gradle like this format?

Update: Please check Anrimian's answer below which is much simpler and shorter.

Try this:

gradle.properties

applicationName = MyApp

build.gradle

android {
...
defaultConfig {
versionCode 111
...
}
buildTypes {
release {
...
applicationVariants.all { variant ->
renameAPK(variant, defaultConfig, 'R')
}
}
debug {
...
applicationVariants.all { variant ->
renameAPK(variant, defaultConfig, 'T')
}
}
}
}
def renameAPK(variant, defaultConfig, buildType) {
variant.outputs.each { output ->
def formattedDate = new Date().format('yyMMdd')

def file = output.packageApplication.outputFile
def fileName = applicationName + "_V" + defaultConfig.versionCode + "_" + formattedDate + "_" + buildType + ".apk"
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}

Reference:
https://stackoverflow.com/a/30332234/206292
https://stackoverflow.com/a/27104634/206292

Different app names for different build flavors?

Instead of changing your main strings.xml with a script and risk messing up your source control, why not rely on the standard merging behavior of the Android Gradle build?

My build.gradle contains

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}

release {
res.srcDir 'variants/release/res'
}

debug {
res.srcDir 'variants/debug/res'
}
}

So now I can define my app_name string in the variants/[release|debug]/res/strings.xml. And anything else I want to change, too!

In a Android gradle build, how to change the package name by build type?

A bit late on with the answer, but you could have a common part of the applicationID defined such as:

android { 
defaultConfig {
applicationId 'net.company.appname'
...
}
}

and have your flavours specify the applicationIdSuffix as in your example, so that'd give you the 'net.company.appname.debug' and 'net.company.appname.release' or whatever you say you want the suffix to be.

How to change the Android app package name when assembling with Gradle?

You could so something like this

android {
...

defaultConfig {
minSdkVersion 8
versionCode 10
}

flavorDimensions "flavor1", "flavor2"

productFlavors {
flavor1 {
applicationId "com.example.flavor1"
versionCode 20
}

flavor2 {
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}

You can also change the field android.defaultConfig.applicationId if you want to do one-off builds.

Taken from: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-Flavor-Configuration



Related Topics



Leave a reply



Submit