Janusz
Janusz

Reputation: 189594

Why are all my Gradle product flavors building?

I have two product flavors in an android project that is build with gradle.

One of the flavors declares an extra dependency but actually the dependency is used in both flavors. Both flavors build, since one of the flavors depends on a library only declared for the first flavor that should not be the case.

Since one of the flavors is the pro version that in the end should not have the admob SDK in the apk I now fear that for some reason both flavors add the admob SDK.

I have the following build.gradle file:

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 {
                compile files('src/Free/libs/admob.jar')
            }
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile fileTree(dir: 'libs', include: '*.jar')
}

Upvotes: 2

Views: 1313

Answers (1)

Janusz
Janusz

Reputation: 189594

The correct way to add a dependency for only one product flavor is:

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 {
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile fileTree(dir: 'libs', include: '*.jar')
    freeCompile files('src/Free/libs/admob.jar')
}

Upvotes: 5

Related Questions