如何通过选择性捆绑依赖库生成多口味apk?

How to generate multi flavor apk by selectively bundling the dependency library?

我的 android 应用程序依赖于多个 android 库(比如 A、B、C、D)。我想从中生成不同的 apks 说 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,但它包括每种风格的所有库。我怎样才能避免这种情况?

为您的 dependencies 使用特定风味的指令。例如,您可以使用 demoImplementationfullImplementation 而不是 implementationdemo 构建将包含 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'
}