7

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

我应该如何更改我的构建文件以获得广告库免费付费版本?

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

1 回答 1

9

经过一些研究,我认为我已经实现了我想要的。在此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 定义。要正确定义风味的 sourceSets,风味必须在 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')
}
于 2013-10-23T19:33:57.753 回答