0

我有 5 个构建变体。需要一组给定 Gradle 依赖项的 4 个不同的构建变体(“内部”)和不需要的 1 个构建变体(“外部”)。引用这些依赖项的代码仅在内部构建变体的 /src 文件夹中。

我可以更简洁地声明“除此变体之外的所有”都需要给定的依赖项列表吗?

这就是我们现在处理这个问题的方式app-level build.gradle file

internalDevelopmentImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalDevelopmentImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalDevelopmentImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalDevelopmentImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

internalReleaseImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalReleaseImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalReleaseImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalReleaseImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

internalStageImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalStageImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalStageImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalStageImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

internalProdImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalProdImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalProdImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalProdImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

然后,externalProd变体根本不声明依赖项。

有没有办法改善这一点?

4

1 回答 1

1

这里有一个想法:使用配置继承。使用大多数配置共享的依赖项创建一个配置,在那里设置公共依赖项。让其他人从这个延伸。就像是:

configurations {
    // Add the bulk of dependencies to this one:
    commonImplementation {}

internalDevelopmentImplementation.extendsFrom(commonImplementation)
    internalReleaseImplementation.extendsFrom(commonImplementation)
    // And so on

免责声明:我没有测试过这个。检查https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ConfigurationContainer.html

于 2021-05-18T13:32:07.390 回答