升级Flutter 3.19后,使用Plugin DSL Gradle添加包遇错误
Flutter 3.19切换Gradle Plugin DSL后添加flutter_local_notifications报错的解决方法
问题场景
升级到Flutter 3.19版本后,按要求将Gradle配置切换为Plugin DSL模式,尝试添加flutter_local_notifications包时,出现插件找不到的错误,具体报错信息如下:
FAILURE: Build failed with an exception. * Where: Settings file '/StudioProjects/project/android/settings.gradle' line: 33 * What went wrong: Plugin [id: 'com.android.tools', version: '7.3.1', apply: false] was not found in any of the following sources: - Gradle Core Plugins (plugin is not in 'org.gradle' namespace) - Included Builds (None of the included builds contain this plugin) - Plugin Repositories (could not resolve plugin artifact 'com.android.tools:com.android.tools.gradle.plugin:7.3.1') Searched in the following repositories: Google MavenRepo Gradle Central Plugin Repository * 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 1s Error: Gradle task assembleDebug failed with exit code 1
错误原因
- 混淆了Android Gradle插件的依赖坐标与Plugin DSL的插件ID:文档要求的
classpath 'com.android.tools.build:gradle:7.3.1'是传统buildscript方式的依赖坐标,对应Plugin DSL模式下的正确插件ID为com.android.application,而非错误的com.android.tools。 settings.gradle中错误声明了不存在的com.android.tools插件,同时com.android.application的版本未匹配到要求的7.3.1。android/app/build.gradle中错误引入了不存在的com.android.tools插件。
修正步骤
1. 修改settings.gradle
删除错误的com.android.tools插件声明,将com.android.application的版本统一为7.3.1:
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath }() includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "7.3.1" apply false id "org.jetbrains.kotlin.android" version "1.7.10" apply false id 'com.google.gms.google-services' version "4.3.15" apply false } include ":app"
2. 修改android/app/build.gradle
删除plugins块中的id 'com.android.tools'声明:
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" } def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { compileSdkVersion rootProject.ext.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } signingConfigs { release { } } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.release shrinkResources false } } } flutter { source '../..' } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2' implementation 'androidx.window:window:1.0.0' implementation 'androidx.window:window-java:1.0.0' implementation 'io.radar:sdk:3.8.+' implementation 'com.facebook.android:facebook-android-sdk:latest.release' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10" }
验证
执行以下命令清理并重新构建项目:
flutter clean flutter run
内容的提问来源于stack exchange,提问作者Atharv Sharma
相关产品推荐
相关产品推荐

