Android Studio Arctic Fox 原生项目集成Flutter模块Failed to apply plugin class ‘FlutterPlugin’
今天升级Android Studio Arctic Fox Patch2后,新建Android原生项目,并在原生项目工程上新建Flutter module,编译无法通过,报错信息: Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin class 'FlutterPlugin'. Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by plugin class 'FlutterPlugin' 谷歌了半天没有找到解决方法,只好自己一步步慢慢解决,最终找到了解决方式。 根据报错信息,应该是在构建Flutter module的时候出的问题,大体意思是构建过程被配置为更喜欢settings里的仓库,而不是project里面的仓库,但是仓库maven通过FlutterPlugin添加了。 打开Android原生工程的settings.gradle文件,对比发现通过Arctic Fox创建的项目,settings.gradle多了这些配置信息: dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } 根据gradle文档描述,DependencyResolutionManagement是gradle6.8之后新增的配置项,它为构建的所有工程配置依赖解析 RepositoriesMode配置在构建中仓库如何设置,总共有三种方式: FAIL_ON_PROJECT_REPOS 表示如果工程单独设置了仓库,或工程的插件设置了仓库,构建就直接报错抛出异常 PREFER_PROJECT 表示如果工程单独设置了仓库,就优先使用工程配置的,忽略settings里面的 PREFER_SETTINGS 表述任何通过工程单独设置或插件设置的仓库,都会被忽略 到这里,综合报错信息就豁然开朗了,原来是因为settings.gradle里配置了FAIL_ON_PROJECT_REPOS,而Flutter插件又单独设置了repository,所以会构建报错,所以我直接把FAIL_ON_PROJECT_REPOS改成PREFER_PROJECT,再重新build,接下来又有报错信息了,大致信息如下: Execution failed for task ':app:mergeDebugResources'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30. Searched in the following locations: – https://storage.flutter-io.cn/download.flutter.io/org/jetbrains/kotlin/kotlin-stdlib-jdk8/ 1.5.30/kotlin-stdlib-jdk8-1.5.30.pom … 原来都所有依赖库都按找flutter module里面配置的仓库下载依赖了,打开Android工程目录下的build.gradle,添加下面的配置: allprojects { repositories { google() jcenter() } } 然后sync,重新build,成功了!顺手给stackoverflow提交了一个回答~: PluginApplicationException: Failed to apply plugin … Read more