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

Flutter旧项目执行flutter run报错:java.lang.NullPointerException

旧Flutter项目执行flutter run时Gradle构建空指针异常问题

问题详情

运行旧Flutter项目时,执行flutter run命令构建失败,抛出java.lang.NullPointerException且无额外错误信息。

flutter run报错日志

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\AbdoulayeB\OneDrive\Bureau\flutter\jellyflut\android\app\build.gradle' line: 30

* What went wrong:
A problem occurred evaluating project ':app'.
> java.lang.NullPointerException (no error message)

* 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 1m 13s
Running Gradle task 'assembleDebug'...                             74,6s
Error: Gradle task assembleDebug failed with exit code 1

执行./gradlew --info后的日志

> Configure project :app
Evaluating project ':app' using build file 'C:\Users\AbdoulayeB\OneDrive\Bureau\flutter\jellyflut\android\app\build.gradle'.
Using Kotlin Gradle Plugin gradle70 variant
Caching disabled for Kotlin DSL script compilation (Project/ScriptPlugin/stage1) because:
  Build cache is disabled
Skipping Kotlin DSL script compilation (Project/ScriptPlugin/stage1) as it is up-to-date.
Could not execute [class org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatHandler.buildFinished]

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\AbdoulayeB\OneDrive\Bureau\flutter\jellyflut\android\app\build.gradle' line: 30

* What went wrong:
A problem occurred evaluating project ':app'.
> java.lang.NullPointerException (no error message)

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --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 15s
4 actionable tasks: 2 executed, 2 up-to-date
Watched directory hierarchies: [C:\dev_mobile\flutter\packages\flutter_tools\gradle]

解决建议

  • 检查android/app/build.gradle第30行代码:空指针通常是引用了未初始化的变量或空对象调用方法,比如读取不存在的属性、插件配置里的空参数,确认该行代码的逻辑是否存在空引用问题。
  • 升级Gradle与Kotlin版本:旧项目的Gradle或Kotlin插件版本可能和当前Flutter版本不兼容,尝试更新android/gradle/wrapper/gradle-wrapper.properties中的distributionUrl到稳定版本(如https\://services.gradle.org/distributions/gradle-7.5-bin.zip),同时更新android/build.gradle中buildscript的Kotlin版本到匹配Gradle的稳定版(如1.7.20)。
  • 清理构建缓存:执行flutter clean,删除android/.gradle、android/app/build目录,再重新运行flutter run。
  • 迁移项目路径:项目放在OneDrive目录下可能存在同步锁或权限问题,将项目复制到本地非同步目录(如C:\Users\AbdoulayeB\Documents\flutter\jellyflut)后重新构建。
  • 获取详细栈追踪:执行./gradlew assembleDebug --stacktrace,查看完整的空指针调用栈,精准定位异常来源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 03:40:02