Unable to resolve dependency for... Could not resolve project :library. Required by: project :app

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任选一种

Best fix:

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')
}