Flutter Build - Gradle 任务 assembleDebug 失败,退出代码为 1

Posted

技术标签:

【中文标题】Flutter Build - Gradle 任务 assembleDebug 失败,退出代码为 1【英文标题】:Flutter Build - Gradle task assembleDebug failed with exit code 1 【发布时间】:2021-08-01 03:52:38 【问题描述】:

我在项目中添加了一些 firebase 依赖项。 Pub getProcess finished with exit code 0 一起工作正常。但我无法构建应用程序导致以下错误。什么是压缩器可能未能优化 Java 字节码?拜托,谁能告诉我正确的原因?这些 firebase 依赖项基于 Firebase(2021 年 1 月 12 日 | Min Dart SDK 2.1)。依赖项有什么要改变的吗?

依赖关系

  firebase_core: ^0.7.0
  firebase_auth: ^0.20.0
  firebase_database: ^6.0.0
  cloud_firestore: ^0.16.0
  firebase_storage: ^7.0.0

  flutter_staggered_grid_view: ^0.4.0
  fluttertoast: ^8.0.6
  google_maps_place_picker: ^1.0.1
  image_picker: ^0.6.7+22
  multi_image_picker: ^4.7.14
  transparent_image: ^2.0.0

错误

Launching lib\main.dart on android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Note: C:\Flutter\flutter\.pub-cache\hosted\pub.dartlang.org\cloud_firestore-0.16.0+1\android\src\main\java\io\flutter\plugins\firebase\firestore\FlutterFirebaseFirestorePlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Flutter\flutter\.pub-cache\hosted\pub.dartlang.org\cloud_firestore-0.16.0+1\android\src\main\java\io\flutter\plugins\firebase\firestore\streamhandler\TransactionStreamHandler.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
D8: Cannot fit requested classes in a single dex file (# methods: 110393 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
    at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:132)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:119)
    at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:102)
    at com.android.build.gradle.internal.tasks.DexMergingTaskRunnable.run(DexMergingTask.kt:432)
    at com.android.build.gradle.internal.tasks.Workers$ActionFacade.run(Workers.kt:242)
    at org.gradle.workers.internal.AdapterWorkAction.execute(AdapterWorkAction.java:57)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:67)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:63)
    at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:63)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:200)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:195)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:75)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:68)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:153)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:68)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:62)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.lambda$call$2(DefaultBuildOperationExecutor.java:76)
    at org.gradle.internal.operations.UnmanagedBuildOperationWrapper.callWithUnmanagedSupport(UnmanagedBuildOperationWrapper.java:54)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:76)
    at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:60)
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:200)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:215)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:131)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, position: null
    at Version.fakeStackEntry(Version_2.1.62.java:0)
    at com.android.tools.r8.utils.Y.a(SourceFile:78)
    at com.android.tools.r8.D8.run(D8.java:11)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:117)
    ... 36 more
Caused by: com.android.tools.r8.utils.b: Cannot fit requested classes in a single dex file (# methods: 110393 > 65536)
    at com.android.tools.r8.utils.T0.error(SourceFile:1)
    at com.android.tools.r8.utils.T0.a(SourceFile:2)
    at com.android.tools.r8.dex.P.a(SourceFile:740)
    at com.android.tools.r8.dex.P$h.a(SourceFile:7)
    at com.android.tools.r8.dex.b.a(SourceFile:14)
    at com.android.tools.r8.dex.b.b(SourceFile:25)
    at com.android.tools.r8.D8.d(D8.java:133)
    at com.android.tools.r8.D8.b(D8.java:1)
    at com.android.tools.r8.utils.Y.a(SourceFile:36)
    ... 38 more


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
     The number of method references in a .dex file cannot exceed 64K.
     Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

* 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 23s
Exception: Gradle task assembleDebug failed with exit code 1
[!] The shrinker may have failed to optimize the Java bytecode.
    To disable the shrinker, pass the `--no-shrink` flag to this command.
    To learn more, see: https://developer.android.com/studio/build/shrink-code

【问题讨论】:

【参考方案1】:

我认为这是 dex 错误。

android/app 目录中的build.gradle 文件中启用multidex

android 
    defaultConfig 
        ...
        minSdkVersion 15 
        targetSdkVersion 28
        // Add this line
        multiDexEnabled true
    
    ...


dependencies 
  // Add this line
  implementation "androidx.multidex:multidex:2.0.1"

如果您有任何疑问,请发表评论。

【讨论】:

以上是关于Flutter Build - Gradle 任务 assembleDebug 失败,退出代码为 1的主要内容,如果未能解决你的问题,请参考以下文章

解决Flutter升级后,flutter run 卡在gradle build running的问题

Flutter App Build apk报错但运行正确解决

Visual Studio Code 中的 build.gradle 错误(Flutter)

Flutter Android Build Gradle错误配置根项目'android'?

无法解析 Flutter 应用程序的 android/app/build.gradle 文件中的符号 GradleException

Flutter build apk - 请更新您的 settings.gradle 文件