Reputation: 189444
I am converting one of my apps to Gradle and would like to use the new build flavor features to have a paid and a free ad based flavor.
I want only the ad based version to depend on the admob SDK.
My build file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
productFlavors {
Pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
Free {
dependencies {
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile fileTree(dir: 'libs', include: '*.jar')
}
Is there a way to configure the dependency in the free product flavor to have its own libs folder that is merged with the main libs folder that contains general libraries for both flavors?
If this is possible how would I define this folder?
Upvotes: 205
Views: 69496
Reputation: 2028
This way will work in multi-module projects
create a build gradle file (for example build_flavor_config.gradle) and define your config in it like this
android {
flavorDimensions 'resource_type'
productFlavors {
create("flavor1") {
dimension 'resource_type'
versionName "$app_version_name - flavor1"
}
create("flavor2") {
dimension 'resource_type'
versionName "$app_version_name - flavor2"
}
}
}
and apply this gradle file for every module that you want for example app module or a feature module like this:
apply from: rootProject.file("build_flavor_config.gradle")
after sync project you can access to specific of implementation of each flavor like this:
flavor1Implementation("flavor1Library")
flavor2Implementation("flavor2Library")
Upvotes: 0
Reputation: 4112
If you use Gradle with Kotlin (build.gradle.kts), dependencies for custom flavors can be added as follows:
dependencies {
"freeImplementation"("com.google.android.gms:play-services-ads:7.5.0")
}
Upvotes: 13
Reputation: 583
Simple:
dependencies {
....
....
gradle.startParameter.getTaskNames().each { task ->
if(task.contains("free")) {
implementation 'com.google.android.gms:play-services-ads:17.2.0'
}
}
....
....
}
or just:
FreeImplementation 'com.google.android.gms:play-services-ads:17.2.0'
Upvotes: 17
Reputation: 7076
Fast forward to mid-2018. You will need to add flavorDimensions
.
android {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "dimensionName"
productFlavors {
pro {
dimension "dimensionName"
}
free {
dimension "dimensionName"
}
}
}
dependencies {
implementation 'com.android.support:support-v4:22.2.0'
freeImplementation 'com.google.android.gms:play-services-ads:15.0.1'
}
Also, take note:
Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Upvotes: 50
Reputation: 28653
To define a flavor specific dependency you can use proCompile
instead of compile
in your dependency section. When you run gradle properties you get an overview of automatic created configurations.
The correct build file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 22
}
productFlavors {
pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
free { }
}
}
dependencies {
compile 'com.android.support:support-v4:22.2.0'
freeCompile 'com.google.android.gms:play-services-ads:7.5.0'
}
Upvotes: 223
Reputation: 1804
Edit: I recommend using one of the other techniques!
An alternative to the accepted answer is this:
ext {
flavorType = ""
}
gradle.startParameter.getTaskNames().each { task ->
if(task.contains("flavor1")){
flavorType = "flavor1"
} else if (task.contains("flavor2")){
flavorType = "flavor2"
} else {
flavorType = "flavor3"
}
}
if(flavorType == 'flavor1' || flavorType == 'flavor2') {
compile 'com.android.support:support-v4:18.0.+'
}
Upvotes: 11
Reputation: 1159
You need to manually add configuration for each flavor. Example
configurations {
proCompile
freeCompile
}
dependencies {
compile 'com.parse.bolts:bolts-tasks:1.3.0'
proCompile 'com.android.support:design:23.1.1'
freeCompile 'com.parse:parse-android:1.12.0'
}
Upvotes: 14