Flutter构建APK失败:kotlin-gradle-plugin:1.7.10依赖解析错误
Flutter构建APK时Gradle无法解析Kotlin插件依赖问题
执行命令E:\src\flutter\bin\flutter.bat --no-color build apk开启严格空安全构建Flutter应用时,运行Gradle任务assembleRelease失败。错误核心为配置根项目android时无法解析:classpath的所有构件,具体是无法解析org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10——当前环境要求找到兼容Java 11、打包为jar且依赖外部声明的运行时组件,但该插件的多个变体均无法完全匹配需求。
完整报错日志
E:\src\flutter\bin\flutter.bat --no-color build apk Building with sound null safety Running Gradle task 'assembleRelease'... FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'android'. > Could not resolve all artifacts for configuration ':classpath'. > Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10. Required by: project : > The consumer was configured to find a runtime of a component compatible with Java 11, packaged as a jar, and its dependencies declared externally. However we cannot choose between the following variants of org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10: - gradle70JavadocElements - gradle70RuntimeElements - gradle70SourcesElements - javadocElements - runtimeElementsWithFixedAttribute - sourcesElements All of them match the consumer attributes: - Variant 'gradle70JavadocElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a runtime of a component, and its dependencies declared externally: - Unmatched attributes: - Provides documentation but the consumer didn't ask for it - Provides javadocs but the consumer didn't ask for it - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about its elements (required them packaged as a jar) - Provides attribute 'org.gradle.plugin.api-version' with value '7.0' but the consumer didn't ask for it - Provides release status but the consumer didn't ask for it - Variant 'gradle70RuntimeElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a runtime of a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Unmatched attributes: - Provides a library but the consumer didn't ask for it - Provides attribute 'org.gradle.jvm.environment' with value 'standard-jvm' but the consumer didn't ask for it - Provides attribute 'org.gradle.plugin.api-version' with value '7.0' but the consumer didn't ask for it - Provides release status but the consumer didn't ask for it - Variant 'gradle70SourcesElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a runtime of a component, and its dependencies declared externally: - Unmatched attributes: - Provides documentation but the consumer didn't ask for it - Provides sources but the consumer didn't ask for it - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about its elements (required them packaged as a jar) - Provides attribute 'org.gradle.plugin.api-version' with value '7.0' but the consumer didn't ask for it - Provides release status but the consumer didn't ask for it - Variant 'javadocElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a runtime of a component, and its dependencies declared externally: - Unmatched attributes: - Provides documentation but the consumer didn't ask for it - Provides javadocs but the consumer didn't ask for it - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about its elements (required them packaged as a jar) - Provides release status but the consumer didn't ask for it - Variant 'runtimeElementsWithFixedAttribute' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a runtime of a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Unmatched attributes: - Provides a library but the consumer didn't ask for it - Provides attribute 'org.gradle.jvm.environment' with value 'standard-jvm' but the consumer didn't ask for it - Provides release status but the consumer didn't ask for it - Variant 'sourcesElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a runtime of a component, and its dependencies declared externally: - Unmatched attributes: - Provides documentation but the consumer didn't ask for it - Provides sources but the consumer didn't ask for it - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about its elements (required them packaged as a jar) - Provides release status but the consumer didn't ask for it The following variants were also considered but didn't match the requested attributes: - Variant 'apiElementsWithFixedAttribute' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component - Variant 'gradle70ApiElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10 declares a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 19s Running Gradle task 'assembleRelease'... 23.9s Gradle task assembleRelease failed with exit code 1 Process finished with exit code 1
当前android\build.gradle文件内容
buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.0-alpha09' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms.google-services:com.google.gms.google-services.gradle.plugin:4.3.13' } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
解决方案
调整Gradle构建工具版本
目前使用的com.android.tools.build:gradle:7.4.0-alpha09是预览版,稳定性差且与Kotlin 1.7.10的Java版本要求不匹配。建议替换为稳定版,比如7.3.1(与Kotlin 1.7.10兼容):classpath 'com.android.tools.build:gradle:7.3.1'修正Google Services依赖格式
原依赖写法错误,改为标准格式:classpath 'com.google.gms:google-services:4.3.13'强制指定Java 11环境
在buildscript块中添加Java版本配置,同时修改项目根目录的gradle.properties文件:- 在
android/build.gradle的buildscript内添加:tasks.withType(JavaCompile) { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } - 在项目根目录的
gradle.properties中添加:org.gradle.java.home=<你的Java 11安装路径>
- 在
清理Gradle缓存
执行以下命令清理缓存并重新同步:flutter clean cd android && gradlew cleanBuildCache cd .. && flutter pub get
内容的提问来源于stack exchange,提问作者John Michael Tan
相关产品推荐
相关产品推荐

