ghita
ghita

Reputation: 2796

How to add libgdx to existing project

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.

Upvotes: 11

Views: 3826

Answers (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);
    }
}

Upvotes: 2

seb-o-matic
seb-o-matic

Reputation: 25

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.

Upvotes: 1

Related Questions