无法解析 Flutter 应用程序的 android/app/build.gradle 文件中的符号 GradleException

我有一个小的 Flutter 应用程序和 AndroidStudio(Arctic Fox 2020.3.1 Patch 2)抱怨以下错误

在 android/app/build.gradle 中:

无法解析符号“GradleException”

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.1, on macOS 11.6 20G165 darwin-x64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.2.2)
[✓] Connected device (3 available)

• No issues found!

我尝试了到目前为止在网上找到的建议,比如定义 SDK(已经定义,更改或删除它并再次设置它没有帮助)。

偶尔,我也得到

1.在android/app/build.gradle中:

无法解析符号“属性”

2.在android/settings.gradle中:

无法解析符号“文件”无法解析符号“属性”

编辑

这是 build.gradle 的内容。它或多或少是用flutter创建的

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 30

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "org.myorg.myapp"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    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.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//  implementation 'com.google.android.gms:play-services-ads:20.4.0'
}
stack overflow Cannot resolve symbol GradleException in Flutter application's android/app/build.gradle file
原文答案

答案:

作者头像

您的问题可以通过导入类 GradleException 来解决。只需添加此代码 import org.gradle.api.GradleException 。如果问题仍然存在,您可能需要重新安装 Gradle。您可以通过以下方式执行此操作:-

  1. 使缓存无效并重新启动(推荐)。

2.在gradle-wrapper.properties文件中,在distribution URL中,将版本改成最新支持的版本,我用的是7.1.1

  1. 如果问题只出在一个项目上,创建一个新的 Flutter 项目,并将你的类文件放在新项目中,因为会生成一个新的 build.gradle,这是由于 gradle 文件损坏或同步错误而发生的。