Dependency management with Gradle - garsue/libgdx GitHub Wiki

Contents

  • [Useful links] (#useful-links)
  • [Guide to build.gradle] (#guide-to-buildgradle)
  • [Libgdx Dependencies] (#libgdx-dependencies)
  • [Available libgdx extensions] (#libgdx-extensions)
  • [External Dependencies] (#external-dependencies)
  • [Adding Repositories] (#adding-external-repositories)
  • [Examples] (#adding-dependencies)
  • [File Dependencies] (#file-dependencies)
  • [Declaring Dependencies with HTML] (#gwt-inheritance)
  • [Libgdx Extension Inherits] (#libgdx-extension-inherits)

Useful links

Dependency management with Gradle is easy to understand, and has many different approaches. If you are familiar with Maven or Ivy, Gradle is fully compatible with both approaches, as well as being able to support custom approaches. If you aren't familiar with Gradle, there are great resources on their site to learn, it is recommended you give them a read to get comfortable with Gradle.

Guide to build.gradle

Gradle projects are managed by build.gradle files in their root directory. If you have used the gdx-setup.jar to build your libgdx project you will notice the structure: Structure Example

The root directory, and each sub directory contains a build.gradle file, for clarity we will define the dependencies in the root directory's build.gradle file. (Note it can be done in each of the build.gradle scripts in the sub directories, it is just cleaner and easier to follow when it is handled all in one place)

Here is a small section of the default buildscript that is generated from the setup:

Full script you will see will differ slightly depending on what other modules you have, see here

buildscript {
    //Defines the repositories that are required by this script, e.g. android plugin
    repositories {
        //maven central repository, needed for the android plugin
        mavenCentral()
        //repository for libgdx artifacts
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    //
    dependencies {
        //Adds the android gradle plugin as a dependency of this buildscript
        classpath 'com.android.tools.build:gradle:0.9+'
    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"
    
    version = "1.0"
    ext {
        appName = "%APP_NAME%"
        gdxVersion = "1.0-SNAPSHOT"
        roboVMVersion = "0.0.10"
    }
    
    repositories {
        //Defines all repositories needed for all projects
        mavenLocal();
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
}

project(":core") {
    apply plugin: "java"
    
    dependencies {
        //Defines dependencies for the :core project, in this example the gdx depdendency
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}

project(":desktop") {
    apply plugin: "java"
    
    dependencies {
    //Defines dependencies for the :desktop project, adds dependency on the :core project as well as 
    //The gdx lwjgl backend and native dependencies
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        //defines dependencies for the :android project, depends on the :core project,
        //and also the android backends and all platform natives. Note the 'natives' classifier
        //in this project
        compile project(":core")
        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"
    }
}

Libgdx Dependencies

Dependencies are configured in the root build.gradle file as shown in the build.gradle guide above. In order to add an external dependency to a project, you must declare the dependency correctly under the correct part of the build.script.

(Some) Libgdx extensions are mavenized and pushed to the maven repo, which means we can very easily pull them into our projects from the build.gradle file. You can see in the list below of the format that these depdencies take. If you are familiar with maven, notice the format:

compile '<groupId>:<artifactId>:<version>:<classifier>'

Let's take a quick example to see how this works with the root build.gradle file.

Here we see the dependencies for the FreeType Extension, say we want our Android project to have this dependency. We locate our project(":android") stub in the root directory's build.gradle:

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile project(":core")
        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"
    }
}

We know our FreeType extension has declarations:

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-x86"

So all we need to do is whack it in the dependencies stub

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile project(":core")
        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-x86"
    }
}

And we are done, our android project now has the freetype dependency. Easy eh.

Libgdx Extensions

Mavenized libgdx extensions ready to import from the build.gradle script include:

  • [Box2D] (#box2d-gradle)
  • [Bullet] (#bullet-gradle)
  • [FreeTypeFont] (#freetypefont-gradle)
  • Controllers
  • [Tools] (#tools-gradle)
  • [Box2DLights] (#box2dlights-gradle)

Box2D Gradle

Core Dependency:

compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

Desktop Dependency:

compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"

Android Dependency:

compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"

iOS Dependency:

natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"

HTML Dependency:

compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"

Bullet Gradle

Core Dependency:

compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"

Desktop Dependency:

compile "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-desktop"

Android Dependency:

compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-x86"

iOS Dependency:

compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-ios"

HTML Dependency: Not compatible!


FreeTypeFont Gradle

Core Dependency:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"

Desktop Dependency:

compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"

Android Dependency:

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-armeabi-x86"

iOS Dependency:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"

HTML Dependency: Not compatible!


Controllers Gradle

Core Dependency:

compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"

Desktop Dependency:

compile "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"

Android Dependency:

compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-android:$gdxVersion"

iOS Dependency: Not supported, but you can still compile and run your iOS app. Controllers just won't be available

HTML Dependency:

compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion:sources"

Tools Gradle

Core Dependency:

compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"

Desktop Dependency:

compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"

Android Dependency: Not compatible!

iOS Dependency: Not compatible!

HTML Dependency: Not compatible!


Box2DLights Gradle

  • Note: this extension also requires the Box2D extension

Core Dependency:

compile "com.badlogicgames.box2dlights:box2dlights:1.1"

Android Dependency:

compile "com.badlogicgames.box2dlights:box2dlights:1.1"

HTML Dependency:

compile "com.badlogicgames.box2dlights:box2dlights:1.1:sources"

External Dependencies

Adding external repositories

Gradle finds files defined as dependencies by looking through all the repositories defined in the buildscript. Gradle understands several repository formats, which include Maven and Ivy.

Under the allprojects stub, you can see how repositories are defined. Here is an example:

allprojects {    
    repositories {
        // Remote Maven repo
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 
        // Maven Central Repo
        mavenCentral()
        // Local Maven repo
        mavenLocal()
        // Remote Ivy dir
        ivy { url "http://some.ivy.com/repo" }
        // Local Ivy dir
        ivy { url "../local-repo" }
    }
}

Adding Dependencies

External dependencies are identified by their group, name, version and sometimes classifier attributes.

dependencies {
    compile group: 'com.badlogicgames.gdx', name: 'gdx', version: '1.0-SNAPSHOT', classifier: 'natives-desktop'
}

Gradle allows you to use shortcuts when defining external dependencies, the above configuration is the same as:

dependencies {
    compile 'com.badlogicgames.gdx:gdx:1.0-SNAPSHOT:natives-desktop'
}

File Dependencies

If you have a dependency that is not mavenized, you can still depend on them!

To do this, in your project stub in the root build.gradle file, locate the dependencies { } section as always, and add the following:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

This will include all the .jar files in the libs directory as dependencies.

An example with a fuller script:

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile project(":core")
        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-x86"
        compile fileTree(dir: 'libs', include: '*.jar')
    }
}

It is worth nothing that these file dependencies are not included in the published dependency descriptor for your project, but they are included in transitive project dependencies within the same build.

Gwt Inheritance

Gwt is special, so in order to let the GWT compiler know what modules the project depends on, and inherits from, you need to let it know.

This is done in the gwt.xml files in the gwt sub directory. You will need to make the changes both to the GdxDefinition.gwt.xml and also the GdxDefinitionSuperdev.gwt.xml.

The default gwt.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd">
<module rename-to="html">
	<inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
	<inherits name='com.badlogic.mygame.MyGame' />
	<entry-point class='com.badlogic.mygame.client.GwtLauncher' />
	
	<set-configuration-property name="gdx.assetpath" value="../android/assets" />
</module>

We depend on the libgdx gwt backend, as well as the core project, so we have them defined in an tag. So when you add your dependency via methods above, you need to add it here too!

Libgdx Extension Inherits

  • Libgdx Core - <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
  • Box2d - <inherits name='com.badlogic.gdx.physics.box2d.box2d-gwt' />
  • Box2dLights - <inherits name='Box2DLights' />
  • Controllers - <inherits name='com.badlogic.gdx.controllers.controllers-gwt' />

An example: The Universal Tween Engine

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd">
<module rename-to="html">
	<inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
	<inherits name='com.badlogic.mygame.MyGame' />
	//Let's inherit tween
	<inherits name='aurelienribon.tweenengine'/>
	<entry-point class='com.badlogic.mygame.client.GwtLauncher' />
	
	<set-configuration-property name="gdx.assetpath" value="../android/assets" />
</module>

List of libgdx extensions that support gwt....

⚠️ **GitHub.com Fallback** ⚠️