将Mapbox sdk添加到build.gradle后无法运行Libgdx游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将Mapbox sdk添加到build.gradle后无法运行Libgdx游戏相关的知识,希望对你有一定的参考价值。

我在logcat中收到此错误:

12-27 17:39:51.213 3365-3365/com.mksolutions.platformerap E/androidRuntime: FATAL EXCEPTION: main
 Process: com.mksolutions.platformerap, PID: 3365
 java.lang.ExceptionInInitializerError
     at java.lang.Class.newInstance(Native Method)
     at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2781)
     at android.app.ActivityThread.-wrap12(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1508)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:241)
     at android.app.ActivityThread.main(ActivityThread.java:6274)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load shared library 'gdx' for target: Linux, 32-bit
     at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:125)
     at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:33)
     at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:63)
     at java.lang.Class.newInstance(Native Method) 
     at android.app.Instrumentation.newActivity(Instrumentation.java:1079) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2781) 
     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1508) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:241) 
     at android.app.ActivityThread.main(ActivityThread.java:6274) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
  Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.mksolutions.platformerap-2/base.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.mksolutions.platformerap-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.mksolutions.platformerap-2/lib/arm64, /data/app/com.mksolutions.platformerap-2/base.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_dependencies_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_0_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_1_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_2_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_3_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_4_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_5_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_6_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_7_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_8_apk.apk!/lib/arm64-v8a, /data/app/com.mksolutions.platformerap-2/split_lib_slice_9_apk.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]] couldn't find "libgdx.so"
     at java.lang.Runtime.loadLibrary0(Runtime.java:984)
     at java.lang.System.loadLibrary(System.java:1530)
     at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:119)
     at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:33) 
     at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:63) 
     at java.lang.Class.newInstance(Native Method) 
     at android.app.Instrumentation.newActivity(Instrumentation.java:1079) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2781) 
     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1508) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:241) 
     at android.app.ActivityThread.main(ActivityThread.java:6274) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

添加我的Mapbox实现后,我开始收到此错误。更确切地说,我的build.gradle依赖项中的这些行会产生错误:

compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.2.0@aar') {
    transitive = true
}

一旦我按下启动游戏的按钮,应用程序就会崩溃。按下按钮后,此代码运行:

Intent intent = new Intent(getActivity(), PlatformerLauncher.class);
getActivity().startActivity(intent);

而这反过来运行这个类:

public class PlatformerLauncher extends AndroidApplication {

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new GamePlatformer(), config);
   }
}

GamePlatformer的内容:

public class GamePlatformer extends Game{
public SpriteBatch batcher;
@Override
public void create() {
    Gdx.input.setCatchBackKey(true);
    batcher = new SpriteBatch();
    //load assets
    Assets.load();

    //draw game
    setScreen(new PlatformerGameScreen(this));
}

@Override
public void render()
{
    super.render();
}
}

最后是完整的build.gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.mksolutions.platformerap"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }
}
configurations { natives }
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.2.0@aar') {
        transitive = true
    }

    compile project(':common')
    compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"

    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"

    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.github.kenglxn.QRGen:android:2.3.0'
    compile 'com.google.android.gms:play-services-vision:11.0.4'
    compile 'com.google.android.gms:play-services-location:11.0.4'
    testCompile 'junit:junit:4.12'
}

task copyAndroidNatives() {
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();
    configurations.natives.files.each { jar ->
        def outputDir = null
        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.jar")) outputDir = file("libs/x86")
        if (outputDir != null) {
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}

有人对我做错了什么有建议吗?

答案

发现android设备试图访问不存在的库。

解决方案:

将此添加到build.gradle中的defaultConfig:

    defaultConfig {
        ...
        ndk {
            abiFilters "armeabi-v7a", "x86", "armeabi"
        }
    }

并在gradle.properties中添加此行:

android.useDeprecatedNdk=true

重新同步gradle。

以上是关于将Mapbox sdk添加到build.gradle后无法运行Libgdx游戏的主要内容,如果未能解决你的问题,请参考以下文章

将标记 infoWindow 添加到 Mapbox 注释

在 Mapbox 的导航视图上添加自定义按钮

mapbox gl 怎样添加文字marker

从地理服务器将 wmts 图层添加到 mapbox gl

MapBox iOS SDK - 第一步

Mapbox 地图SDK极速集成指导