But I'm still not a fan of Gradle

So, I've wanted to switch to Android Studio for a while. Eclipse as an IDE is a great tool, but largely, it seems to be showing its age. I've just not had great experience with it before, and trying to configure plugins, etc. Plus, Android Studio offered a lot of new features that looked cool.

Problem: Configuring builds to use Gradle.

JSword is going to be used for most of the backend functionality, MinimalBible is the user interface driving everything. The JSword library is built using Ant, and I have another Ant build responsible for tweaking the original library (currently it just shades dependencies and builds an uberjar).

So my build of JSword was going just fine, but I wanted to have a one-click build of the entire MinimalBible app (and eventually use something like Travis). That means getting a build of JSword working alongside the main build.

Originally, it would have made the most sense to stick with Ant builds and re-use targets from that. But me being adventurous and wanting to try the new shiny tooling, I gave Gradle a shot. The results really didn't pan out.

To be honest, I don't really remember why I gave Android Studio a second shot. But I did, and got a preliminary build working after an hour or two. I knew the build wasn't perfect, but developing in Android Studio was now a live option.

The next step was trying to get a build working on Windows. That wasn't incredibly challenging, but Windows was complaining about a builtBy clause in the dependencies. I'm not sure why Linux didn't complain the same way, but oh well. So at this point, the build is working on Windows as well.

The final problem was the bootstrap build. All the builds prior succeeded because the jsword.jar file already existed in the libs/ folder of the project. When you deleted the JAR and tried to build it would fail. But if you tried to rebuild, the JAR already existed as part of the last build, and so the current build would succeed. This, however, would lead to a build failure on any sane CI system, plus it's ugly.

So, I did what any sane developer would do after failing to understand the Gradle build system, and posted a StackOverflow question. As of the time of writing, there were no answers, but I did get it figured out on my own.

What follows next is not an explanation of why the code works, but more so a "here's what I did and it fixed my problem." There were two big changes to make:

Main app:

The way the build originally worked was that a task ran a stubbed Gradle build file, and this build file imported the Ant targets from my build of JSword (the idea I used is over here). This is problematic since Gradle is then unable to track what is generated from this (and I assume this is the reason why the new JAR wasn't added as a dependency).

The new way of doing things is to run that build as part of a different Gradle project, and export the artifacts of that project. So basically instead of the main app being responsible for building the external artifact, the main app just depends on what my JSword build produces. You can find the exact changes over here

JSword build:

My build of JSword is now responsible for exporting the artifact from Gradle. However, I still want to depend on Ant targets, since that's what the underlying library actually uses. In the future, I might switch to pure Gradle, but for now, it's working. So, I added an artifacts section, made a specific configuration, and then the artifact now depends on the task which actually generates the JAR file for JSword.

So the first iteration of the build was complete, and I could now build the entire MinimalBible app using Gradle, without including the jsword.jar in the app itself. Daringly so, I set up my git tree in another location to guarantee everything was working correctly. Turns out I forgot to add the artifact dependency on the build task. I fixed that here.

Wrapping up

But now the entire build works correctly, and I can use Gradle! All told, it took me a lot longer than I'd like to get everything set up. With great power comes great responsibility, and honestly, the Gradle documentation was of very little help in trying to get my build set up. I did eventually get a solution (and a good solution at that), and I'm glad I put the time in to make the switch - it seems everyone is migrating to Android Studio, and trying to do this in the future after having set up an Ant build for MinimalBible would have been a nightmare. Starting with Gradle I think will make things easier.

So Android Studio is now working. I have a good, clean, build of MinimalBible going that I should be able to evolve as my needs change. I still really don't like Gradle, but I can understand that when you're working with an Ant build with ~1500 lines you need a build system as complex as Gradle. At this point, we'll call it good.

Robolectric + Android Lollipop

Getting actual Android code running on your computer----------------------------------------------------So far in the life of [MinimalBib...… Continue reading

Android & Jacoco

Published on January 01, 2015

Boundary Value Testing

Published on November 07, 2014