使用颤振运行命令时出现此错误

Posted

技术标签:

【中文标题】使用颤振运行命令时出现此错误【英文标题】:Getting this error when I use flutter run command 【发布时间】:2021-10-31 08:16:28 【问题描述】:

将firebase依赖项添加到我的build.gradle 得到这个错误,因为我做了flutter run

   Launching lib\main.dart on KB2001 in debug mode...
    Running Gradle task 'assembleDebug'...
    D8: Cannot fit requested classes in a single dex file (# methods: 88379 > 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: 88379 > 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 10s
    [!] 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
    Exception: Gradle task assembleDebug failed with exit code 1

知道如何解决这个问题吗?

【问题讨论】:

【参考方案1】:

Android 5.0(API 级别 21)之前的平台版本使用 Dalvik 运行时来执行应用代码。默认情况下,Dalvik 将应用程序限制为单个 classes.dex

修改app级的build.gradle文件启用multidex,并将multidex库添加为依赖,如下图:

android 
    defaultConfig 
        ...
        minSdkVersion 15 
        targetSdkVersion 28
        multiDexEnabled true
    
    ...


dependencies 
    implementation "androidx.multidex:multidex:2.0.1"

创建应用类

 import io.flutter.app.FlutterApplication
    import android.content.Context
    import androidx.multidex.MultiDex
    
    class App : FlutterApplication() 
    
        override fun attachBaseContext(base: Context) 
            super.attachBaseContext(base)
            MultiDex.install(this)
        
    
    

编辑你的清单文件,在标签中设置 android:name 如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name=".App" >
        ...
    </application>
</manifest>

**注意或者你可以设置minSdkVersion 21来避免这个错误

请参阅documentation 了解更多信息

【讨论】:

【参考方案2】:

将您的最低 sdk 版本设置为 21。

【讨论】:

【参考方案3】:

这意味着您的应用已达到 android 对单个 dex 文件设置的最大方法限制。解决此问题的一种方法是启用多 dex 功能。按照这个链接,它提供了关于您的问题以及如何解决它的最佳解释。 https://developer.android.com/studio/build/multidex

【讨论】:

请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。

以上是关于使用颤振运行命令时出现此错误的主要内容,如果未能解决你的问题,请参考以下文章

每次在 vscode 中运行我的颤振应用程序时出现此错误 [重复]

执行歌词命令时出现此错误:DiscordAPIError:无法发送空消息

创建新的 React 应用程序时出现此错误。也尝试了卸载命令,但它不起作用

使用 laravel 微风运行 npm 时出现节点错误

运行查询以创建表时出现错误 1064?

使用 mysql 运行 JDBC 程序时出现此错误 [重复]