如何在 Android Studio 中解决 java.lang.NoClassDefFoundError 异常



我正在使用LibGDX,musicg和jlayer在Android Studio中创建一个项目。问题是,当在核心项目中使用这些库中的任何类时,会出现错误java.lang.NoClassDefFoundError。我在核心项目的文件夹库上的 jar 中拥有库。

这不是 multidex 错误,因为我的项目只有 18k 方法。

来自错误和 gradle 构建的屏幕截图:

日志错误

W/dalvikvm: threadid=12: thread exiting with uncaught exception (group=0x41a2bc98)
W/dalvikvm: threadid=12: uncaught exception occurred
W/System.err: java.lang.NoClassDefFoundError: javazoom.jl.converter.Converter
W/System.err:     at com.beatshare.game.Utilities.Utils.sync(Utils.java:129)
W/System.err:     at Screens.ListMusicScreen.show(ListMusicScreen.java:44)
W/System.err:     at com.badlogic.gdx.Game.setScreen(Game.java:61)
W/System.err:     at Screens.MainMenuScreen$2.touchUp(MainMenuScreen.java:86)
W/System.err:     at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:58)
W/System.err:     at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:353)
W/System.err:     at com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:379)
W/System.err:     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:457)
W/System.err:     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1814)
W/System.err:     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1517)
W/dalvikvm: threadid=12: calling UncaughtExceptionHandler
E/AndroidRuntime: FATAL EXCEPTION: GLThread 794
                  Process: com.beatshare.game, PID: 29535
                  java.lang.NoClassDefFoundError: javazoom.jl.converter.Converter
                      at com.beatshare.game.Utilities.Utils.sync(Utils.java:129)
                      at Screens.ListMusicScreen.show(ListMusicScreen.java:44)
                      at com.badlogic.gdx.Game.setScreen(Game.java:61)
                      at Screens.MainMenuScreen$2.touchUp(MainMenuScreen.java:86)
                      at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:58)
                      at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:353)
                      at com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:379)
                      at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:457)
                      at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1814)
                      at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1517)

Core build.gradle

apply plugin: "java"
sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]

dependencies {
    compile 'com.google.code.gson:gson:2.8.0'
    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/jl1.0.1.jar')
    compile files('libs/musicg-1.4.2.0.jar')
}

eclipse.project {
    name = appName + "-core"
}

App build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://repo1.maven.org/maven2/" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
}
allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"
    version = '1.0'
    ext {
        appName = "Beatshare"
        gdxVersion = '1.9.6'
        roboVMVersion = '2.3.0'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}
project(":android") {
    apply plugin: "android"
    configurations { natives }
    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
    }
}
project(":core") {
    apply plugin: "java"
    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

    }
}
tasks.eclipse.doLast {
    delete ".project"
}

jlayer.jar 在 maven 存储库中可用,因此以这种方式编译并从 libs 文件夹中删除jlayer.jar

dependencies {
    compile 'com.google.code.gson:gson:2.8.0'
    compile group: 'javazoom', name: 'jlayer', version: '1.0.1'
    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    compile fileTree(include: ['*.jar'], dir: 'libs')   
}

java.lang.NoClassDefFoundError

此异常表示 JVM 在其内部类定义数据结构中查找了类的定义,但没有找到它。

这与说它无法从类路径加载不同。通常这表明我们之前尝试从类路径加载一个类,但由于某种原因失败了 - 现在我们尝试再次使用该类(因此需要加载它,因为它上次失败了(,但我们甚至不会尝试加载它,因为我们之前加载失败了(并且有理由怀疑我们会再次失败(。

较早的故障可能是ClassNotFoundExceptionExceptionInInitializerError(指示静态初始化块中的故障(或任何其他问题。关键是,NoClassDefFoundError不一定是类路径问题。

相关内容

  • 没有找到相关文章

最新更新