Jeff George
Jeff George

Reputation: 43

How to setup gradle in Android Studio to provide two flavors with different minimum SDK versions?

Problem: I want two product flavors, a FREE version with ads and a PRO version without ads. Ads require Google Play with a min SDK of 9 so I set that for FREE but I want my pro version to have a min SDK of 8.

The build of the FREE version works but the build of PRO does not.

I am using (stable) Android Studio 1.1.0.

I setup a new project with a blank activity (Hello World example). I then modified the build.gradle file (below) to include the two flavors and FREE-specific compile dependency and then modified the file structure to move the activity's java and layout xml files into the flavor structures. Thus, the project has the following file structure:

 
app\src\
  free\
    java\com\sample\adexample\MainActivity.java  - This is Hello World.
    res\layout\activity_main.xml - This is the Hello World layout.
    res\values\strings.xml  - Unique Hello World string for free version.
    res\AndroidManifest.xml - This is a copy of the manifest in main.
  main\
    java\com\sample\adexample\ - empty
    res\layout\ - empty
    res\AndroidManifest.xml - This is the Hello World manifest.
  pro\
    java\com\sample\adexample\MainActivity.java  - This is Hello World.
    res\layout\activity_main.xml - This is the Hello World layout.
    res\values\strings.xml  - Unique Hello World string for free version.
    res\AndroidManifest.xml - This is a copy of the manifest in main.

Here is my build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.sample.adexample"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        pro {
            applicationId "com.sample.adexample.pro"
            minSdkVersion 8
            versionName "1.0-Pro"
        }
        free {
            applicationId "com.sample.adexample.free"
            minSdkVersion 9
            versionName "1.0-Free"
            dependencies {
                compile 'com.google.android.gms:play-services:6.+'
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

Building the freeDebug flavor works fine. But I get the following error when building proDebug:

    :app:processProDebugManifest
C:\Users\Jeff\AndroidStudioProjects\AdExample\app\src\main\AndroidManifest.xml:0:0 Error:
    uses-sdk:minSdkVersion 8 cannot be smaller than version 9 declared in library C:\Users\Jeff\AndroidStudioProjects\AdExample\app\build\intermediates\exploded-aar\com.google.android.gms\play-services\6.5.87\AndroidManifest.xml
    Suggestion: use tools:overrideLibrary="com.google.android.gms" to force usage

Is there a way to accomplish the requirements?

Thanks for your time.

Upvotes: 4

Views: 1062

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

Remove the dependencies closure from free. Move that compile 'com.google.android.gms:play-services:6.+' into your existing dependencies closure and make it be freeCompile rather than compile.

A prefix on the compile statement is how you make dependencies be tied to build variants rather than be used all the time the way compile is. So, a debugCompile would declare a dependency for only the debug build type, and freeCompile would declare a dependency for only the free product flavor.

AFAIK, this should work for multiple flavor dimensions, so if you had a dependency that was only relevant for the bird flavor (on one dimension) and the free flavor (on another dimension), you could use freeBirdCompile 'com.skynyrd.lynyrd:raised-lighter:1.0.0' to pull that in.

Upvotes: 4

Related Questions