MinimalBible.github.io/_posts/2014-04-22-xml-is-a-terrible-programming-language.md
2014-04-29 17:39:47 -04:00

56 lines
6.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
layout: post
title: "XML is a terrible programming language"
modified: 2014-04-29 17:32:29 -0400
tags: [xml, ant, gradle, maven, dependency management, android studio]
image:
feature:
credit:
creditlink:
comments:
share:
---
_But Gradle is worse_
So, one of the first goals in building MinimalBible was getting the underlying library building correctly. Im using [jSword][1] to drive most of the underlying functionality of the app downloading Bibles, searching Bibles, displaying the text, etc. I have incredibly little experience with Java build systems, but in my experience prior, I had worked with Maven a bit. So when I noticed a pom.xml file, I thought I had it made. Turns out the pom.xml was outdated, and for an old build of the library.
Looking at [And-bible][2] was one of the things that first clued me in that I was approaching this the wrong way (the fact that the build was producing a jsword-1.6.jar when I knew the build was at least at version 2.1 was the other). The build.xml file also looked important, so I decided to do some more research. Heres what Ive learned.
There are a lot of different systems to build Java projects (I come from Python/C). The three most prevalent are [Gradle][3], [Ant][4], and [Maven][5]. By default, Android projects (using [Android Development Tools in Eclipse][6]) use none of these. Small-scale Android apps that dont rely on a lot of extra libraries or other projects simply dont need the level of functionality provided by any of these. But for the apps that do need extra functionality, youre going to need to put a build system in place.
Ill start with Maven, since it seems to be the least used in the Android world. Theres a [tutorial][7] and a [plugin][8] for doing it, but on the whole I didnt see a whole lot of information on how to use it. That being said, the Maven project retains a [central repository][9] of Java libraries that all the other build systems rely on. This way, you can declare dependencies on other libraries, and have the build system automatically download and install them. Lovely.
Ant seems to be the most well-used (if soon to be phased-out) build system in use with Android. ADT has support for generating an Ant build file, and Google has info on how to use Ant with ADT [online][10]. However, Ant in and of itself does not have the dependency management that Maven and Gradle do. For that, we rely on a different library known as [Ivy][11]. This way, while Ant handles the build process, Ant can invoke Ivy to fetch all the needed libraries before actually compiling. This build system is what is in use by jSword, and they have a fairly nice build set up.
Gradle is the final build toolkit Ill talk about. First things first, Gradle is **powerful**. Their manifesto even [says so][12]: “Make the impossible possible, make the possible easy and make the easy elegant.” Gradle is also the build system used by [Android Studio][13], the IDE that will eventually replace ADT. So starting as a new project, I thought it would be a good idea to give Android Studio/Gradle a spin. This is eventually going to be the way of the future.
Getting the initial Eclipse project imported wasnt too complicated, the documentation for doing that [is pretty simple][14]. I generated my build.gradle file, and imported the project. And thats about as far as I got.
The first order of business was making sure that I could build the jSword project using the new Gradle system. Building the Android project itself Im sure is incredibly simple, I can get to that later. I wanted to first make sure it would be possible to even get the underlying library working first. To that end, I started doing some research and playing around with getting [Ant to work with Gradle][15]. Theoretically, Ant build tasks are first-class citizens in Gradle world. The problem is, Gradle has no way of namespacing the imported Ant tasks. That means if theres a naming conflict, the build fails (theres a [patch to fix this][16] coming up). While this isnt an issue if youre just migrating from Ant to Gradle, if youre trying to use both Gradle and Ant at the same time, this will cause catastrophic problems. Since I dont intend to fork jSword for Gradle alone, I needed to come up with something different.
For a while I tried to hack together namespacing by putting the jSword Ant tasks into another project. You can find an example (that I assume worked for the author) [over here][17]. This didnt really work for me. By this time, I had been checking the And-bible [build script][18] trying to see how its author solved this problem. Without having any actual knowledge of how to write Ant tasks, what I learned by just looking at the code was that he copied over the original source, merged in his modified code, and then compiled the entire thing. And to be honest, I didnt even try to attempt this with Gradle. While Im sure Gradle is more than capable of doing this, it wasnt worth the effort to figure it out; I had a build I could imitate and use as a starting point.
And so, using the And-bible build.xml as a starting point, I wrote my own build script, which is now available over [here][19]. Ill have more information about actually writing the build script in another post!
[1]: http://crosswire.org/jsword/
[2]: https://github.com/mjdenham/and-bible/tree/master/AndBible
[3]: http://www.gradle.org/
[4]: https://ant.apache.org/
[5]: http://maven.apache.org/
[6]: http://developer.android.com/sdk/index.html
[7]: http://www.vogella.com/tutorials/AndroidBuildMaven/article.html
[8]: https://code.google.com/p/maven-android-plugin/
[9]: http://search.maven.org/
[10]: http://developer.android.com/tools/building/building-eclipse.html
[11]: https://ant.apache.org/ivy/
[12]: http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html
[13]: http://developer.android.com/sdk/installing/studio.html
[14]: http://developer.android.com/sdk/installing/migrate.html
[15]: http://www.gradle.org/docs/current/userguide/ant.html
[16]: http://issues.gradle.org/i#browse/GRADLE-771
[17]: http://gradle.1045684.n5.nabble.com/Accessing-ant-build-xml-from-gradle-td4650344.html
[18]: https://github.com/mjdenham/and-bible/blob/master/jsword-tweaks/build.xml
[19]: https://github.com/DjBushido/MinimalBible/blob/master/jsword-minimalbible/build.xml