Set value in manifestPlaceholders to applicationId - android

I am trying to configure some SDK inside application and I am facing this gradle problem, that just gives me a headache. What I have to do is set below value
manifestPlaceholders = [manifestApplicationId: "$applicationId"]
to be something like com.flavorA.debug for debug builds and honestly I have no idea how am I supposed to set this. If I put this section to defaultConfig then I get null inside my manifest file. If I put it to productFlavors section then I only get com.flavorA, so I'm missing buildType suffix.
I was also trying different things like
applicationVariants.each{ variant->
manifestPlaceholders = [manifestApplicationId: "test"]
}
but above doesn't even change the name to test, not mentioning other things.
How am I supposed to solve this with gradle?

You don't need manifestPlaceHolders or the applicationVariants block to achieve this. The applicationId and the package name in the manifest is decoupled, so you simply write:
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}
and if you want to change it for the build type:
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
More details here:
applicationid-vs-packagename

Related

How do you install the dev version and release version of your android app? [duplicate]

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).
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:
If you need different package names to be able to have both debug and release apks installed on a device, then use the build type approach below, as Gradle plugin docs agree. In this case flavours are an overkill. (I think all projects should by default do this, as it will make life easier especially after you've published to the store and are developing new features.)
There are valid uses for product flavours, the typical example being an app with free and paid versions. In such case, check Ethan's answer and read the documentation too: Configuring Gradle Builds and Gradle Plugin User Guide.
(You can also combine the two approaches, which results in every build variant having distinct package name.)
Build type configuration
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:
This example is straight from Xavier Ducrohet's "Google I/O 2013: The New Android SDK Build System" presentation.
Build Types in the User Guide.
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.
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
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.

How to handle AndroidManifest file for different environments

We have three environments for our app - DEV, QA and PROD.
Now we have created three AndroidManifest files for those three envs(like AndroidManifest_QA.xml), and we have to change the file name EVERY TIME when we want to release a new version, which is not unfailing.
So, I am wondering if there is an easier way to handle this situation?
Thanks in advance.
I would go for Gradle Flavours:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
or you can make it more separated, by using build variants. I think this is what you are really looking for in your case:
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
a very good tutorial:
http://www.techotopia.com/index.php/An_Android_Studio_Gradle_Build_Variants_Example
article:
http://developer.android.com/tools/building/configuring-gradle.html
Let's consider the following example from the techotopia.com
productFlavors {
phone {
applicationId
"com.ebookfrenzy.buildexample.app.phone"
versionName "1.0-phone"
}
tablet {
applicationId
"com.ebookfrenzy.buildexample.app.tablet"
versionName "1.0-tablet"
}
}
if you need to separate the falvours on code level, than please see Xavier Ducrohet stackoverflow link below:
Using Build Flavors - Structuring source folders and build.gradle correctly

Android Flavor ACTION_CHANGE_LIVE_WALLPAPER

Hi I'm trying to implement android app flavor (free and full) to live wallpaper. In eclipse, I used to use this following code to open live wallpaper preview from my own android Activity:
Intent intent = new Intent();
intent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = WallpaperService.class.getPackage()
.getName();
String cls = WallpaperService.class.getCanonicalName();
intent.putExtra(
WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(pkg, cls));
But now it does not work correctly as the free and full flavor are using same package name with just different applicationId in android studio. The problem is when it starts either in free or full version, it will goto full version no matter how, regardless of what flavor it is. I specify app flavor using applicationId in project gradle like this:
productFlavors {
free {
applicationId "com.kkl.app.free"
}
full {
applicationId "com.kkl.app"
}
}
How do we make it to get the correct package name that matches the app flavor?
You can call getPackageName() in your Activity to get Android packageName. This will be packageName from manifest file i.e. the one equal to current applicationId.
Method documentation can be found here.
Fixed by using the following:
intent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = getPackageName();
String cls = WallpaperService.class.getCanonicalName();
intent.putExtra(
WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(pkg, cls));
Special thank to Lingviston
You can do a lot with flavors, but what you are trying to do is far simpler than anyone has answered.
First you have a build variant to select your flavor for debugging and running. So use this, otherwise all your debugging will use default main release.
Secondly, you don't have to get package name, just use a build config flag or check flavor. I.E.
android {
signingConfigs {
releaseA35Demo {
storeFile file("$projectDir/../yaskeystore.jks")
storePassword System.getenv('YOUR_APP_STUDIO_STORE_PASSWORD')
keyAlias System.getenv('YOUR_APP_STUDIO_KEY_ALIAS')
keyPassword System.getenv('YOUR_APP_STUDIO_KEY_PASSWORD')
}
}
flavorDimensions 'default'
productFlavors {
a35Demo {
dimension 'default'
applicationId "com.appstudio35.yourappstudio"
buildConfigField "String", "SERVER_URL", '"http://fakeNumbers.compute-1.amazonaws.com:3006"'
buildConfigField "int", "BUSINESS_ID", "1"
versionCode 1
versionName "0.01.01-b1"
minSdkVersion 21
}
a35DemoDev {
dimension 'default'
applicationId "com.appstudio35.yourappstudio.dev"
buildConfigField "String", "SERVER_URL", '"http://fakeNumbers2.compute-1.amazonaws.com:3006"'
buildConfigField "int", "BUSINESS_ID", "2"
versionCode 1
versionName "0.01.01-b1"
minSdkVersion 21
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
productFlavors.a35Demo.signingConfig signingConfigs.releaseA35Demo
productFlavors.a35DemoDev.signingConfig signingConfigs.releaseA35Demo
}
}
}
Then simply reference it in code like:
BuildConfig.BUSINESS_ID
Wherever you need it. Just make sure you don't accidentally use the BuildConfig of a library project when it auto imports the BuildConfig.
Next way is if you want to check your flavor you can simply do
BuildConfig.FLAVOR to see which one you are on. However, keep in mind there are some compiler warnings about using it because you are checking against a flavor and the BuildConfig assumes it will ALWAYS be whatever you are currently in for the Build Variant dropdown, Which is not true, you can ignore this always true or always false warning, I assure you it works.
Lastly your package issue is just because you are debugging the wrong build variant. I'll add an image so you can see where to change that.
Hope that helps.

Create separate apk for separate flavor in android

I have used build.gradle(app) to create different flavors of apk.
But installing different flavors of same apk overrides the previous one.
I want to create different apks to run on same device simultaneously.
I want to create different apk with different appicon which can be installed on same device and run simultaneously.
Any link or tutorial or direct help is appreciated.
Thanks in advance.
Change the PackageName of the flavor
Sample Gradle File
apply plugin: 'com.android.application'
android {
lintOptions {
abortOnError false
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
buildTypes {
debug {
minifyEnabled false
zipAlignEnabled true
}
release {
minifyEnabled true
zipAlignEnabled true
}
}
productFlavors {
Flavor1 {
applicationId "com.falvor.one" //This is where you change the package name
}
Flavor2 {
applicationId "com.falvor.two"
}
}
}
Flavor Hierarchy in Android
- src/main/java
- src/flavor1
--------------Java
----------------Your java files
--------------res
----------------Drawable
src/flavor2/java
For more understanding, follow this link
You need to create new productFlavors in your gradle file, like this;
productFlavors {
Flavor1 {
applicationId 'com.project.fl1'
signingConfig signingConfigs.xx
versionCode 1
}
Flavor2 {
applicationId 'com.project.fl2'
signingConfig signingConfigs.xx
versionCode 1
}
Flavor3 {
applicationId 'com.project.fl3'
signingConfig signingConfigs.xx
versionCode 1
}
}
The important thing here is to give each one a unique applicationId, they can then be installed on the same phone.
This post explains exactly how to achieve what you want step by step.
Most importantly:
add the product flavours container to the app build.gradle file
productFlavors {
free {
applicationId "antoniocappiello.com.buildvariantsexample.free"
}
paid {
applicationId "antoniocappiello.com.buildvariantsexample.paid"
}
}
create inside src a directory with the exact name of the product flavour that you want to look different from the main variant, for example with the configuration at step 1 the directory name could be paid or free . And inside that directory create the subfolder res/drawable where you are going to place your new app launcher icon.
Directory structure example

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

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).
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:
If you need different package names to be able to have both debug and release apks installed on a device, then use the build type approach below, as Gradle plugin docs agree. In this case flavours are an overkill. (I think all projects should by default do this, as it will make life easier especially after you've published to the store and are developing new features.)
There are valid uses for product flavours, the typical example being an app with free and paid versions. In such case, check Ethan's answer and read the documentation too: Configuring Gradle Builds and Gradle Plugin User Guide.
(You can also combine the two approaches, which results in every build variant having distinct package name.)
Build type configuration
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:
This example is straight from Xavier Ducrohet's "Google I/O 2013: The New Android SDK Build System" presentation.
Build Types in the User Guide.
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.
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
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.

Categories

Resources