News‎ > ‎

Android gradle build with free and paid flavors

posted 23 Oct 2013, 12:57 by Simon Šander   [ updated 23 Oct 2013, 23:30 ]

Problem

My goal was to have two different flavors for my Android application build. One for free and one for paid version. Paid version would be published to slide.me and Amazon app store.

I'm new to gradle build system, so my first attempt was quite good but not perfect. I have managed to get two different flavors but the problem was that advertising libraries that should be linked only to the free version, were linked to the paid version too.

Amazon had no problems with that and they accepted both version, but guys at slide.me were a lot more demanding. They run analysis of apk that I have send them and they discovered traces of advertising libraries. With slide.me it's not enough just to remove ads from app, they demand that there is no traces of advertising libraries inside apk.

Solution

After a lot of Goggling I have finally came up with solution that works great. I will not go into details because final gradle build file will probably help you more than thousand words.

Here it is. Gradle build file with many flavors and with custom dependencies for each of them. I think It can be a great start for your own Android project.

Project setup



gradle.build

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
    mavenLocal()
}

android {
    compileSdkVersion 18

    buildToolsVersion "18.0.1"

    defaultConfig {
        versionCode 26
        versionName "2.0"
        minSdkVersion 9
        targetSdkVersion 18
        packageName 'com.example.myexample'
    }

    signingConfigs {
        debug {
            storeFile file("path to your debug store")
        }

        release {
            storeFile file("path to your release store")
            storePassword ""
            keyAlias ""
            keyPassword ""
        }
    }

    buildTypes {
        debug {
            packageNameSuffix '.debug'
            versionNameSuffix '-DEBUG'
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
        }
    }

    flavorGroups 'market', 'version'

    productFlavors {
        google {
            flavorGroup 'market'
        }
        amazon {
            flavorGroup 'market'
        }
        slideme {
            flavorGroup 'market'
        }
        free {
            flavorGroup "version"
        }
        paid {
            flavorGroup "version"
            packageName = android.defaultConfig.packageName + ".paid"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
        amazon {
            assets.srcDirs = ['src/amazon/assets']
            res.srcDirs = ['src/amazon/res']
            java.srcDirs = ['src/amazon/java']
        }
        slideme {
            assets.srcDirs = ['src/slideme/assets']
            res.srcDirs = ['src/slideme/res']
            java.srcDirs = ['src/slideme/java']
        }
        free {
            manifest.srcFile 'src/free/AndroidManifest.xml'
            assets.srcDirs = ['src/free/assets']
            res.srcDirs = ['src/free/res']
            java.srcDirs = ['src/free/java']
        }
        paid {
            assets.srcDirs = ['src/paid/assets']
            res.srcDirs = ['src/paid/res']
            java.srcDirs = ['src/paid/java']
        }
    }
}

dependencies {
    compile fileTree(dir: 'src/main/libs', include: '*.jar')
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.android.support:appcompat-v7:18.0.+'    
    freeCompile fileTree(dir: 'src/free/libs', include: '*.jar')
}
Comments