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

Flutter项目可在模拟器运行但无法构建APK,image_picker测试失败

Flutter项目调试正常但无法构建Release APK(image_picker相关测试失败)

问题说明

项目可通过flutter run正常调试运行,但执行flutter build apk或构建Release版本时失败。添加--stacktrace参数排查后发现,问题源于使用最新版image_picker: ^0.8.5+1,出现Task:image_picker_android:testDebugUnitTest FAILED错误,多项单元测试不通过。

构建报错信息

Building with sound null safety 


FAILURE: Build failed with an exception.

* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1159

* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* 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 35s
Running Gradle task 'assembleRelease'...                           36.5s
Gradle task assembleRelease failed with exit code 1

Flutter环境检测结果(flutter doctor)

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.3.9, on Microsoft Windows [Version 10.0.22000.1219], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[!] Visual Studio - develop for Windows (Visual Studio Professional 2017 15.9.51)
    X Visual Studio 2019 or later is required.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2021.2)
[√] VS Code (version 1.73.1)
[√] Connected device (4 available)
[√] HTTP Host Availability

! Doctor found issues in 1 category.

解决方案建议

1. 跳过image_picker的单元测试

在项目的android/app/build.gradle文件中,添加测试过滤配置,跳过image_picker相关的单元测试:

android {
    // 其他配置...
    testOptions {
        unitTests {
            filter {
                // 排除image_picker的所有测试用例
                excludeTestsMatching "io.flutter.plugins.imagepicker.*"
            }
        }
    }
}

保存后重新执行flutter build apk。

2. 临时降级image_picker版本

在pubspec.yaml中将image_picker降级到已知稳定的版本,比如:

dependencies:
  image_picker: ^0.8.4+4

执行flutter pub get更新依赖后,再尝试构建Release版本。

3. 清理构建缓存后重试

依次执行以下命令清理缓存并重新构建:

flutter clean
flutter pub get
flutter build apk

4. 直接跳过所有测试任务构建

执行构建命令时,通过参数跳过测试任务:

flutter build apk --no-test
# 或者直接调用Gradle命令
./gradlew assembleRelease -x testDebugUnitTest -x testReleaseUnitTest

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 08:45:29