【问题标题】:NoClassDefFoundError with Android Studio on Android 4Android 4 上 Android Studio 的 NoClassDefFoundError
【发布时间】:2023-03-22 01:35:02
【问题描述】:

我正在将现有应用从 Eclipse 转换为 Android Studio。但是,当我在运行 4.x 的设备上运行它时(我已经在模拟器上测试了几个版本),它立即崩溃并显示 NoClassDefFoundError

我已经尝试注释掉对它找不到的类的引用,但总会有另一个。据我所知,有问题的班级总是

  1. 在我自己的代码中
  2. 一个内部类(更新:经过更多调查,这并不总是正确的)

在 5.0.1 模拟器上一切正常(我没有要测试的设备)。我的build.gradle 文件相当长,但看起来像这样:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

def AAVersion = "2.7.1"

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.myapp.android"
        minSdkVersion 8
        targetSdkVersion 19
        multiDexEnabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        *snip*
    }
}

buildscript {
    repositories {
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

repositories {
    maven {
        url "https://repo.commonsware.com.s3.amazonaws.com"
    }
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.pact.android'
    }
}

dependencies {
    *snip compile project(':...') declarations

    apt "com.googlecode.androidannotations:androidannotations:$AAVersion"
    compile "com.googlecode.androidannotations:androidannotations-api:$AAVersion"

    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.google.android.gms:play-services:3.1.36'

    *snip many more compile declarations*

    compile fileTree(dir: 'libs', include: ['*.jar'])
}

导致麻烦的类的一些例子:

  • 在 facebook 库中实现接口的匿名类(作为库项目)
  • Parcelable.CREATOR 我的模型中的实现
  • 扩展 android.os.Library 的内部类
  • 我自己的代码中的内部类
  • 在 maven 库中实现接口的类

出了什么问题,我该如何解决?

【问题讨论】:

    标签: android android-studio android-gradle-plugin


    【解决方案1】:

    我没有完全实现 MultiDex 支持,所以我的一些类不在正确的 dex 文件中。要修复它,您要做的不仅仅是在 defaultConfig 块中设置 multiDexEnabled = true。您还必须:

    1. 在您的依赖项中包含 compile 'com.android.support:multidex:1.0.1'
    2. 让您的应用程序类扩展MultiDexApplication 而不仅仅是Application。或者,您可以在您的应用程序的attachBaseContext() 中调用MultiDex.install()

    更多详情请见https://developer.android.com/tools/building/multidex.html

    【讨论】:

    • 帮助很大。谢谢你:)
    • 非常感谢。从eclipse移动项目后无法弄清楚。干杯:)
    • 男人!你为我节省了几天的工作时间。非常感谢!
    • MultiDexApplication 它的好决定!非常感谢:)
    【解决方案2】:

    1) 在默认配置中添加multiDexEnabled = true

    2) 在依赖项中添加 compile com.android.support:multidex:1.0.0

    3) Application 类扩展 MultiDexApplication 而不仅仅是 Application

    【讨论】:

      【解决方案3】:

      1) 在 build.gradle 的 defaultConfig 中添加 multiDexEnabled = true

      2) 您还必须包含 compile 'com.android.support:multidex:1.0.1'

      3) 将以下内容添加到清单中的“应用程序”标签中:

      android:name="android.support.multidex.MultiDexApplication"

      它与我合作,希望有所帮助

      【讨论】:

        【解决方案4】:

        在我的情况下,我通过删除 multiDexEnabled = true 解决了这个问题。

        【讨论】:

        • 刚加multiDexEnabled = true是不行的……karl的回答是对的。
        【解决方案5】:

        执行以下步骤后,我的问题也得到解决

        1) 在 build.gradle 的 defaultConfig 中添加 multiDexEnabled = true

        2) 你还必须包含编译com.android.support:multidex:1.0.1

        3) 将以下内容添加到清单中的“应用程序”标签中:

        android:name="android.support.multidex.MultiDexApplication"
        

        【讨论】:

          【解决方案6】:

          我遇到了同样的问题,尝试了 StackOverflow 上建议的所有解决方案,但在我的情况下它们不起作用。
          之后我发现了Java 8编译的库之一。所以,我也需要在我的项目中启用Java 8。

            android {
              //...
              compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_8
                targetCompatibility JavaVersion.VERSION_1_8
              }
            }
          

          导致此错误的原因有很多。希望对您有所帮助。

          【讨论】:

            【解决方案7】:

            就我而言,是 Kotlin forEach 导致了异常。 更多信息在这里:java.lang.NoClassDefFoundError $$inlined$forEach$lambda$1 in Kotlin

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-07-28
              • 2014-07-01
              • 2019-01-06
              • 1970-01-01
              • 2013-05-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多