Yaniv
Yaniv

Reputation: 572

Export libgdx project as Android library including dependencies

I have two, separate Android projects. One is a regular Android application and the other one is a libgdx project. My goal is to be able to compile the libgdx project as an Android library into aar file, so I could use it in the regular, non libgdx, Android application (I'm going to start the libgdx game's activity from the regular Android project).
The libgdx project consists of several modules (I'm using only the android and the desktop modules), so in my libgdx project I can find 3 modules: android, desktop and core (where basically the whole game's code is resides). When compiling and running the game on Android, the android module kicks in, but it uses the core module as a dependency.

When trying to change the libgdx project into an Android library project and compiling it into aar, it seems like it lacks the needed dependencies (like the core module, in addition to some other dependencies).

How can I create an aar file from the libgdx project which has all the needed dependencies?

Upvotes: 3

Views: 284

Answers (1)

Yaniv
Yaniv

Reputation: 572

So eventually I managed to find a solution for my problem.
Lets start with the fact that my first impression was misleading, and the problem I had was not a libgdx specific problem, but a gradle "problem".
In short, the reason behind that is that the aar/jar files don't contain the transitive dependencies and don't have a pom file which describes the dependencies used by the library. To overcome this behaviour you need somehow to specify the dependencies in your project. You can choose between 2 approaches:

first approach:
You can use a central repository, such as JCenter, and publish the project as library. In this case, gradle will be able to download the dependencies using the pom file which contains the dependencies list.

Second approach:
You need to manually copy all your dependencies to the libs folder. You can do this relatively easy by writing a small gradle task:

task copyCompileDependenciesToLibs(type: Copy) {
    def libsPath = project.projectDir.toString() + "/libs"

    from configurations.compile
    into libsPath
}

This snippet will copy all your dependencies to the libs folder, and once you compile your library project the dependencies will be included.

Upvotes: 1

Related Questions