解决迁移至 Android Build Plugin 3.x 提示找不到 submodule 依赖的问题
Android Studio 2.3 升级到 Android Studio 3.0 Gradle builde 报错
Unable to resolve dependency for ':app@xxPreview/compileClasspath': Could not resolve project :library.
Could not resolve project :library.
Required by: project :app
> Unable to find a matching configuration of project :library:
- Configuration 'debugApiElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'preview' and found incompatible value 'debug'.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but was't required.
- Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
- Required versionCode 'xiaomi' but no value provided.
- Configuration 'debugRuntimeElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'preview' and found incompatible value 'debug'.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
- Required org.gradle.api.attributes.Usage 'java-api' and found incompatible value 'java-runtime'.
- Required versionCode 'xiaomi' but no value provided.
- Configuration 'releaseApiElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'preview' and found incompatible value 'release'.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
- Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
- Required versionCode 'xiaomi' but no value provided.
- Configuration 'releaseRuntimeElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'preview' and found incompatible value 'release'.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
- Required org.gradle.api.attributes.Usage 'java-api' and found incompatible value 'java-runtime'.
- Required versionCode 'xiaomi' but no value provided.
背景:build.gradle(module:library):
apply plugin:'com.android.library'
android{
buildTypes{
release{
...
}
debug{
...
}
}
}
build.gradle(module:app):
apply plugin:'com.android.application'
// 或
//apply plugin: 'com.android.library'
android{
buildTypes{
release{
...
}
preview{
...
}
debug{
...
}
}
}
dependencies{
compile project(':library')
}
builde.gradle(project:root):
from
dependencies{
classpath'com.android.tools.build:gradle:2.3.3'
}
to
dependencies{
classpath'com.android.tools.build:gradle:3.0.0'
}
原因: app 中 buildTypes 集合不是 library 的 buildTypes 集合子集, 即 app 中 buildType 属性 preview 在依赖的 library 中找不到.
解决:
1, 在所有 library 中添加 buildType 属性 preview. 如下:build.gradle(module:library):
apply plugin:'com.android.library'
android{
buildTypes{
release{
...
}
// 新增
preview{
...
}
debug{
...
}
}
}
2, 将 Gradle API 回滚到能够适配的版本. 如下:builde.gradle(project:root):
dependencies{
// 从 3.0.0 回滚到 2.3.3
classpath'com.android.tools.build:gradle:2.3.3'
}
方法 1 和方法 2 任选一种Bestfix:
build.gradle(module:app):
apply plugin:'com.android.application'
// 或
//apply plugin: 'com.android.library'
android{
buildTypes{
release{
...
}
preview{
...
// 关键代码, release, debug 为 library 中已有 buildType
matchingFallbacks=['release','debug']
}
debug{
...
}
}
}
dependencies{
compile project(':library')
}
来源: https://juejin.im/entry/5b17bce0e51d45067b059980