简体   繁体   English

无法为目标加载共享库“gdx”

[英]Couldn't load shared library 'gdx' for target

I'm having the same issue as in this question , but the answers there doesn't solve my problem.我遇到了与这个问题相同的问题,但那里的答案并没有解决我的问题。

I didn't create project by gdxsetup.jar, I just included gdx.jar and gdx-backend-android.jar.我没有通过 gdxsetup.jar 创建项目,我只是包含了 gdx.jar 和 gdx-backend-android.jar。 I added the libgdx.so to libs/x86, but it stills throws an exception.我将 libgdx.so 添加到 libs/x86,但它仍然会抛出异常。 How should I solve this?我应该如何解决这个问题?

Logcat:日志:

02-16 11:59:45.604: E/AndroidRuntime(14788): FATAL EXCEPTION: main
02-16 11:59:45.604: E/AndroidRuntime(14788): java.lang.ExceptionInInitializerError
02-16 11:59:45.604: E/AndroidRuntime(14788):    at java.lang.Class.newInstanceImpl(Native Method)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at java.lang.Class.newInstance(Class.java:1130)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.os.Looper.loop(Looper.java:176)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at android.app.ActivityThread.main(ActivityThread.java:5419)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at java.lang.reflect.Method.invokeNative(Native Method)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at java.lang.reflect.Method.invoke(Method.java:525)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at dalvik.system.NativeStart.main(Native Method)
02-16 11:59:45.604: E/AndroidRuntime(14788): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load shared library 'gdx' for target: Linux, 32-bit
02-16 11:59:45.604: E/AndroidRuntime(14788):    at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:114)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:34)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:62)
02-16 11:59:45.604: E/AndroidRuntime(14788):    ... 15 more
02-16 11:59:45.604: E/AndroidRuntime(14788): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load gdx from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.gamr-1.apk,libraryPath=/data/app-lib/com.example.gamr-1]: findLibrary returned null
02-16 11:59:45.604: E/AndroidRuntime(14788):    at java.lang.Runtime.loadLibrary(Runtime.java:355)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at java.lang.System.loadLibrary(System.java:525)
02-16 11:59:45.604: E/AndroidRuntime(14788):    at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:110)
02-16 11:59:45.604: E/AndroidRuntime(14788):    ... 17 more

If it helps, I'm running 32-bit Linux.如果有帮助,我正在运行 32 位 Linux。

The problem I had was that for some reason libgdx.so was not copied to any of the armeabi , armeabi-v7a or x86 folders in the android project's lib folder.我遇到的问题是由于某种原因libgdx.so没有复制到 android 项目的lib文件夹中的任何armeabiarmeabi-v7ax86文件夹。

Copying these over from the libgdx distribution worked for me.从 libgdx 发行版复制这些对我有用。

My problem was that I was trying to make my GDX app within a shared library (aka, not the thing that gets compiled into an APK), but hadn't finished setting up all the GDX-including stuff in my lib.我的问题是我试图在共享库中制作我的 GDX 应用程序(也就是,不是编译成 APK 的东西),但还没有完成在我的库中设置所有包含 GDX 的东西。

So I had:所以我有:

MyProject
-->MyMainApp
-->-->build.gradle <-- no updates required, doesn't do anything with GDX
-->MySharedLibraryWhereMyGameEngineIs
-->-->build.gradle <-- this is where the problem was

In the shared lib's build.gradle, I hadn't included the sourceSets parameter.在共享库的 build.gradle 中,我没有包含sourceSets参数。

Adding it fixed my problem.添加它解决了我的问题。 GDX now starts up successfully. GDX 现在成功启动。

apply plugin: 'com.android.library'
android {
    ... config stuff ...

    sourceSets {                       // this wasn't here before
        main {                         // this wasn't here before
            jniLibs.srcDirs = ['libs'] // this wasn't here before
        }                              // this wasn't here before
        instrumentTest.setRoot('tests')// this wasn't here before
    }     

    ... a bunch of other config stuff ...
}

I actually just faced the same problem after I uploaded by first update to my app ("October Bro").在我通过第一次更新上传到我的应用程序(“October Bro”)之后,我实际上只是遇到了同样的问题。 While the first release has been run perfectly when installed from the Play Store, the update crashed right after launching.虽然从 Play 商店安装第一个版本时运行良好,但更新启动后立即崩溃。 Checking Logcat, I exactly have been facing this issue.检查 Logcat,我确实遇到了这个问题。

The reason why my first release worked without a problem, but not the update was simple: because I only wanted to fix a minor bug, I checked out the project from GitHub, only ran the Desktop app once to verify the fix, and then created the signed AppBundle right away.我的第一个版本没有问题,但没有更新的原因很简单:因为我只想修复一个小错误,我从 GitHub 上检出了项目,只运行了一次桌面应用程序来验证修复,然后创建立即签署的 AppBundle。 It looks like I simply had to run the "android" run configuration at least once, before creating the AppBundle.看起来我只需要在创建 AppBundle 之前至少运行一次“android”运行配置。 Or to be more precise, the build.grade of the android module, which performs these tasks:或者更准确地说,是执行以下任务的android模块的build.grade

// android/build.gradle

...

// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives {
    doFirst {
        file("libs/armeabi/").mkdirs()
        file("libs/armeabi-v7a/").mkdirs()
        file("libs/arm64-v8a/").mkdirs()
        file("libs/x86_64/").mkdirs()
        file("libs/x86/").mkdirs()

        configurations.natives.files.each { jar ->
            def outputDir = null
            if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
            if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
            if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
            if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
            if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
            if(outputDir != null) {
                copy {
                    from zipTree(jar)
                    into outputDir
                    include "*.so"
                }
            }
        }
    }
}

I thought that this android/build.gradle is executed anyways once I hit Build > Build Bundle(s) / APK(s) .我认为这个android/build.gradle无论如何都会在我点击Build > Build Bundle(s) / APK(s) 后执行 But that does not seem to be the case.但情况似乎并非如此。

I was able to fix the problem by removing armeabi natives from the copyAndroidNatives task我能够通过从copyAndroidNatives任务中删除armeabi natives 来解决问题

// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives {
    doFirst {
        // file("libs/armeabi/").mkdirs()
        file("libs/armeabi-v7a/").mkdirs()
        file("libs/arm64-v8a/").mkdirs()
        file("libs/x86_64/").mkdirs()
        file("libs/x86/").mkdirs()

        configurations.natives.copy().files.each { jar ->
            def outputDir = null
            if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
            if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
            // if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
            if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
            if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
            if(outputDir != null) {
                copy {
                    from zipTree(jar)
                    into outputDir
                    include "*.so"
                }
            }
        }
    }
}

我发现这很简单:添加

static { System.loadLibrary("gdx");}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM