Android Studio | Libgdx |添加Kryonet库 - NoClassDefFoundError

时间:2015-07-15 22:50:15

标签: java android android-studio libgdx kryonet

我目前正在开发Android游戏,并希望实现多人游戏。 所以我想使用Kryonet:

  • 我下载了图书馆(https://github.com/EsotericSoftware/kryonet/releases
  • 在我的核心模块中创建了一个libs文件夹
  • 将kryonet-2.21-all.jar复制到其中
  • 并在build.gradle中将其添加为Module:

    dependencies {
     compile files('src/libs/kryonet-2.21-all.jar')  }
    

在Android Studio中,它似乎有效,我可以访问所有导入等,但在我的设备上运行应用程序时...崩溃了

07-16 00:00:44.419  12176-12196/com.cookedapps.robotredemption.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 3363
Process: com.cookedapps.robotredemption.android, PID: 12176
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/esotericsoftware/kryonet/Server;
        at com.cookedapps.robotredemption.server.GameServer.<init>(GameServer.java:20)
        at com.cookedapps.robotredemption.states.Play.<init>(Play.java:73)
        at com.cookedapps.robotredemption.handlers.GameStateManager.getState(GameStateManager.java:39)
        at com.cookedapps.robotredemption.handlers.GameStateManager.pushState(GameStateManager.java:49)
        at com.cookedapps.robotredemption.handlers.GameStateManager.<init>(GameStateManager.java:21)
        at com.cookedapps.robotredemption.main.Game.create(Game.java:41)
        at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.esotericsoftware.kryonet.Server" on path: DexPathList[[zip file "/data/app/com.cookedapps.robotredemption.android-2/base.apk"],nativeLibraryDirectories=[/data/app/com.cookedapps.robotredemption.android-2/lib/arm, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at com.cookedapps.robotredemption.server.GameServer.<init>(GameServer.java:20)
        at com.cookedapps.robotredemption.states.Play.<init>(Play.java:73)
        at com.cookedapps.robotredemption.handlers.GameStateManager.getState(GameStateManager.java:39)
        at com.cookedapps.robotredemption.handlers.GameStateManager.pushState(GameStateManager.java:49)
        at com.cookedapps.robotredemption.handlers.GameStateManager.<init>(GameStateManager.java:21)
        at com.cookedapps.robotredemption.main.Game.create(Game.java:41)
        at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
Suppressed: java.lang.ClassNotFoundException: com.esotericsoftware.kryonet.Server
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 10 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我在cmd和其他东西中尝试/ gradlew清洁,但没有任何帮助。 请给我一个暗示,因为这几个小时后卡住了...谢谢!

2 个答案:

答案 0 :(得分:2)

确保添加:

dependencies {
 compile files('src/libs/kryonet-2.21-all.jar') 
}

in

project(":android")
你的build.gradle文件中也是

,而不仅仅是桌面项目

答案 1 :(得分:1)

我找到了一个解决方案。我的方法不对。 如何将Kryonet添加到libgdx - Android项目:

  • 转到root build.gradle
  • 转到project(":core") - dependencies并添加:

compile "com.esotericsoftware:kryo:3.0.1" compile ("com.esotericsoftware:kryonet:2.22.0-RC1") { exclude module : 'kryo' }

  • 使用Gradle文件同步项目

重点是添加Kryonet,排除Kryo并单独添加Kryo。就像这样可以避免此异常:

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define

我希望我可以帮助这个人!