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

时间:2017-04-10 16:53:10

标签: java android libgdx jlayer musicg

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

这不是多指标错误,因为我的项目只有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"
}

1 个答案:

答案 0 :(得分:0)

maven repo中提供了

jlayer.jar,因此以这种方式编译并从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不一定是类路径问题。