With the Maven Android Plugin it is possible to build and deploy/undeploy your android app and start/stop the emulator – if you’re used to maven you won’t be going without it ;)
If you’re interested in signing your apk using maven – take a look at this article
Project Setup
-
Create an android project using the android tool
-
We need some dependencies – so create a pom.xml in the project’s root directory – I took this from the plugin samples and modified it:
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (C) 2009 Jayway AB Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.android.app</groupId> <artifactId>demo</artifactId> <packaging>apk</packaging> <name>hasCode.com - Sample Android App using the Maven Android Plugin</name> <version>0.1</version> <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <!--<finalName>${artifactId}</finalName>--> <plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <configuration> <sdk> <path>${env.ANDROID_HOME}</path> <platform>3</platform> </sdk> <deleteConflictingFiles>true</deleteConflictingFiles> </configuration> <extensions>true</extensions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project>
-
Manually change the directory structure – hopeful there will be some archetype stuff in the future
mv src java &&mkdir-p src/main &&mv java src/main/
-
Another way is to tell Maven where the source directory is:
<build><sourceDirectory>src</sourceDirectory></build>
-
We won’t need some directories – delete them if they exist
rm-r bin build.xml build.properties libs
-
Build and install the app
mvn install mvn maven-android-plugin:deploy
Maven Dependencies for Android
update
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>1.6_r2</version>
<scope>provided</scope>
</dependency>
How to shorten the ultra-mega-thousand-miles-long command line
-
Avoid bleeding fingers by adding the pluginGroup to your Maven config ~/.m2/settings.xml:
<settings 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/settings-1.0.0.xsd"> <pluginGroups> <pluginGroup>com.jayway.maven.plugins.android.generation2</pluginGroup> </pluginGroups> </settings>
-
So instead of this command:
mvn com.jayway.maven.plugins.android.generation2:maven-android-plugin:deploy -Dandroid.file=demo.apk
-
We are able to type this stuff:
mvn android:deploy -Dandroid.file=demo.apk
-
More Information here: Maven Android Plugin – Tips & Tricks
Useful Plugin Goals
-
android:apk – creates a new apk file
-
android:deploy – deploys the apk to a connected device
-
android:emulator-start - starts the emulator with a specified android virtual device
-
android:emulator-stop – stops the emulator
-
android:pull/android:push – copies files/dirs from/to device
-
android:undeploy – undeploys the apk from a connected device
-
A list of all supported goals can be found here
Resources
Article Updates
-
2015-03-03: Table of contents added.