找不到方法 flutter()

Could not find method flutter()

我试图在 macOS 上的 android 模拟器中打开我的应用程序,但出现以下错误

* Where:
Build file '/Users/home/app_sources/my_app/android/app/build.gradle' line: 63

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not find method flutter() for arguments [build_enl1wb3rxbgojqnsmrkb1pqib$_run_closure3@4a124cf3] on project ':app' of type org.gradle.api.Project.

另外,如果我尝试 Open Android module in Android Studio

打开时没有配置

当我尝试添加配置时 select

没有模块

过去 3 天我一直面临这个问题,相信我,我已经尝试了这个网站上的每个主题,关于这个问题的每篇文章都没有用,所以请不要建议 google 搜索,因为我做了很多!

注:

  1. 使用Sync Project with Gradle Files return 和上面一样的错误
  2. 使用 Invalidate Caches 也无济于事。
  3. settings.gradle 中评论 include ':app' 并同步,再次取消评论和同步也无济于事。

有什么想法吗?

更新

settings.gradle

include ':app'

def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()

assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }

def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"

android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

android/app/build.gradle

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 flutter.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    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 "xxx.xxx.xxx.xxx"
        minSdkVersion 19
        targetSdkVersion 32
        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"
}

您需要从

更新build.gradle
def localPropertiesFile = rootProject.file('local.properties')

def localPropertiesFile = new rootProject.file('local.properties')

会解决你的问题。

已解决

I've removed Flutter SDK and Dart SDK from my system and re-install them then error gone away.

PS: 我不知道为什么会这样或者为什么全新安装解决了这个问题(特别是我安装的版本与这个全新安装的版本相同)但它成功了,这就是我所知道的。