7

Build file that I'm using is below. I would like to achieve that my paid build contains no advertising libraries. I only include advertising libraries in free flavor. But they are present in all builds.

How should I change my build file to get ad libraries free paid version?

buildscript {
    repositories {
        mavenCentral()
    }

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

tasks.withType(Compile) {
    options.encoding = "UTF-8"
}

apply plugin: 'android'

repositories {
    mavenCentral()
    mavenLocal()
}

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

android {
    compileSdkVersion 18

    buildToolsVersion "18.0.1"

    defaultConfig {
        versionCode 25
        versionName "1.9.1"
        minSdkVersion 9
        targetSdkVersion 18
        packageName 'com.example.myproject'
    }

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

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

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

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

    flavorGroups 'market', 'version'

    productFlavors {
        google {
            flavorGroup 'market'
        }
        amazon {
            flavorGroup 'market'
        }
        slideme {
            flavorGroup 'market'
        }
        free {
            flavorGroup   "version"
            dependencies {
                compile fileTree(dir: 'src/free/libs', include: '*.jar')
            }
        }
        paid {
            flavorGroup   "version"
            packageName = android.defaultConfig.packageName + ".paid"
        }
    }

    android.sourceSets.amazon {
        assets.srcDirs = ['src/amazon/assets']
        res.srcDirs = ['src/amazon/res']
        java.srcDirs = ['src/amazon/java']
    }

    android.sourceSets.slideme {
        assets.srcDirs = ['src/slideme/assets']
        res.srcDirs = ['src/slideme/res']
        java.srcDirs = ['src/slideme/java']
    }

    android.sourceSets.free {
        manifest.srcFile 'src/free/AndroidManifest.xml'
        assets.srcDirs = ['src/free/assets']
        res.srcDirs = ['src/free/res']
        java.srcDirs = ['src/free/java']
    }

    android.sourceSets.paid {
        assets.srcDirs = ['src/paid/assets']
        res.srcDirs = ['src/paid/res']
        java.srcDirs = ['src/paid/java']
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
sajmons
  • 249
  • 2
  • 10

1 Answers1

9

After some research I think I have achieved what I wanted to. By the help from this https://stackoverflow.com/a/17246390/1463357 and this https://stackoverflow.com/a/18213259/1463357 post I was able to fix my gradle.build file.

There was two problems with my original build file. First problem was wrongly defined dependences for flavors. Dependencies must be defined after productFlavors, and they must be defined in form flavorNameCompile like this:

productFlavors {
    free {
    }
}

dependencies {
    compile fileTree(dir: 'src/main/libs', include: '*.jar')
    freeCompile fileTree(dir: 'src/free/libs', include: '*.jar')
}

Second problem was in the wrong sourceSets definition. To correctly define sourceSets for flavors, flavors must be defined before sourceSets. Like this:

productFlavors {
    free {                
    }
}

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

After applying those changes, apk sizes for different flavors were different as expected and no traces of advertising libraries are found in them.

Here is my final gradle.build file:

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

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

        release {
            storeFile file("your path")
            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')
}
Community
  • 1
  • 1
sajmons
  • 249
  • 2
  • 10
  • Hi , in similar way if we want to add some library compile line based on min android api level used for a particular flavour , can we do that ? – beginner Jul 24 '17 at 09:46
  • For reference, flavorGroups have been renamed to flavorDimensions. https://stackoverflow.com/a/28233651 – seed_87 Aug 14 '19 at 23:23