我正在尝试将我的 AS 升级到最新的 beta2,并遇到以下app
和lib
模块的问题。
在我的app
build.gradle
我有 2 种口味
flavorDimensions "default"
productFlavors {
stage {
applicationId "com.mycompany.hello.stage"
resValue "string", "app_name", "Stage"
}
production {
applicationId "com.mycompany.hello.stage.production"
resValue "string", "app_name", "Live"
}
}
我指定该应用程序将仅与特定类型的lib
通信,如下所示:
stageCompile project(path: ':lib', configuration: 'debug')
productionCompile project(path: ':lib', configuration: 'release')
lib
build.gradle
文件中,我只有构建类型,没有风格块
publishNonDefault true
buildTypes {
debug {
versionNameSuffix ".debug"
}
release {
versionNameSuffix ".release"
minifyEnabled true
}
}
以上代码据我所知,app
将取决于构建变体并与lib
的特定构建变体交谈。在我升级到 AS 3.0 之前,它运行良好。
这是 Gradle 错误消息...我不确定这是否是由于我的两个 gradle 文件中flavorDimensions
不匹配引起的。
Error:Could not determine the dependencies of task ':app:compileStageDebugAidl'.
> Could not resolve all task dependencies for configuration ':app:stageDebugCompileClasspath'.
> Could not resolve project :lib.
Required by project :app
> Project :app declares a dependency from configuration 'stageCompile' to configuration 'debug' which is not declared in the descriptor for project :lib.
所以我花了几个小时迁移我的项目,以匹配我的Android Studio提供的迁移指南。
我最终会进行以下更改以实现我想要的:
app
变体productionRelease
应调用lib
release
app
变体stageDebug
应调用lib
debug
App build.gradle file
flavorDimensions "default" //add this line for flavorDimensions
productFlavors {
stage {
...
}
production {
...
}
}
implementation project(':lib')
在我的lib build.gradle中,我保持不变
publishNonDefault true
buildTypes {
debug {
versionNameSuffix ".debug"
}
release {
versionNameSuffix ".release"
minifyEnabled true
}
}
我在这里唯一的问题是忽略某些我不想实现生产发布(app(+release(lib(和stageDebug(app(+debug(lib(组合的变体。
这样,我将只剩下 AS 面板中的productionRelease
和stageDebug
变体。
在应用程序构建中.gradle
variantFilter { variant ->
def names = variant.flavors*.name
if (names.contains("stage") && variant.buildType.name == "release") {
variant.ignore = true
}
if (names.contains("production") && variant.buildType.name == "debug") {
variant.ignore = true
}
}
当我切换app
的构建变体时,它还会将我的lib
切换到相应的构建类型。
我仍然不确定这是否是最佳实践。如果你们找到更好的方法。请让我知道。