收缩器可能无法优化 Java 字节码

问题描述 投票:0回答:18

我正在尝试将 Cloud Firestore 集成到 Android 应用程序,但每次我得到的都是这个错误:

Launching lib/main.dart on Android SDK built for x86 in debug mode...
Note: /home/tr/DevTools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.13.4+2/android/src/main/java/io/flutter/plugins/firebase/cloudfirestore/CloudFirestorePlugin.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
D8: Cannot fit requested classes in a single dex file (# methods: 76095 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
The number of method references in a .dex file cannot exceed 64K

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 6m 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
Exited (sigterm)
java android firebase flutter google-cloud-firestore
18个回答
86
投票

在应用程序的

build.gradle
中:

defaultConfig {
    applicationId "com.company.test"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

minSdkversion
从 16 更改为 21,这对我来说有效。


28
投票

您只需将 minsdkversion 更改为 24 而不是 16

android\app\build.gradle

 defaultConfig {
        applicationId "com.company.example"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 }

它有效或我。 ;)


20
投票

我在使用 flutter 编码时遇到了类似的问题,但是

BUILD FAILED in 9s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 11.0s [!] 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
Gradle task assembleDebug failed with exit code 1

但是我已经成功运行了我的应用程序,这就是我所做的。

1.我找到了 android/app/build.gradle 文件 2.然后在gradle文件中访问以下代码

    buildTypes {
    release {

        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
    }
}

}

并将其更改为

buildTypes {
    debug {
        minifyEnabled true

        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
    }
}

}

该应用程序能够在Android模拟器中运行


9
投票

这个问题有两种不同的答案,但我认为最合适的是@Sarang Pal 给出的答案 这是Google官方的解释:

Android 构建失败故障排除:

如果您计划使用 Android 设备或模拟器进行开发,则需要处理 multidex 支持 - 否则,您的构建将失败并显示“无法在单个 dex 文件中容纳请求的类。”

默认情况下,Flutter 支持 Android SDK v16(Jelly Bean,2012 年发布),但 multidex 并不能真正与 Jelly Bean 配合使用(尽管这是可能的)。配置 Jelly Bean 使其工作超出了本 Codelab 的范围,因此我们将最低目标 SDK 版本从 v16 更改为 v21(Lollipop,2014 年发布)。

更改最低目标 SDK 版本:

  1. 打开 android/app/build.gradle,然后找到显示 minSdkVersion 16 的行。
  2. 将该行更改为 minSdkVersion 21。
  3. 保存文件。

https://codelabs.developers.google.com/codelabs/flutter-firebase/index.html#3


6
投票

导航到 {your-app-name} ndroid pp uild.gradle

// 将你的 minSdkVersion 从 16 更改为 21

defaultConfig {
    applicationId "com.example.testapp"
    minSdkVersion 21 
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName

}

5
投票

只需将

minSdkVersion
更改为 21 即可正常工作

defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.flutter_app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

5
投票

我尝试了上面的每一步,但没有成功。

最后,我在终端内使用这些命令解决了:

$: flutter clean

$: flutter pub get

一切都很顺利。


3
投票

终于我用最简单的方法解决了这个问题!

重要提示: 只需转到 app:level module

android/app/build.gradle
并放置
multiDexEnabled true

android {
.
.
.
 defaultConfig {
        multiDexEnabled true
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.you_app_name"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
}

Android 应用程序默认具有 SingleDex 支持,这限制了您的应用程序仅具有 65536 个方法(引用)。所以 multidexEnabled = true 只是意味着现在您可以在应用程序中编写超过 65536 个方法(引用)。

尝试一下,也许可以解决这个问题

  [!] The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the --no-shrink flag to this command.

希望!!!它有帮助。


3
投票

就我而言,我已将这些行添加到 Android 中的应用程序级 gradle 文件中以禁用 minify,即缩小代码

buildTypes {
    release {
        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
        shrinkResources false
        minifyEnabled false
    }
}

2
投票

转到 android/app/build.gradle,然后复制并粘贴此内容并更改应用程序 ID。

defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "your application id"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
}

2
投票

我已经跑了

flutter create .

为了生成windows和web文件夹 但由于我的项目设置为使用 java 作为本机而不是 Kotlin,生成器在 /android/src/main 中创建了一个 Kotlin 文件夹,这导致了混乱,我想我刚刚删除了它,它对我有用。


1
投票

在 android pp uild.gradle 文件中: 只需将 minSdkVersion 16 更改为 21

minSdkVersion 21

而且效果会很好。


1
投票

更改最低目标 SDK 版本:

  1. 打开 android/app/build.gradle,然后找到显示 minSdkVersion 16 的行。
  2. 将该行更改为 minSdkVersion 21。
  3. 保存文件。

1
投票

这是我修复我的问题的方法:

android {
    defaultConfig {
        multiDexEnabled true
    }
}

1
投票

所以,我遇到了这个问题,因为我手动创建了一个 MainActivity.java 文件,因为我的 flutter 项目没有创建它。

我尝试了文档中的所有内容,例如

         minifyEnabled true
         shrinkResources true
         minSdkVersion 21

但是他们没有工作。

我分析了代码并删除了MainAcivity.kt Kotlin 文件。现在可以了。


0
投票

enter image description here

defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.placementapp"
        minSdkVersion 23 //replace 23
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

0
投票

当我第一次使用“cloud firestore”时,我也遇到了这个错误,你应该更改你的“Android Level build.gradle”

Android 级别“build.gradle”

确保:

defaultConfig {
   "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}


dependencies {
    implementation 'com.android.support:multidex:1.0.3'
}

0
投票

我遇到了同样的问题,我只是将 minSdkVersion 从 24 更改为 26:minSdkVersion 26

© www.soinside.com 2019 - 2024. All rights reserved.