Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude advertising libraries from paid app build?

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']
    }
}
like image 834
sajmons Avatar asked Oct 01 '13 20:10

sajmons


1 Answers

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')
}
like image 149
sajmons Avatar answered Oct 08 '22 11:10

sajmons