如何定义不同的依赖于不同的产品口味不同、口味、定义、依赖于

2023-09-12 08:38:53 作者:梦的方向叫做闯

我将我的应用程序,以摇篮之一,并想用新建立的风味特性有付费和免费的基于广告的味道。

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.

我只希望基于广告的版本依赖于AdMob的SDK。

I want only the ad based version to depend on the admob SDK.

我的build文件看起来是这样的:

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

有没有一种方法来配置依赖于免费的产品味道都被合并,它包含两种口味一般库主libs文件夹自身的libs文件夹?

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?

推荐答案

要定义一个味道特定的依赖,你可以使用ProCompile,而不是编译你的依赖部分。当您运行摇篮属性你自动创建的配置概要。

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.

正确的build文件看起来是这样的:

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