Libgdx库集成在原生android项目中

时间:2017-02-17 10:06:48

标签: android 3d libgdx

我正在开发一个包含一个小型3D游戏的Android应用程序。在这里,我使用Libgdx框架(使用gdx-setup.jar)来开发我的3D模型。由于它使用原生的android功能,我需要使用libgdx作为android应用程序内的库。         我按照libgdx / wiki的说明进行操作

所以我从http://libgdx.badlogicgames.com/nightlies/下载了libgdx-nightly-latest.zip

并在libs文件夹中放置 gdx.jar,gdx-backend-android.jar,gdx-natives.jar,armeabi和armeabi-v7a文件夹。然后我将它们添加为库。

My Gradle依赖项:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile files('libs/gdx-backend-android.jar')
compile files('libs/gdx.jar')
compile files('libs/x86.jar')
compile files('libs/gdx-natives.jar')
}

MainActivity类

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (Button) findViewById(R.id.button);

btnPlay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), GameActivity.class);
        startActivity(intent);
    }
});
}

GameActivity类

public class GameActivity extends AndroidApplication {

GdxGame gdxGame;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gdxGame = new GdxGame();
    AndroidApplicationConfiguration config = new      AndroidApplicationConfiguration();
    initializeForView(gdxGame, config);
}
}

GdxGame是我的ApplicationListener类。 当我运行此代码时,应用程序崩溃时出现此错误:

java.lang.ExceptionInInitializerError
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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:124)
at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:33)
at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:63)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)

任何人都可以帮忙解决这个问题吗?我错过了什么吗? 谢谢。

project library structure and gradle

1 个答案:

答案 0 :(得分:2)

您只将.jar文件添加为依赖项。有一些针对不同处理器体系结构的.so文件,如arm64-v8aarmeabiarmeabi-v7ax86x86_64。你需要添加它们。将所有.so文件保存在libs文件夹中。

在soureSets中添加jniLibs.srcDirs = ['libs']

android的必需库

armeabi/libgdx.so
armeabi-v7a/libgdx.so
x86/libgdx.so
gdx-backend-android.jar
gdx-backend-android-sources.jar

像这样编译:

  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"