Posted on by | Posted in Developer Blog, Mobile Blog, Tutorial | Tagged , , , , ,

Building and managing Android projects with maven is not as easy as it could be. So in this blog, I'll show you how we managed to get it work nicely.

In this example, we'll create a parent project with an app module and a separate instrumentation tests module.

Project setup

The quickest approach to create a new Android project with maven is using a maven archetype from aquinet (http://mvnrepository.com/artifact/de.akquinet.android.archetypes).
For this project, we'll use the android-release archetype, as it creates a skeleton for exactly our case, as we also want to release the app with maven.

Hint: be sure to use the latest version of the archetype

mvn archetype:generate -DarchetypeArtifactId=android-release -DarchetypeGroupId=de.akquinet.android.archetypes -DarchetypeVersion=1.0.9 -DgroupId=com.foo.bar -DartifactId=my-android-project -Dpackage=com.foo.bar.android

With this, a parent project named after the artifactId gets created in your current directory. Inside, you'll find two modules, one for the app and one for the instrumentation tests, and the pom.xml of the parent is also already pre-configured with both of them as modules. In the pom, you'll find several plugins already configured for you, on top of all the android-maven-plugin

Now check the pom for the android-maven-plugin version and update it to the latest version (3.6.1 at the moment I wrote this blog). It should be at least 3.6.0 - if not, the build will fail, because the aapt tool can't be found as it moved in a previous android release and the plugin only considers this since version 3.6.0.

To test the setup, simply go into the parent project and run

mvn clean install

Now everything should be compiled and the instrumentation tests should run on any emulator or device that is connected.

If you get an error stating "Platform/API level 16 not available", install it via the SDK Manager, or replace the sdk version in the pom to an available one.

In the android-maven-plugin config:

 
  16
 

Note: the archetypes from aquinet also include their androlog logging framework which is a wrapper around the Android logging that adds the functionality of disabling the logging for releases and provides log4j-like configuration. You could remove it from the poms, or you could give it a try, which I would recommend you to ­čÖé

Setting up Android Studio to work with maven

To conveniently work on a maven project with Android studio, we have to set it to automatically import Maven projects, so that it notices changes to the pom.xml and updates it's dependencies.

File -> Settings -> Maven -> Importing -> Import Maven projects automatically

Import the project:

* File -> Import project
* Select your parent project
* Import from external model -> Select Maven
* Check "Import Maven projects automatically" -> Next
* Select the Android platform -> Next
* Finish

configure Android Studio to also build using Maven:

* Select the project
* Run -> Edit Configurations
* "+" Android Application
* Module: select Android App
* Launch default Activity
* Select default target device
* Before Launch: remove "Make"
* Before Launch: add Maven "clean install"
* OK

Now if you run the project in Android studio, Maven will be used.

Running Android Lint

It's recommended to run Android Lint to check warnings and errors. To run it with the android-maven-plugin, simply insert this config to the android-maven-plugin int the pom (and edit the sources path accordingly to your project):

 
    false
    ${project.basedir}/my-android-project/src/main/java/
    true
    false
 

If you want to execute android lint on the build, add android:lint to the maven commands (e.g. "mvn clean install android:lint")
Don't forget to edit the Run config you previously created to include "android:lint"!

The results will be written to /target/lint-results/lint-results-html/. (We disabled the XML output and enabled HTML, because it just has a better readability and you have a way better overview on the HTML pages)

Create a separate Instrumentation Test Profile

We don't want to install the app and run the Instrumentation Tests on every build, because it just takes so long. So we'll execute the tests only in a separate maven profile.

First, we'll disable the instrumentation tests for all cases by inserting a configuration for the android-maven-plugin in the parent pom:

   
       
           true
       
       ...
   

In the parent pom, add the new profile "IT":


        
            IT
        

And in the pom of the IT module, we also add the IT profile and enable the tests for it again:

        
            IT
            
                
                    
                        com.jayway.maven.plugins.android.generation2
                        android-maven-plugin
                        true
                        
                            
                                false
                            
                        
                    

                
            
        

To test it, run

mvn clean install

The Instrumentation Tests should not run. And then

mvn clean install -PIT

Now the tests should run.

Dependencies on android libraries

The dependencies on the android libraries are some kind of problem, because not all of them are published in maven central (e.g. only up to Android 4.1.1.4 right now, and only the support-v4 library, not the -v7 one...)

To compensate this, there is the maven-android-sdk-deployer project that lets you deploy your local sdk components into a maven repository of your choice (defaults to your local one).
For the tutorial on how to use it, please head over to their github page, everything is explained nicely detailed there.

Just some small pointers for this this setup: if you have a dependency on a apklib (except from the android framework itself), define the dependency in the parent project to define the version:

            
                android.support
                compatibility-v4
                18
            

In the app,

        
            android.support
            compatibility-v4
        

And in the IT module, set it as provided!

        
            android.support
            compatibility-v4
            provided
        

If you want to use newer versions than the preconfigured 4.1.1.4, you have to deploy them yourself and also use another group id for android, simply "android" instead of "com.google.android". For the exact versions, deploy them with the maven-android-deployer and look into your repository, or simply look at the readme at the github page of the deployer.
Here's an example config of the dependencies deployed with the maven-android-deployer:

    
        4.2.2_r2
        17
    

    
        
            
                android
                android
                ${platform.version}
                provided
            
            
                android.test.uiautomator
                uiautomator
                ${platform.version}
                provided
            
         
     

Don't forget to update the modules dependencies accordingly!

As you can probably see, we also replaced the sdk platform version config with a parameter, in the android maven plugin config:

    
        ${android.platform}
    

With this you should be able to develop Android (more or less comfortable) using maven and Android studio.
In the next blog, I'll show you how to configure the plugins to build releases of Android apps!

I'll also post the sources for the project at the end of the next blogpost, so you can check against it, if you get some errors.



9 Responses to Building Android projects with Maven – Part 1: Setup

  1. Tanish says:

    Thanks for the great article! Just what I needed.
    Would you know how I can pass configurable parameters from maven to the Test App.
    for e.g: server url


  2. Tobias Knell Tobias Knell says:

    You can use the android string property files (like for i18n) for this purpose, just let maven filter them and write the needed maven property in it. In the app you can access this like the other strings.
    url=${some.url}

    http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html


  3. Ram Patnaik says:

    android 4.3 with SDK 18 support which version of maven ???


  4. Tobias Knell Tobias Knell says:

    With the maven android plugin (current version), your maven has to be at least version 3.0.4. See here for more details and future updates in the versions: https://code.google.com/p/maven-android-plugin/wiki/Changelog


  5. subin says:

    how to install android-maen-plugin in AndroisStudio? And how to check version?


  6. radio says:

    Having read this I believed it was really enlightening. I appreciate you taking the time and effort
    to put this content together. I once again find myself spending way too much time
    both reading and commenting. But so what, it was still worthwhile!


  7. Teo says:

    Hey man, i followed your nice tutorial for building an Android Project with Maven. The project compiles and runs ok. But when i want to add some 3rd party libraries like (com.fasterxml.jackson.jaxrs), the build proccess fails. Take a look at the messages

    [INFO] trouble processing “javax/xml/stream/EventFilter.class”:
    [INFO]
    [INFO] Ill-advised or mistaken usage of a core class (java.* or javax.*)
    [INFO] when not building a core library.
    .
    .
    .
    [INFO] If you find that you cannot do this, then
    [INFO] that is an indication that the path you are on will ultimately
    [INFO] lead to pain, suffering, grief, and lamentation.

    [INFO] 1 error; aborting
    [INFO] ————————————————————————
    [INFO] Reactor Summary:
    [INFO]
    [INFO] android-with-maven – Parent ………………….. SUCCESS [ 0.131 s]
    [INFO] android-with-maven – Application ……………… FAILURE [ 16.181 s]
    [INFO] android-with-maven-it – Integration tests ……… SKIPPED
    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————

    [ERROR] Maven execution terminated abnormally (exit code 1)

    THIS IS NOT THE FULL LOG!!!

    Any ideas what is going on?


  8. simon says:

    if you dont want to setup a maven project paste the dependecy xml on this site: http://jar-download.com/index.php


  9. Varsha Das says:

    Whenever I am loading a maven based project in android studio I am getting this error “Unable to locate Android SDK used by project”. I did all the steps in your page.


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht ver├Âffentlicht. Erforderliche Felder sind mit * markiert.