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

Flutter安卓真机构建失败:Debug/Release模式XML解析错误求助

Flutter安卓真机构建失败:javax.xml.bind.UnmarshalException 解决方法

问题复现

Debug模式构建错误:

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> javax.xml.bind.UnmarshalException
   - with linked exception:
  [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.]

Release模式构建错误:

* What went wrong:
Could not determine the dependencies of task ':app:lintVitalReportRelease'.
> javax.xml.bind.UnmarshalException
   - with linked exception:
  [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.]

已尝试无效方案

  • 修改build.gradle中的编译配置:
    compileSdkVersion 34
    buildToolsVersion "34.0.0"
    
  • 更新Android SDK及SDK工具至最新版本
  • 卸载并重新安装Android Studio
  • 执行flutter doctor -v未检测到异常
  • 更换Gradle版本(如gradle-7.5-all.zip)并调整Kotlin版本

有效排查/解决步骤

  • 检查并修复Android SDK配置文件
    前往以下路径:

    • Linux/macOS:~/.android/repository/
    • Windows:C:\Users\<你的用户名>\.android\repository\
      删除空的或损坏的repository.xml、sources.xml文件,重启Android Studio后SDK Manager会自动重建这些文件。
  • 清理缓存
    执行以下命令彻底清理项目和Gradle缓存:

    flutter clean
    flutter pub get
    # Linux/macOS 清理Gradle缓存
    rm -rf ~/.gradle/caches/
    # Windows 清理Gradle缓存
    rd /s /q %USERPROFILE%\.gradle\caches
    
  • 验证Java环境
    确保JDK版本与Gradle兼容(Gradle 7.x对应JDK 11),执行java -version确认版本,检查JAVA_HOME环境变量是否指向正确的JDK安装路径。

  • 重置项目Android配置
    删除项目中android/.gradle和android/app/.gradle目录,对比Flutter官方空项目的build.gradle配置,移除自定义的异常配置项后重新同步项目。

内容的提问来源于stack exchange,提问作者Çağatay Kaya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:35:55