如何通过有选择地捆绑依赖库来生成多种风味的apk?

时间:2020-02-08 12:53:02

标签: android gradle build.gradle

我的android应用依赖于多个android库(例如A,B,C,D)。我想从中生成不同的apk,例如apk1只需要消耗A,B,而apk2需要只消耗B,C。

我通过做类似的事情探索了Android product flavor概念的选择,

android {
    ...
    defaultConfig {...}
    buildTypes {
        debug{...}
        release{...}
    }
    // Specifies one flavor dimension.
    flavorDimensions "version"
    productFlavors {
        demo {
            // Assigns this product flavor to the "version" flavor dimension.
            // If you are using only one dimension, this property is optional,
            // and the plugin automatically assigns all the module's flavors to
            // that dimension.
            dimension "version"
            applicationIdSuffix ".demo"
            versionNameSuffix "-demo"
        }
        full {
            dimension "version"
            applicationIdSuffix ".full"
            versionNameSuffix "-full"
        }
    }
}

现在,它正在生成具有多种产品的APK,但它包括每种口味的所有库。如何避免这种情况?

1 个答案:

答案 0 :(得分:1)

dependencies使用特定于风味的指令。例如,您可以使用implementationdemoImplementation代替fullImplementationdemo构建将包括demoImplementation依赖性,而full构建将包括fullImplementation依赖性。

documentation page that you linked to显示了一个使用free风格的示例:

dependencies {
    // Adds the local "mylibrary" module as a dependency to the "free" flavor.
    freeImplementation project(":mylibrary")

    // Adds a remote binary dependency only for local tests.
    testImplementation 'junit:junit:4.12'

    // Adds a remote binary dependency only for the instrumented test APK.
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}