Android 工作室 | Libgdx |添加 Kryonet 库 - NoClassDefFoundError

Android Studio | Libgdx | Adding Kryonet Library - NoClassDefFoundError

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

在 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 clean,但没有任何帮助。 请给我一个提示,我已经用了几个小时了。谢谢!

确保添加:

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

project(":android")

也在您的 build.gradle 文件中,而不仅仅是桌面项目

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

  • 转到根目录 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

我希望我能帮到别人!