Build / Maven

Maven and Android – Part 2

In the previous article we installed Maven and some Android related tools, and got an existing project converted to a Maven build. We had an error because despite all of the tools we installed, Maven itself does not know how to build Android apps. In this article we’ll fix the build and begin to get an understanding of how Maven works and what benefits it offers us.

Maven LogoAs was explained previously, Maven does not directly know how to build Android apps, and that was the cause of the error that we saw – we asked it to build an APK, but it didn’t know how. Maven is vastly extensible through plugins, and there is a plugin that we can use which does know how to build an APK. All that we need to do is to configure Maven to use that plugin for the build. We do this by modifying the POM:

Let’s try and break this down a little, and explain what each piece is doing. Firstly:

These are just some basic configuration which tells Maven the filename that we want to use for our final APK, and where to find our source files relative to the top level directory of the project. Next we have declare a plugin that is required to perform the build:

This tells Maven that there is a dependency on the defined plugin as part of the build. Finally we have:

This is where we actually configure the plugin that we previously declared. In this case we are specifying that we are building against Android API level 14. And that’s all we need.

I’m sure that some will read this and think that I’ve omitted a couple of things, but I really haven’t. Maven does a whole load of stuff for us quite seamlessly, but an explanation of this is needed to fully understand how powerful Maven can be.

The first thing that really isn’t covered is how does Maven get the android-maven-plugin? We haven’t given it a URL or anything. We’ll cover this more later in this series, but Maven retrieves it for us automatically.

The other thing that appears to be missing is how simply specifying this plugin is used to actually do the build. This is all done through the Packaging type of “apk” that we specified. Before we added the plugin declaration, Maven gave an error because it did not know how to build a taget package of “apk”. When it starts, Maven initialises all of its plugins, and android-maven-plugin registers itself with Maven with the appropriate packaging type. Maven now hands control over to the plugin as required.

This is a somewhat simplified version of events as the plugin actually attaches itself to various phases of the Maven build lifecycle.

So now we have an error free build, or do we? While Eclipse is not showing an error anymore, Eclipse is not actually doing a maven build each time, it’s still using its own build process. The error that we saw was because Eclipse could not correctly determine how to perform a build based upon the Maven pom. In the next article we’ll look at how to actually perform a Maven build.

© 2012, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Maven and Android – Part 2 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *