You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter构建APK报错:Could not find method buildTypes() 求解

问题

我想通过Android Studio构建Flutter APK用于手机测试,操作步骤是点击Build > Flutter > Build APK,但构建失败。报错指向app/build.gradle第73行,提示Could not find method buildTypes() for arguments...。我已经确认buildTypes是在android{}代码块内,以下是项目和app模块的build.gradle配置代码,求解决办法。


构建报错日志

C:\SRC\flutter\bin\flutter.bat --no-color build apk

 Building with sound null safety 

Running Gradle task 'assembleRelease'...                        

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\Can Ahmet\StudioProjects\myapp\myapp\android\app\build.gradle' line: 73

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not find method buildTypes() for arguments [build_72oyoy28ktxj66hn01bbfxy4$_run_closure5@613623d3] on project ':app' of type org.gradle.api.Project.

* 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 11s
Gradle task assembleRelease failed with exit code 1
Running Gradle task 'assembleRelease'...                           18,0s
Process finished with exit code 1

项目根目录build.gradle

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

    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        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
}

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

    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 "com.example.myapp"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        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"
}

解决方案

问题根源是app模块build.gradle里的语法错误,导致Gradle无法正确解析脚本,进而误报找不到buildTypes()方法。

  1. 修复语法错误
    打开app模块的build.gradle文件,找到第10行左右的这段代码:

    localPropertiesFile.withReader('UTF-8') { reader -
        localProperties.load(reader)
    }
    

    把其中的reader -改成reader ->(Groovy闭包的正确语法):

    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
    
  2. 重新构建
    保存修改后,重新执行Build APK操作,或在终端运行flutter build apk命令。

如果修复后仍有问题,可尝试:

  • 检查local.properties中Flutter SDK路径是否正确,路径避免空格或特殊字符
  • 执行flutter clean清理构建缓存后再重新构建
  • 确认gradle版本(7.0.4)与当前Flutter版本兼容(建议Flutter 2.8.x及以上)

内容的提问来源于stack exchange,提问作者CanKURT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 07:35:22