11

I have provided an aplication made in android that has a navigation drawer and in it has a list of games. I have to create another game and to put it there. The game that has to be created by my must use libGDX but the original application didn't use this library.

Is it possible to do this ? If Yes, how can I add the libgdx to the exiting project. On github I found only how to start a new project with libGDx, an not how to add it to exising code. thanks.

ghita
  • 2,746
  • 7
  • 30
  • 54

2 Answers2

2

You can achieve this with these 2 steps:

1. Add LibGDX to your project

On your android gradle file: build.gradle (app or android module)

dependencies {
    ...

    // Add this line, it is recommended to use the latest LibGDX version
    api "com.badlogicgames.gdx:gdx-backend-android:1.9.10"
}

2. Initialize LibGDX for a view or use a LibGDX managed activity

Option 1: Initialize for a view
Since you have a navigation drawer and more code native to android this option fits your needs better. From this question (How to add LibGDX as a sub view in android):

The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.

-- Matsemann

Also here's documentation on how to implement this (Fragment based LibGDX).

Option 2: Use a managed activity
This is the default/most common use of LibGDX, you will need to change your code as follows:

  • Your activity needs to extend Android application:
public class MainActivity extends AndroidApplication {
  • Create a class extending Game or ApplicationAdapter:
import com.badlogic.gdx.Game;

public class LibGDXGame extends Game {

    @Override
    public void create() {
        System.out.println("Success!");
    }
}
  • Initialize the LibGDX managed activity:
import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class MainActivity extends AndroidApplication {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new LibGDXGame(), config);
    }
}
  • You also need to copy the native libs into your build. There’s a script that does that in the Android/build.gradle of a project made with the Libgdx Setup GUI. And you need to specify the Android native libraries in gradle as well. – Tenfour04 Feb 26 '20 at 13:20
  • How can the JNI code possibly run without the libraries? The instant you call `initialize` to start Gdx, it calls System.loadLibrary and will crash if the appropriate .so file is missing. – Tenfour04 Feb 26 '20 at 23:36
  • 1
    Using your steps (option 2) I encountered an exception: "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) ... Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: library "/system/product/lib/libgdx.so" needed or dlopened by "/apex/com.android.runtime/lib/libnativeloader.so" is not accessible for the namespace "classloader-namespace" – Sergio Aug 17 '20 at 17:44
  • Yes, you're right, I just tested my option 2 and it crashed, somehow it worked when I first answered the question, tho I tried it before posting. The solution would be to do as Tenfour04 states, sadly, I've never done it that way and don't know how to do it. – Luis Fernando Frontanilla Aug 18 '20 at 21:36
  • 1
    Here is an updated link or anyone looking for the FragmentActivity (subview) documentation: https://libgdx.com/wiki/app/starter-classes-and-configuration – jbcaveman Apr 10 '22 at 16:12
1

Generally what you want is possible, I reckon your best bet would be to create a new libgdx project with their GUI and to then manually merge the files that are needed.

  • well.. their project, is a little bigger, they take the score from my game and put it in some of database, my game game when start, takes an userID provided by them,,, I'm not sure that would be a good idea to start a new libGDX project, I have only to do my game, and I ask if it is possible to use libGDX only in my game, separated from their project.. – ghita Jun 15 '15 at 07:55
  • What exactly is going on? Who is creating the libgdx part, who is creating the Database and who has the app that needs the game to be integrated in it? – seb-o-matic Jun 15 '15 at 07:57
  • I am creating the libdgx game, the database and the app that will have the game integrated in it is provided by a client. the only question is if it is possible to have libgdx only in my game, and not in the entire project and how exactly to do it – ghita Jun 15 '15 at 08:02
  • Well libGDX is a dependency, so it needs to be included when the app is shipped because a part of the app (your game) needs it to run. The rest of the app can run independently from libGDX and the game you add. – seb-o-matic Jun 15 '15 at 08:05
  • "The rest of the app can run independently from libGDX and the game you add." this is what I wanted to hear ! can you give me an advice of how to add libgdx to the existing project ? so I can use it in my game ? – ghita Jun 15 '15 at 08:08
  • Well I thought you knew that, when you add a library to your project, it doesn't influence the code you already have. My advice would still be to create a libgdx project from scratch and then extract the files you need. – seb-o-matic Jun 15 '15 at 08:10
  • and what would be the advantage if I create a new libgdx project and then to copy paste it to my game app, and not to create it direct in my game app ? not ironic, just asking – ghita Jun 15 '15 at 08:14
  • They provide some kind of GUI that you can use to create your project. It sets up the file/directory structure, downloads the libraries you need and provides the gradle files that you need for it to compile. Also it sets up the basic construct and your default methods so that you know directly where to put your code and can get into coding the game rather than bothering with setting up libgdx. – seb-o-matic Jun 15 '15 at 08:18
  • thank you for your answers. I choose to add the libgdx library to the existing project, so what I have to do is to add the gdx-setup file to the libs folder ? – ghita Jun 15 '15 at 09:17
  • No, the gdx-setup.jar is the GUI I was mentioning. It is an executable jar that displays an interface that lets you create a new project from scratch as I proposed. As I don't know your project structure I can't help you much further as to say you need to get the actual libgdx jar and all its dependencies and add them to your project – seb-o-matic Jun 15 '15 at 09:32
  • know I have a better idea about what I have to do. Thanks you very much ! – ghita Jun 15 '15 at 10:37