1

Problem

I am developing a game using libGDX with a project structure similar to:

Root project 'gdx-play'
+--- Project ':android'
+--- Project ':core'
\--- Project ':desktop'

Subprojects android and desktop depend on core.

I want to keep my assets in the resources folder of the core project. Depending on core from desktop works just fine, as libGDX is happy loading the assets from the packaged JAR root. On Android, however, libGDX expects the assets to reside in the assets folder of the packaged Apk.

How best can I configure my multi-project build without tight coupling among projects?

Possible solutions

One

Store the assets in an assets subfolder in the core project, so that they magically end up in the assets folder of the Apk when packaged.

Two

Publish two separate JARs from core; one with classes and one with resources. Or separate the core project into two. Then, in android, somehow add the items in the resources JAR to the Android plugin's assets SourceDirectorySet and remove that JAR from the classpath (so that its items are not redundantly included into the APK). I don't know if this is dangerous.

Three

Add the android-library plugin to the core project and export an AAR. I'm sure this would be most compatible with android, however, I do not know how I would then get a normal JAR for the desktop project.

Andrew McKinlay
  • 431
  • 3
  • 15

1 Answers1

0

I would take one of two approaches. Either @jake-wharton's approach to creating jars from APK's in this similar question that, basically, involves creating JAR tasks for each release variant of the library (skipping over debug variants).

Or I would pull the assets into their own Android Library, like core-assets, leaving behind only java code in core. One benefit of this approach is that pure Java projects are easier to thoroughly test. Android's testing support is a bit lacking, at the moment.

Community
  • 1
  • 1
gMale
  • 17,147
  • 17
  • 91
  • 116
  • Why do you skip over debug variants? – Andrew McKinlay Mar 25 '14 at 08:19
  • Also, if I depend on an `android-library` project from a plain old Java project, will I have to make a new configuration to avoid importing the `AAR` file into my classpath? – Andrew McKinlay Mar 25 '14 at 08:21
  • Good questions. Typically, you wouldn't be interested in the "Debug" variants for JARs. For APKs, those variants are "debuggable" and signed with different certs. There is no analogy for JARs so it usually makes sense to just use "release" builds. – gMale Mar 25 '14 at 17:15
  • For your second question, AAR files are just zip files. Unzip them and you'll find the classes.jar, assets & res directories. However, it's probably easier to take [the first suggestion](http://stackoverflow.com/questions/19307341/android-library-gradle-release-jar) and create a JAR that the Java project uses and an APK that the android project uses. If this were my project, I'd probably follow your "first solution" (along with good naming conventions to avoid collisions) because it seems to be the simplest alternative. – gMale Mar 25 '14 at 17:31
  • I tried the first solution, but the `PACKAGE-INF` from the assets JAR gets included under the `assets` folder in the APK. Is there any way to avoid this? – Andrew McKinlay Mar 25 '14 at 23:08
  • You can try excluding things via packaging options `android { packagingOptions { exclude 'PACKAGE-INF' } }` here's a random example of [code that does something like this](http://code.ohloh.net/file?fid=ViTw3Hed7uloqZgc6LZ1lcf_BrA&cid=sthQr3UpzdI&s=android%20%22packagingOptions%22&filterChecked=true&fp=306234&mp,=1&ml=1&me=1&md=1&projSelected=true#L0). – gMale Mar 26 '14 at 17:40
  • I was talking about the solution that does not involve the `android-library` plugin. But I think I found the solution. I think Gradle allows exclude patterns in dependency configuration closures. – Andrew McKinlay Apr 01 '14 at 03:29