Below are the buildTypes and flavors parts of my build.gradle:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
}
}
flavorDimensions "default"
productFlavors {
free {
android.sourceSets.free.setRoot('src/main')
dimension "default"
}
plus {
applicationIdSuffix '.plus'
versionName '1.0'
android.sourceSets.plus.setRoot('src/plus')
dimension "default"
}
}
Android Studio only shows two build variants (freeDebug and freeRelease) in the Build variants window. It does not show plusDebug or plusRelease. I have another project with a similar build.gradle and I can clearly see four build variants. Any ideas where I should look?
plus is a default method in groovy. It's not a bug in Android Studio or anything else. You are executing this function in DefaultGroovyMethods
public static <T> Set<T> plus(Set<T> left, T right) {
return (Set)plus((Collection)left, (Object)right);
}
This is because the delegate passed into productFlavors implements Set.
See productFlavors definition
This appears to be a bug/limitation in Gradle. I have filed an issue for it.
Use something else, other than plus. I tried quoting it ("plus"), thinking that perhaps it's a conflict with a keyword, but that had no effect. But Plus and plussss and phat all work.
Related
I modified the build.gradle file for the Teapot example for the AGDK so that it has a debug build type as well as product flavors. My modifications are below.
buildTypes {
debug {
debuggable true
jniDebuggable true
minifyEnabled false
renderscriptDebuggable true
}
}
flavorDimensions "Dimension"
productFlavors {
development {
dimension "Dimension"
applicationIdSuffix ".dev"
}
production {
dimension "Dimension"
}
}
When I do this, the APK builds but Visual Studio can't find it to deploy to device for debugging. I get this error dialog.
What can I do to make this work?
My Android project has multiple build type and productFlavors
flavorDimensions "default"
productFlavors {
favor1 {
applicationId "com.abc.android"
versionCode 1
versionName "1"
}
flavor2 {
applicationId "com.abc.android"
versionCode 1
versionName "2"
}
}
buildTypes {
staging {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
develop {
applicationIdSuffix ".develop"
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
I have placed my file, say(Abc.java) under each of flavor1staging, flavor1develop, flavor2staging, flavor2develop directory
I can do ./gradlew assemblefalor1staging on my local Android studio and it works fine but when run using travis it cannot map the file and gives me error Unresolved reference Abd
Similarly for any string resources that are defined in the flavorbuild folders but not in the main folder
I have a question does the build is working fine if you give it only 1 build type?
Because according to Travis-CI they do not mention about supporting multiple build types in this link
https://docs.travis-ci.com/user/languages/android/
As all builds in the example containing only one build as you can see in these examples
https://github.com/andrewhr/rxjava-android-example/blob/master/app/build.gradle
https://github.com/pestrada/android-tdd-playground/blob/master/app/build.gradle
Please follow these projects if the project working fine after you make build.gradle as it is shown in example then add multiple builds if then gives issues need to check also log.trace of android & maybe not supported.
make it simple then complex the solution so that you could find where is the error.
I wanted to create 2 different versions of the app on the same tablet, the code is identical just a different package name, in the build.gradle file I wrote this code:
QA {
applicationIdSuffix ".qa"
debuggable true
signingConfig signingConfigs.debug
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
now when I open the app, on some screens, it gives me a pop up to choose which package name I want to use. How can I get rid of this pop up message ? What caused it ?
I was testing on a nexus 10 tablet.
You can easily change package name in Android Studio.
you should leave the buildTypes unchanged and create productFlavors instead if you are just changing the applicationId
productFlavors {
QA {
applicationIdSuffix ".qa"
}
Dev {
applicationIdSuffix ".dev"
}
...
...
}
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
I was wondering if it is possible to provide a different API Host per build using gradle. Ideally I would like to access the constant through my code the same so when I do a gradle build, it builds the release.apk to point to http://example.com and the debug.apk to point to http://debug.example.com.
I have achieved this using the following:
buildTypes {
debug {
buildConfig "public final static String API_HOST = \"http://debug.example.com\";"
}
release {
buildConfig "public final static String API_HOST = \"https://example.com\";"
}
}
However that seems pretty dirty
Cheers
I think a better alternative with today's Gradle features is to specify both productFlavors and buildTypes (example below).
The buildTypes control what certificate I sign with, and whether Proguard is run.
The productFlavors control the intended environment, which includes custom resources, as well as a different package name so I can install them both side by side.
Then I configure my server address in strings.xml for each variant and load it at runtime.
src/dev/res/values/strings.xml
src/staging/res/values/strings.xml
strings.xml example from the "dev" variant:
<string name="config_url">http://com.example.debug</string>
build.gradle snippet:
productFlavors {
dev {
packageName "com.example.dev"
}
staging {
packageName "com.example.staging"
}
}
buildTypes {
debug {
versionNameSuffix " debug"
signingConfig signingConfigs.debug
}
release {
// A release build runs Proguard, and signs with a release certificate
zipAlign true
runProguard true
proguardFile 'proguard-project.txt'
proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
signingConfig signingConfigs.release
}
}
An alternative is to use buildConfigField:
buildTypes {
debug {
buildConfigField 'String', 'API_HOST', '"https://debug.example.com"'
}
release {
buildConfigField 'String', 'API_HOST', '"https://example.com"'
}
}
You would then refer to it in your code via BuildConfig.API_HOST
So I ended up speaking to one of the gradleware engineers about this.... My initial solution is the correct way. Google/Gradle will be improving this in the future.
To add multiple values you separate the the strings with comas.