Nicozan-Blog

Knowledge Seeking

Android Applications with Maven – How Should We Start? December 10, 2010

Filed under: Java — nicozan @ 3:15 pm


Now days more and more cellphones are using Android as its OS. So if you are interested in developing simple or complex apps for your device, you just need a couple of concepts and tools to get started.
We`ll be doing this first approach using:
-Maven (2.x or greater).
-JDK 1.6
-Android SDK 2.3.x
-ADT plugin for Eclipse
-On Windows

Before we proceed with the explanation, I need to warn you about how to install the Android SDK 2.3.x. In previous versions you just needed to add to your PATH (Environment Variable), %ANDROID_HOME%\tools (ANDROID_HOME = ..\android-sdk-windows).But in the 2.3.x we need to set too %ANDROID_HOME%\platform-tools.

Another useful tip if you are using Eclipse it`s to keep UPDATED the ADT (Android Development Tools) Plugin and the DDMS.

So now let`s describe the steps to create, compile and deploy our Android App:

Note: In this Link you can find the example I am describing.

1- In the Eclipse IDE go to File -> New -> Android Project.
2- Once you have finished the creation step, you have to create the pom.xml in the PocAndroidMaven/pom.xml location. Inside our pom.xml we`ll have:

<?xml version="1.0" encoding="UTF-8"?>
  <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.wordpress.nicozan</groupId>
      <artifactId>PocAndroidMaven</artifactId>
      <version>0.1-SNAPSHOT</version>
      <packaging>apk</packaging>
      <name>pocAndroidMaven</name>

<repositories>
  	<repository>
  			<id>jboss-public-repository-group</id> 
  			<name>JBoss Public Maven Repository Group</name> 
  			<url>https://repository.jboss.org/nexus/content/groups/public/</url> 
  			<layout>default</layout> 
  			<releases>
  			<enabled>true</enabled> 
  			<updatePolicy>never</updatePolicy> 
  		</releases>
 		<snapshots>
  			<enabled>true</enabled> 
  			<updatePolicy>never</updatePolicy> 
  		</snapshots>
  	</repository>
 	<repository>
  		<id>repository.jboss.org</id> 
  		<url>http://repository.jboss.org/maven2</url> 
 		<snapshots>
  			<enabled>false</enabled> 
  		</snapshots>
 		<releases>
 			<enabled>true</enabled> 
  		</releases>
  	</repository>
 </repositories>

      <dependencies>

		  <dependency>
              <groupId>com.google.android</groupId>
              <artifactId>android</artifactId>
              <version>2.2.1</version>
              <scope>provided</scope>
          </dependency>
      </dependencies>

      <build>
          <sourceDirectory>src</sourceDirectory>
          <plugins>
          	  <plugin>
                	<groupId>org.apache.maven.plugins</groupId>
                	<artifactId>maven-compiler-plugin</artifactId>
                	<version>2.3.2</version>
                   	<configuration>
                    	<source>1.6</source>
                    	<target>1.6</target>
                	</configuration>
              </plugin>          
              <plugin>
                  <groupId>
                    com.jayway.maven.plugins.android.generation2
                  </groupId>
                  <artifactId>maven-android-plugin</artifactId>
                  <version>2.8.3</version>
                  <executions>
      					<execution>
        					<phase>pre-integration-test</phase>
        					<goals>
          						<goal>apk</goal>
          						<goal>deploy</goal>
        					</goals>
      					</execution>
    			  </executions>
                  <configuration>
                      <sdk>                         
                          <platform>8</platform>
                      </sdk>
                  	  <emulator>
      						<avd>Default</avd>
      						<wait>20000</wait>
    			  	  </emulator>    			
                      <deleteConflictingFiles>true</deleteConflictingFiles>
                      <undeployBeforeDeploy>true</undeployBeforeDeploy>                  
                  </configuration>
                  <extensions>true</extensions>
              </plugin>              
          </plugins>          
      </build>     
     
  </project>

3-We have to pay special attention to the maven-android-plugin artifact. The main part to understand is that when we execute, in the CLI(Command Line Interface), mvn clean install it will create our APK package, install it in our Local Maven Repository %USER_HOME%\.m2\repository and deploy our APK in the AVD (Android Virtual Device). PLEASE REMEBER TO HAVE THE AVD RUNNING WHEN YOU DO THE BUILD.

This was a brief explanation to build an Android Maven App, thats why I am leaving you these links:

Android Developers
SonaType Maven Android Plugin Documentation

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s