如何从付费应用构建中排除广告库?

时间:2013-10-01 20:55:46

标签: android build gradle

我正在使用的构建文件如下。我想实现我的付费版本不包含广告库。我只包含免费的广告图书馆。但它们存在于所有版本中。

如何更改构建文件以获取广告库免费付费版本?

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']
    }
}

1 个答案:

答案 0 :(得分:8)

经过一番研究后,我想我已经实现了我想要的目标。在此https://stackoverflow.com/a/17246390/1463357和此https://stackoverflow.com/a/18213259/1463357帖子的帮助下,我能够修复我的gradle.build文件。

我的原始构建文件存在两个问题。第一个问题是错误定义的依赖。必须在productFlavors 之后定义依赖关系,并且必须以 flavorNameCompile 的形式定义它们,如下所示:

productFlavors {
    free {
    }
}

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

第二个问题出现在错误的sourceSets定义中。要正确定义flavor的类型,必须在sourceSets 之前定义。像这样:

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']
    }
}

应用这些更改后,不同口味的apk尺寸与预期不同,并且没有找到广告库的痕迹。

这是我最后的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.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')
}