Android 4.x NoClassDefFoundError Multidex 完全实现

Android 4.x NoClassDefFoundError with Multidex fully implemented

我最近向我的应用程序添加了另一个依赖项,这使它超过了 64k 限制并需要 Multidex。当 运行 在 Android 4.x 时,应用程序现在在启动时崩溃并出现 NoClassDefFoundError:

FATAL EXCEPTION: main
Process: org.naturenet.debug, PID: 3136
java.lang.NoClassDefFoundError: org.naturenet.util.ForestFire
at org.naturenet.NatureNetApplication.onCreate(NatureNetApplication.java:60)

此错误的其他情况表明这是因为 Multidex 未完全实现,但在进行以下所有更改后仍会发生此错误:

应用构建文件

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:support-v4:25.1.0'
    ...
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "org.naturenet"
        minSdkVersion 16
        targetSdkVersion 23
        multiDexEnabled true
        jackOptions {
            enabled true
            additionalParameters('jack.incremental': 'true')
        }
    }
}

申请class

public class NatureNetApplication extends MultiDexApplication {

有问题的 ForestFire class 没有改变。我最初在调试和发布源代码树下有不同的实现,但是用主源代码树中的单个文件替换它们没有任何区别。

尝试将其添加到您的清单中,以防万一。

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

更多信息:https://developer.android.com/studio/build/multidex.html?hl=en-419

我在 Android 4.* 设备上也遇到了同样的问题,它以某种方式与使用 Jack 工具链有关。

Jack was deprecated 和 Android Studio 2.4 Preview 6 已发布:

Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.

有关详细信息,请参阅 this

现在您可以安装 Android Studio Preview 并从 build.gradle 中删除 jackOptions