ChatGPT解决这个技术问题 Extra ChatGPT

How to define different dependencies for different product flavors

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?


S
Sébastien

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

I'm trying this and getting this error, Error:(28, 0) Build script error, unsupported Gradle DSL method found: 'glassCompile()'! (The flavor I'm trying to add a dependency to is "glass".)
Ahh, looks like you have to move the dependencies block below the android block. How intuitive.
Note that the xxxCompile closures Gradle generates will not work if the flavor name starts with a capital letter.
"packageName" should be "applicationId"
How do you handle not calling the Ad classes?
u
user1506104

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


Do you know how to do it with buildVariants instead? like proDebugImplementation.
please add a separate question for this. link it here in the comment section. please add complete details of what you are trying to achieve. :)
n
nnyerges

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'

A
Arkadiusz Konior

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

How to go on from there? How to activate the different configurations? Does this apply to the Android build only? Is it possible to that with non-Android builds?
J
Jordy

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

that does not allow you to build multiple flavours at once, which is kind of a deal breaker for most of us. Furthermore it might not work well with android studio too
this is in the dependencies block?
Is this block in dependecy ?
hmm... doesn't scale well with 10 or 50 flavors does it?
@Ewoks I hava to make lots of flavors, do you have any better idea?
M
Miloš Černilovský

If you use Gradle with Kotlin, dependencies for custom flavors can be added as follows:

dependencies {
    "freeImplementation"("com.google.android.gms:play-services-ads:7.5.0")
}

It works but why is it a valid answer? Any source?

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now