Contents

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

  1. //Configuration for the script itself (aka, listing the dependencies of the script that lists dependencies - InSCRIPTion!)
  2. buildscript {
  3. //Defines the repositories required by this script, e.g. hosting the android plugin
  4. repositories {
  5. //local maven repository (advanced use)
  6. mavenLocal()
  7. //maven central repository, needed for the android plugin
  8. mavenCentral()
  9. //snapshot repository (in case this script depends on snapshot/prerelease artifacts)
  10. maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
  11. }
  12. //Defines the artifacts this script depends on, e.g. the android plugin
  13. dependencies {
  14. //Adds the android gradle plugin as a dependency of this buildscript
  15. classpath 'com.android.tools.build:gradle:1.5.0'
  16. }
  17. }
  18. //Configuration common to all projects (:core, :desktop and :android in this example)
  19. allprojects {
  20. //Defines gradle plugins used by all projects.
  21. //A plugin extends gradle with additional tasks, configurations, etc., with defaults set according to conventions.
  22. apply plugin: "eclipse"
  23. apply plugin: "idea"
  24. //Version of your game
  25. version = "1.0"
  26. //Defines 'extra' (custom) properties for all projects
  27. ext {
  28. appName = "the-name-of-your-game"
  29. //Versions of the libGDX dependencies (used further below on those 'compile' lines)
  30. gdxVersion = "1.9.3"
  31. roboVMVersion = '2.1.0'
  32. box2DLightsVersion = '1.4'
  33. ashleyVersion = '1.7.0'
  34. aiVersion = '1.8.0'
  35. }
  36. //Defines all repositories needed for all projects
  37. repositories {
  38. mavenLocal()
  39. mavenCentral()
  40. maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
  41. maven { url "https://oss.sonatype.org/content/repositories/releases/" }
  42. }
  43. }
  44. //Configuration for the :desktop project
  45. project(":desktop") {
  46. //Uses the java plugin (provides compiling, execution, etc.).
  47. //That one is bundled with gradle, so we didn’t have to define it in the buildscript section.
  48. apply plugin: "java"
  49. //Defines dependencies for the :desktop project
  50. dependencies {
  51. //Adds dependency on the :core project as well as the gdx lwjgl backend and native dependencies
  52. implementation project(":core")
  53. implementation "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
  54. implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
  55. }
  56. }
  57. //Configuration for the :android project
  58. project(":android") {
  59. //Uses the android gradle plugin (provides compiling, copying on device, etc.)
  60. apply plugin: "android"
  61. configurations { natives }
  62. //Defines dependencies for the :android project
  63. dependencies {
  64. //Adds dependencies on the :core project as well as the android backends and all platform natives.
  65. //Note the 'natives' classifier in this project.
  66. implementation project(":core")
  67. implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
  68. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
  69. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
  70. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
  71. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
  72. }
  73. }
  74. //Configuration for the :core project
  75. project(":core") {
  76. //Uses the java gradle plugin
  77. apply plugin: "java"
  78. dependencies {
  79. //Defines dependencies for the :core project, in this example the gdx dependency
  80. implementation "com.badlogicgames.gdx:gdx:$gdxVersion"
  81. }
  82. }

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 dependencies take. If you are familiar with maven, notice the format:

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

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

As mentioned earlier, you do not need to modify the individual build.gradle files in each of the different platform-specific folders (e.g., -desktop, -ios, -core). You only need to modify 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:

  1. project(":android") {
  2. apply plugin: "android"
  3. configurations { natives }
  4. dependencies {
  5. implementation project(":core")
  6. implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
  7. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
  8. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
  9. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
  10. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
  11. }
  12. }

We know our FreeType extension has declarations:

  1. implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
  2. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
  3. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
  4. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
  5. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"

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

  1. project(":android") {
  2. apply plugin: "android"
  3. configurations { natives }
  4. dependencies {
  5. implementation project(":core")
  6. implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
  7. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
  8. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
  9. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
  10. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
  11. implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
  12. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
  13. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
  14. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
  15. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
  16. }
  17. }

And we are done, our android project now has the freetype dependency. After this you will need to refresh your dependencies. Easy eh.

libGDX Extensions

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

Box2D Gradle

Core Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

Desktop Dependency:

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

Android Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
  2. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
  3. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
  4. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
  5. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"

iOS Dependency:

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

HTML Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
  2. implementation "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"

and in ./html/src/yourgamedomain/GdxDefinition*.gwt.xml add <inherits name="com.badlogic.gdx.physics.box2d.box2d-gwt"/>


Bullet Gradle

Core Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"

Desktop Dependency:

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

Android Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
  2. natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-v7a"
  3. natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-arm64-v8a"
  4. natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86"
  5. natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86_64"

iOS Dependency:

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

HTML Dependency: Not compatible!


FreeTypeFont Gradle

Core Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"

Desktop Dependency:

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

Android Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
  2. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
  3. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
  4. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
  5. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"

iOS Dependency:

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

iOS-MOE Dependency:

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

HTML Dependency: Not compatible!


Controllers Gradle

Core Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"

Desktop Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
  2. implementation "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"

Android Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
  2. implementation "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:

  1. implementation "com.badlogicgames.gdx:gdx-controllers:$gdxVersion:sources"
  2. implementation "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion"
  3. implementation "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion:sources"

and in ./html/src/yourgamedomain/GdxDefinition*.gwt.xml add <inherits name="com.badlogic.gdx.controllers.controllers-gwt"/>


Tools Gradle

Core Dependency: Don’t put me in core!

Desktop Dependency (LWJGL2 only):

  1. implementation "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:

  1. implementation "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"

Android Dependency:

  1. implementation "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"

HTML Dependency:

  1. implementation "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion:sources"

and in ./html/src/yourgamedomain/GdxDefinition*.gwt.xml add <inherits name="Box2DLights"/>


Ashley Gradle

  • Note: This extension release cycle is not dependent on the main libGDX library, and so it is not unusual to have a new version published between two libGDX releases. If you want to pull in a new (or different) version, check https://repo1.maven.org/maven2/com/badlogicgames/ashley/ashley/ and change the ashleyVersion value in the ext section.

Core Dependency:

  1. implementation "com.badlogicgames.ashley:ashley:$ashleyVersion"

Android Dependency:

  1. implementation "com.badlogicgames.ashley:ashley:$ashleyVersion"

HTML Dependency:

  1. implementation "com.badlogicgames.ashley:ashley:$ashleyVersion:sources"

and in ./html/src/yourgamedomain/GdxDefinition*.gwt.xml add <inherits name='com.badlogic.ashley_gwt' />


AI Gradle

  • Note: This extension release cycle is not dependent on the main libGDX library, and so it is not unusual to have a new version published between two libGDX releases. If you want to pull in a new (or different) version, check https://repo1.maven.org/maven2/com/badlogicgames/gdx/gdx-ai/ and change the aiVersion value in the ext section.

Core Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-ai:$aiVersion"

Android Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-ai:$aiVersion"

HTML Dependency:

  1. implementation "com.badlogicgames.gdx:gdx-ai:$aiVersion:sources"

and in ./html/src/yourgamedomain/GdxDefinition*.gwt.xml add <inherits name='com.badlogic.gdx.ai' />


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:

  1. allprojects {
  2. repositories {
  3. // Remote Maven repo
  4. maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
  5. // Maven Central Repo
  6. mavenCentral()
  7. // Local Maven repo
  8. mavenLocal()
  9. // Remote Ivy dir
  10. ivy { url "http://some.ivy.com/repo" }
  11. // Local Ivy dir
  12. ivy { url "../local-repo" }
  13. }
  14. }

Adding Dependencies

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

  1. dependencies {
  2. implementation group: 'com.badlogicgames.gdx', name: 'gdx', version: '1.0-SNAPSHOT', classifier: 'natives-desktop'
  3. }

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

  1. dependencies {
  2. implementation 'com.badlogicgames.gdx:gdx:1.0-SNAPSHOT:natives-desktop'
  3. }

Mavenizing Local Dependencies

If you would prefer to use maven repositories to manage local .jar files, these two commands will take any local .jar file and install them (and their source) to your local maven repository.

  1. mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
  1. mvn install:install-file -Dfile=<path-to-source-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -Dclassifier=sources

To then set up gradle to include your new dependency, edit your build.gradle file in the root project directory and edit the core project entry:

  1. project(":core") {
  2. ...
  3. dependencies {
  4. ...
  5. implementation "<group-id>:<artifact-id>:<version>"
  6. implementation "<group-id>:<artifact-id>:<version>:sources"
  7. }
  8. }

After this you will need to refresh your dependencies for your IDE to see, so run:
Command line - $ ./gradlew --refresh-dependencies
Eclipse - $ ./gradlew eclipse
IntelliJ - $ ./gradlew idea

Also, don’t forget that any dependencies added this way also need to be included in the GWT inheritance file.

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:

  1. dependencies {
  2. implementation fileTree(dir: 'libs', include: '*.jar')
  3. }

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

NOTE: “dir” is relative to the project root, if you add the dependencies to your android project, ‘libs’ would need to be in the android/ directory. If you added the dependencies in the core project, ‘libs’ would need to be in the core/ directory.

An example with a more complete script:

  1. project(":android") {
  2. apply plugin: "android"
  3. configurations { natives }
  4. dependencies {
  5. implementation project(":core")
  6. implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
  7. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
  8. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
  9. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
  10. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
  11. implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
  12. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
  13. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
  14. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
  15. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
  16. implementation fileTree(dir: 'libs', include: '*.jar')
  17. }
  18. }

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.

Android Pitfall

When adding flat file dependencies to a project, for example the core project, you would need to duplicate the dependency declaration for the android project. This is because the Android Gradle plugin currently can’t handle transitive flat file dependencies.

For example, if you were to add the all the jars in your libs directory as dependencies for your project, you would need to do the following.

  1. project(":core") {
  2. ...
  3. implementation fileTree(dir: '../libs', include: '*.jar')
  4. ...
  5. }
  6. // And also
  7. project(":android") {
  8. ...
  9. implementation fileTree(dir: '../libs', include: '*.jar')
  10. ...
  11. }

This is only required for the android project, all other projects inherit flat file dependencies OK.

External Dependencies Examples

Universal-Tween-Engine by jar

  1. Download the jars
  2. Place jars in the directory core/libs (You can change this if you wish)
  3. Alter your build.gradle script in the root directory as follows:
  • Locate the :core stub where the core project’s dependencies are declared
  • Add the line in dependencies
  1. implementation fileTree(dir: 'libs', include: '*.jar')
  • Your script should now look a little like this:
  1. project(":core") {
  2. ...
  3. dependencies {
  4. ...
  5. implementation fileTree(dir: 'libs', include: '*.jar')
  6. }
  7. }

Finally Refresh your Gradle project, either on command line or using your IDE plugin.

Universal-Tween-Engine by locally mavenizing maven

First, download and extract the tween-engine-api from its repository (https://code.google.com/p/java-universal-tween-engine/). To install this dependency and its source files into your local maven repo, use these commands:

  1. mvn install:install-file -Dfile=tween-engine-api.jar -DgroupId=aurelienribon -DartifactId=tweenengine -Dversion=6.3.3 -Dpackaging=jar
  2. mvn install:install-file -Dfile=tween-engine-api-sources.jar -DgroupId=aurelienribon -DartifactId=tweenengine -Dversion=6.3.3 -Dpackaging=jar -Dclassifier=sources

With the tween engine jars in your local maven repo, add a dependency to them in your build.gradle in the root project file.

  1. project(":core") {
  2. ...
  3. dependencies {
  4. ...
  5. implementation "aurelienribon:tweenengine:6.3.3"
  6. implementation "aurelienribon:tweenengine:6.3.3:sources"
  7. }
  8. }

Add the inheritance to GdxDefinition.gwt.xml and GdxDefinitionSuperdev.gwt.xml

  1. <inherits name='aurelienribon.tweenengine'/>

Then just refresh your dependencies.

  1. $ ./gradlew --refresh-dependencies

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:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!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">
  3. <module rename-to="html">
  4. <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
  5. <inherits name='MyGameName' />
  6. <entry-point class='com.badlogic.mygame.client.HtmlLauncher' />
  7. <set-configuration-property name="gdx.assetpath" value="../android/assets" />
  8. </module>

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

libGDX Extension Inherits

These are the libGDX extensions that are supported in gwt

  • 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' />
  • Ashley - <inherits name='com.badlogic.ashley_gwt' />
  • AI - <inherits name='com.badlogic.gdx.ai' />

An example: The Universal Tween Engine

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!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">
  3. <module rename-to="html">
  4. <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
  5. <inherits name='com.badlogic.mygame.MyGame' />
  6. //Let's inherit tween
  7. <inherits name='aurelienribon.tweenengine'/>
  8. <entry-point class='com.badlogic.mygame.client.GwtLauncher' />
  9. <set-configuration-property name="gdx.assetpath" value="../android/assets" />
  10. </module>

Step by step Universal Tween Engine guide

This guide uses everything that is explained in this article to add the Universal Tween Engine to your project as a dependency.

Click me

Dependency management for libraries

If you’re creating a library that people can include in their projects via gradle, you might need to replace the implementation keyword by api. Any dependency of your library that you declare with api will be visible and usable by others that depend on your library while implementation makes it only accessible for you.