Flutter迁移Gradle Plugin DSL后报错:com.android.tools.build:gradle未设置
com.android.tools.build:gradle is not set 错误信息
Where:
Build file 'flutter/android/build.gradle' line: 17What went wrong:
A problem occurred evaluating root project 'android'.A problem occurred configuring project ':app'.
Failed to notify project evaluation listener.
com.android.tools.build:gradle is not set in the build.gradle file
com.android.tools.build:gradle is not set in the build.gradle file
项目配置详情
app/build.gradle
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" id "com.google.firebase.crashlytics" id "com.huawei.agconnect" } 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' } android { signingConfigs { debug { storeFile file(System.properties['user.home'] + '/.android/debug.keystore') storePassword 'android' keyAlias 'androiddebugkey' keyPassword 'android' } } compileSdkVersion 34 sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "xx.xxxx.xxx" minSdkVersion 24 targetSdkVersion 34 multiDexEnabled true versionCode flutterVersionCode.toInteger() versionName flutterVersionName signingConfig signingConfigs.'release-signing' resConfigs "en", "zh-rCN", "ar" } flavorDimensions "default" productFlavors { huawei { dimension "default" applicationIdSuffix "" manifestPlaceholders = [appName: "Alriyada"] } } buildTypes { debug { signingConfig signingConfigs.debug } release { signingConfig signingConfigs.'release-signing' aaptOptions { minifyEnabled true shrinkResources true ignoreAssetsPattern '!mlkit_pose:!mlkit_label_default_model:' proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } packagingOptions { exclude 'lib/**/libtranslate_jni.so' exclude 'lib/**/libdigitalink.so' exclude 'lib/**/libxeno_native.so' exclude 'lib/**/libmlkitcommonpipeline.so' exclude 'lib/**/libbarhopper_v2.so' exclude 'lib/**/libclassifier_jni.so' exclude 'lib/**/libface_detector_v2_jni.so' exclude 'lib/**/libtensorflowlite_jni.so' exclude 'lib/**/liblanguage_id_jni.so' } } flutter { source '../..' } dependencies { implementation 'io.github.pilgr:paperdb:2.7.2' implementation "androidx.multidex:multidex:2.0.1" implementation "com.google.firebase:firebase-iid:21.1.0" huaweiImplementation 'com.huawei.agconnect:agconnect-core:1.9.0.300' huaweiImplementation 'com.huawei.hms:push:6.11.0.300' huaweiImplementation 'com.huawei.hms:hmscoreinstaller:6.7.0.300' } googleServices.disableVersionCheck = true
settings.gradle
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() maven { url 'https://developer.huawei.com/repo/' } gradlePluginPortal() } resolutionStrategy { eachPlugin { if (requested.id.id == "com.huawei.agconnect") { useModule("com.huawei.agconnect:agcp:1.9.0.300") } } } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "7.4.2" apply false id "org.jetbrains.kotlin.android" version "1.9.0" apply false id "com.google.gms.google-services" version "4.4.2" apply false id 'com.google.firebase.crashlytics' version '2.9.0' apply false id 'com.huawei.agconnect' version '1.9.0' apply false } include ':app'
build.gradle(根目录)
allprojects { repositories { google() mavenCentral() maven { url "https://jitpack.io" } maven {url 'https://developer.huawei.com/repo/'} } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
环境信息
- Flutter版本:3.16.0
- Gradle版本:7.4.2
- Kotlin版本:1.9.0
- Android Gradle Plugin版本:7.4.2
解决方案
1. 调整Flutter Gradle插件的引用方式
在settings.gradle的pluginManagement块中,移除includeBuild("$flutterSdkPath/packages/flutter_tools/gradle"),改为通过仓库引入并指定插件映射:
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 }() repositories { google() mavenCentral() maven { url 'https://developer.huawei.com/repo/' } gradlePluginPortal() maven { url "$flutterSdkPath/packages/flutter_tools/gradle" } } resolutionStrategy { eachPlugin { if (requested.id.id == "dev.flutter.flutter-gradle-plugin") { useModule("dev.flutter:flutter-gradle-plugin:1.0.0") } if (requested.id.id == "com.huawei.agconnect") { useModule("com.huawei.agconnect:agcp:1.9.0.300") } } } }
同时在settings.gradle的plugins块中添加Flutter Gradle插件的版本声明:
plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false id "com.android.application" version "7.4.2" apply false id "org.jetbrains.kotlin.android" version "1.9.0" apply false id "com.google.gms.google-services" version "4.4.2" apply false id 'com.google.firebase.crashlytics' version '2.9.0' apply false id 'com.huawei.agconnect' version '1.9.0' apply false }
2. 校验版本兼容性
AGP 7.4.2对应Gradle 7.5版本,建议将gradle/wrapper/gradle-wrapper.properties中的Gradle版本更新为7.5:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
3. 修复语法错误
app/build.gradle中读取local.properties的代码存在语法错误,将reader -改为reader ->,确保配置正常加载。
4. 清理缓存并重新同步
- 删除项目根目录及
android目录下的.gradle、build文件夹 - 执行终端命令:
flutter clean && flutter pub get && cd android && ./gradlew clean && ./gradlew assembleDebug - 或在Android Studio中点击
File -> Sync Project with Gradle Files
内容的提问来源于stack exchange,提问作者Mohanned Binmiskeen

