Android with Maven

android program

Ever since making the switch from Ant to Maven 2 back in 2005, I’ve never looked back. So one of the first things I wanted to know was if I could use maven to build my Android apps. And of course, you can.

Here’s an example pom.xml file:


    4.0.0
    com.androidlearning
    androidlearning
    1.0.0-SNAPSHOT
    apk
    androidlearning
    Learning Android!

    
    ...
    

    
        ${project.artifactId}
        src
        install

        
            
                
                    com.jayway.maven.plugins.android.generation2
                    android-maven-plugin
                    3.7.0
                    true
                
            
        
        
            
                com.jayway.maven.plugins.android.generation2
                android-maven-plugin
                
                    
                        /opt/adt-bundle-linux-x86_64-20130917/sdk
                        
                        18
                    
                
            
        
    

There are certainly plenty of maven detractors, but the reasons why I prefer maven to ant (or any other tool that’s available at the moment):

  • XML is a reasonably good format for configuration. It is a terrible format for programming. With ant, you are essentially programming a build with XML. Maven configures builds with XML.
  • Programming a build leads to developers doing…whatever they want. But builds should not be particularly complex things, and with a few conventions can be very standardized. This is what maven does.
  • A maven expert is immediately a build expert for any product that builds with maven. Not so much with Ant (or any other build tool that gives the build developer freedom to program the build however they want).
  • Maven’s dependency management system is the right way to handle dependencies. There’s no checking jar files into source control, just some configuration specifying what your dependencies are.

Caveats:

  1. Android appears to moving towards using Gradle as it’s official build system, although it may not be ready for primetime yet. Gradle certainly has a lot of momentum in the industry.
  2. Maven creates .apklib files for android dependencies, which allows android dependencies to have xml resource files in them. This is great, except that the apklib concept is specific to maven. IntelliJ (and presumably the extremely beta Android Studio) will support these types of dependencies, but Eclipse knows nothing of them.