5. Maven Dependency Management

5.1 Introduction

Android Studio and the new Android build system support the use of Maven dependencies through Gradle. Alternatively, the Android Maven Plugin can also be used to build an Android application.

5.2 Spring Repository

The following repositories are available for all Spring projects. Much more information is available at the Spring Repository FAQ.

Release versions are available through Maven Central or via the Spring Repository:

repositories {
    maven { url "https://repo.spring.io/release" }
}
		

<repository>
    <id>spring-repo</id>
    <name>Spring Repository</name>
    <url>https://repo.spring.io/release</url>
</repository>
        

If you are developing against the latest milestone version, you will need to add the following repository in order to resolve the artifact:

repositories {
    maven { url "https://repo.spring.io/milestone" }
}
        

<repository>
    <id>spring-milestone</id>
    <name>Spring Milestone Repository</name>
    <url>https://repo.spring.io/milestone</url>
</repository>
		

If you are testing with the latest build snapshot, you will need to add the following repository:

repositories {
    maven { url "https://repo.spring.io/snapshot" }
}
        

<repository>
    <id>spring-snapshot</id>
    <name>Spring Snapshot Repository</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>
		

5.3 Example build.gradle

The following is an example build.gradle for use with Android Studio and the new Android build system.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.1'

    defaultConfig {
        applicationId 'org.springframework.demo'
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName '1.0'
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.+'
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M2'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
}
    

5.4 Example POM

The following Maven POM file illustrates how to configure the Maven Android Plugin and associated dependencies for use with Spring for Android.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework.android</groupId>
    <artifactId>spring-android-client</artifactId>
    <version>0.1.0.SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>spring-android-client</name>
    <url>http://projects.spring.io/spring-android</url>
    <inceptionYear>2010</inceptionYear>
    <organization>
        <name>Spring</name>
        <url>http://spring.io</url>
    </organization>

    <properties>
        <android-platform>21</android-platform>
        <android-maven-plugin-version>3.9.0-rc.2</android-maven-plugin-version>
        <maven-compiler-plugin-version>3.1</maven-compiler-plugin-version>
        <java-version>1.6</java-version>
        <com.google.android-version>4.1.1.4</com.google.android-version>
        <org.springframework.android-version>2.0.0.M2</org.springframework.android-version>
		<org.apache.httpcomponents-version>4.3.5</org.apache.httpcomponents-version>
        <com.fasterxml.jackson-version>2.3.4</com.fasterxml.jackson-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${com.google.android-version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-rest-template</artifactId>
            <version>${org.springframework.android-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient-android</artifactId>
            <version>${org.apache.httpcomponents-version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${com.fasterxml.jackson-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone</url>
        </repository>
    </repositories>

    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>${android-maven-plugin-version}</version>
                <configuration>
                    <sdk>
                        <platform>${android-platform}</platform>
                    </sdk>
                    <deleteConflictingFiles>true</deleteConflictingFiles>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin-version}</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
	

5.5 Maven Commands

Once you have configured a Maven POM in your Android project you can use the following Maven command to clean and assemble your Android APK file.

$ mvn clean install
	

The Android Maven Plugin provides several goals for use in building and deploying your application. You can configure a specific emulator in the plugin configuration, or if you omit the emulator name, the plugin will attempt to execute the specified goal on all available emulators and devices.

The following command starts the emulator specified in the Maven Android Plugin section of the POM file. If no emulator name is configured, then the plugin attempts to start an AVD with the name of Default.

$ mvn android:emulator-start
	

Deploys the built apk file to the emulator or attached device.

$ mvn android:deploy
	

Starts the app on the emulator or attached device.

$ mvn android:run
	

Displays a list of help topics for the Android Maven Plugin.

$ mvn android:help