Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and jar an Android project using Ant?

I'm trying to get a handle on this whole "Ant" buildsystem thing. I've created a build.xml that Android natively gives me by using the command: android update project -p . .

While this has given me a template build.xml file, I would like a couple more options. Specifically, I would like to be able to JAR and/or Compile my project; preferably with a version number.

Bonus Question: How can I exclude files from my JAR build. For example, I have one activity that I've used to test my functionality, but I don't want that to be included in the final JAR.

My build.xml file that Android has given me is Gisted here since it's pretty long (mostly comments).

EDIT: I'm also using IntelliJ so no Eclipse specifics, please!

like image 671
hwrdprkns Avatar asked Feb 22 '12 23:02

hwrdprkns


2 Answers

Specifically, I would like to be able to JAR and/or Compile my project; preferably with a version number.

In Android, version numbers are for apps, not JARs.

That being said, adding a jar target is a matter of adding this to your build.xml file towards the bottom:

<target name="jar" depends="debug">
    <jar
        destfile="bin/whatever-you-want-to-call-the.jar"
        basedir="bin/classes"
    />
</target>

Then, run ant jar to compile and package the JAR file.

You will see this pattern in most of the reusable Android components in my GitHub area (see the cwac- repos).

Bonus Question: How can I exclude files from my JAR build. For example, I have one activity that I've used to test my functionality, but I don't want that to be included in the final JAR.

Use excludes or excludesfile, as described in the jar Ant task documentation.

like image 88
CommonsWare Avatar answered Sep 28 '22 07:09

CommonsWare


For reuse of Android and non-android projects I would suggest you take a look at the Android Maven Plugin. It takes advantage of dependency management and other powerful features of Maven and will be much more suitable to what you want to do.

Checkout the webpage and documentation on it and maybe the chapter about Android development in Maven : The Complete Reference I wrote to get a bit more understanding as well as have something to look at about Maven.

The Android plugin supports reuse for normal jar artifacts and if there are Android dependencies within the code you can use apklib projects. Lots of projects like Roboguice, robolectric, actionbarsherlock and others use this feature successfully.

like image 37
Manfred Moser Avatar answered Sep 28 '22 06:09

Manfred Moser