How to define different dependencies for different product flavors

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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?

回答1:

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' } 


回答2:

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' } 


回答3:

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.+'     } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!