This is a modified stock libGDX project that uses Dagger 2.
- Dagger 2 dependency has been introduced in
build.gradle
:
-
apt
plugin has been added to build classpath - this is needed to be able to avoid includingdagger-compiler
dependency ascompile
, thus, packagingdagger-compiler
into the game.buildscript { ... dependencies { ... classpath "net.ltgt.gradle:gradle-apt-plugin:0.4" } }
-
Declaring dagger2 dependencies for
core
subproject:ext { ... daggerVersion = '2.0.2' } project(":core") { apply plugin: "java" apply plugin: "net.ltgt.apt" dependencies { ... compile "com.google.dagger:dagger:$daggerVersion" apt "com.google.dagger:dagger-compiler:$daggerVersion" } }
- dagger GWT module has been added into
html/src/dagger/Dagger.gwt.xml
. Also, in order for GWT to work, sources for dagger core andjavax.inject
packages has been re-packaged underhtml/src/dagger/emu
directory and declared in the GWT module:<?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> <source path="" /> <super-source path="emu"/> </module>
I also need to inherit from this module in core/src/MyGame.gwt.xml
.
-
html
module has been modified to add path to generated dagger sources:gwt { ... src = files(file("src/")) // Needs to be in front of "modules" below. src += files(new File(project(":core").projectDir, "/build/generated/source/apt/main")) }
-
In order to run
desktop
I had to remove standard Application configuration in IntelliJ and add Gradle configuration:This is needed in order for IntelliJ to generate dagger sources on running
desktop
. On the very first run it feels very slow, but once gradle daemon warms up it makes little to no difference with traditional Application launcher configuration. It also supports hot swap.
That's pretty much it. I have tested this configuration on desktop, ios, android, html (both superDev and dist). If you're aware of a better way of incorporating dagger 2, let me know.