Is it possible to change the package name of an Android application using Gradle?
I need to compile two copies of the same app, having a unique package name (so I can publish to the market twice).
Is it possible to change the package name of an Android application using Gradle?
I need to compile two copies of the same app, having a unique package name (so I can publish to the market twice).
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
packageName
was renamed applicationId
(and flavorGroups
=> flavorDimensions
) in Android Gradle plugin version 1.0. (Consider updating the answer.) –
Bowler packageName
is not the same as applicationId
, see tools.android.com/tech-docs/new-build-system/… –
Barometrograph As a simpler alternative to using product flavours as in Ethan's answer, you can also customise build types.
How to choose between the approaches:
(You can also combine the two approaches, which results in every build variant having distinct package name.)
For debug build type, and all other non-release types, define applicationIdSuffix
which will be added to the default package name.
(Prior to Android Gradle plugin version 0.11 this setting was known as packageNameSuffix
.)
android {
buildTypes {
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
}
beta {
applicationIdSuffix '.beta'
versionNameSuffix '-BETA'
// NB: If you want to use the default debug key for a (non-debug)
// build type, you need to specify it:
signingConfig signingConfigs.debug
}
release {
// signingConfig signingConfigs.release
// runProguard true
// ...
}
}
}
Above, debug
and release
are default build types whose some aspects are configured, while beta
is a completely custom build type. To build the different types, use assembleDebug
, assembleBeta
, etc, as usual.
Similarly, you can use versionNameSuffix
to override the default version name from AndroidManifest (which I find very useful!). E.g. "0.8" → "0.8-BETA", as configured above.
Resources:
Myself I've been using productFlavors
so far for this exact purpose, but it seems build type customisation may be closer to my needs, plus it keeps the build config simpler.
Update (2016): I've since used this approach in all my projects, and I think it definitely is the way to go. I also got it included in Android Best Practices guide by Futurice.
gradle assembleDebug
, it will only produce myapp-debug-unaligned.apk
. Building a custom type like beta
above, assembleBeta
will produce both myapp-beta-unaligned.apk
and myapp-beta.apk
(aligned i.e. optimised), unless you define zipAlign false
for that type. See "The possible properties and their default values" in User Guide. I assume it's usually fine to only use zipAlign only for actual release builds. –
Bowler applicationIdSuffix
you specifically change the application ID (package name), which means you certainly can have beta and debug installed at the same time. In addition, my answer shows how to customise version name (which you might for example show inside the app) using versionNameSuffix
. –
Bowler 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
gradle assembleFlavor1Debug
or gradle assembleFlavor2Release
. gradle tasks
is also helpful. –
Bowler packageName
was renamed applicationId
(and flavorGroups
=> flavorDimensions
) in Android Gradle plugin version 1.0. (Consider updating the answer.) –
Bowler packageName
is not the same as applicationId
, see tools.android.com/tech-docs/new-build-system/… –
Barometrograph With the gradle plugin version of 1.0.0+ you have to use applicationId as stated in the migration guide
Renamed Properties in ProductFlavors
packageName => applicationId
Thus in your build.gradle you would now use:
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
}
flavor2 {
applicationId "com.example.flavor2"
}
}
From Ethan's answer, both flavorGroups and packageName both are not available anymore. Below works as of March 2015.
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
flavorDimensions "flavor"
productFlavors {
flavor1 {
flavorDimension "flavor"
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
flavorDimension "flavor"
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}
I did not want to use Flavors, so I found a way to do so with buildTypes. I did this by changing my app/build.gradle
file as follows:
defaultConfig {
applicationId "com" // See buildTypes.type.applicationIdSuffix
...
}
...
buildTypes {
debug {
applicationIdSuffix ".domain.name.debug"
...
}
releaseStaging {
applicationIdSuffix ".compagny.staging"
...
}
release {
applicationIdSuffix ".domain.name"
...
}
}
This allows me to have 3 apps next to each other on my devices.
I hope this helps others.
© 2022 - 2024 — McMap. All rights reserved.
gradle assembleFlavor1Debug
orgradle assembleFlavor2Release
.gradle tasks
is also helpful. – Bowler