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

Flutter Web构建正常但APK构建失败,提示Gradle元数据文件不存在

Flutter Web构建正常但APK构建失败,提示Gradle元数据文件不存在

我有一个Flutter应用在Chrome网页端运行完全正常,之前Android APK也能正常构建,但现在构建一直失败。试了GitHub Gemini给的各种建议都没解决,现在怀疑是Gradle文件出了问题——毕竟之前跟着Gemini的建议改了不少配置。

我平时用Android Studio Narwhal开发,但这次的输出都是在没开Android Studio的情况下生成的,已经执行过这些清理命令:

  • flutter clean
  • flutter pub get
  • ./gradlew clean
  • rm -rf ~/.gradle/caches/

系统信息

Linux PostCovid 6.8.0-62-generic #65-Ubuntu SMP PREEMPT_DYNAMIC Mon May 19 17:15:03 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

Flutter Doctor 结果

[✓] Flutter (Channel stable, 3.32.5, on Ubuntu 24.04.2 LTS 6.8.0-62-generic, locale en_CA.UTF-8) [105ms]
    • Flutter version 3.32.5 on channel stable at /home/doug/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fcf2c11572 (3 days ago), 2025-06-24 11:44:07 -0700
    • Engine revision dd93de6fb1
    • Dart version 3.8.1
    • DevTools version 2.45.1

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1) [2.1s]
    • Android SDK at /home/doug/Android/Sdk
    • Platform android-35, build-tools 35.0.1
    • ANDROID_SDK_ROOT = /home/doug/Android/Sdk
    • Java binary at: /home/doug/.local/share/JetBrains/Toolbox/apps/android-studio/jbr/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13391695-b895.109)
    • All Android licenses accepted.

[✓] Chrome - develop for the web [21ms]
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop [170ms]
    • Ubuntu clang version 18.1.3 (1ubuntu1)
    • cmake version 3.28.3
    • ninja version 1.11.1
    • pkg-config version 1.8.1
    ! Unable to access driver information using 'eglinfo'. It is likely available from your distribution (e.g.: apt install mesa-utils)

[✓] Android Studio (version 2025.1.1) [19ms]
    • Android Studio at /home/doug/.local/share/JetBrains/Toolbox/apps/android-studio/
    • Flutter plugin can be installed from:
      https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /home/doug/.local/share/JetBrains/Toolbox/apps/android-studio/
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13391695-b895.109)

[✓] VS Code (version unknown) [17ms]
    • VS Code at /usr/share/code
    • Flutter extension can be installed from:
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
    ✗ Unable to determine VS Code version.

[✓] Connected device (3 available) [332ms]
    • SM G965W (mobile) • 3357393445533098 • android-arm64 • Android 10 (API 29)
    • Linux (desktop) • linux • linux-x64 • Ubuntu 24.04.2 LTS 6.8.0-62-generic
    • Chrome (web) • chrome • web-javascript • Google Chrome 138.0.7204.49

[✓] Network resources [625ms]
    • All expected network resources are available.
    • No issues found!

APK构建错误信息

flutter build apk
FAILURE: Build failed with an exception.

* What went wrong:
Could not read workspace metadata from /home/doug/.gradle/caches/8.13/kotlin-dsl/accessors/45fa3cb6b4077a86ce0b8bff6eabdd7d/metadata.bin
> /home/doug/.gradle/caches/8.13/kotlin-dsl/accessors/45fa3cb6b4077a86ce0b8bff6eabdd7d/metadata.bin (No such file or directory)

相关Gradle文件内容

android/build.gradle.kts

val newBuildDir: Directory = rootProject.layout.buildDirectory.dir("../../build").get()
rootProject.layout.buildDirectory.value(newBuildDir)
subprojects {
    val newSubprojectBuildDir: Directory = newBuildDir.dir(project.name)
    project.layout.buildDirectory.value(newSubprojectBuildDir)
    repositories {
        google()
        mavenCentral()
    }
}
tasks.register<Delete>("clean") {
    delete(rootProject.layout.buildDirectory)
}

android/settings.gradle.kts

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    val flutterSdkPath = run {
        val properties = java.util.Properties()
        file("local.properties").inputStream().use { properties.load(it) }
        val flutterSdkPath = properties.getProperty("flutter.sdk")
        require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" }
        flutterSdkPath
    }
    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
    resolutionStrategy {
        eachPlugin {
            when (requested.id.id) {
                "org.jetbrains.kotlin.android" -> useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.24")
                "com.android.application" -> useModule("com.android.tools.build:gradle:8.5.0")
            }
        }
    }
    plugins {
        id("dev.flutter.flutter-plugin-loader") version "1.0.0"
        id("com.android.application") version "8.5.0"
        id("org.jetbrains.kotlin.android")
    }
}
include(":app")

android/app/build.gradle.kts

注:仅提供开头部分内容://import androidx.compose.

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 10:49:35