Alternatively, we can create the Android application manually. We will show two ways to create a project manually:
AndroidMavenProject
using Android tools.Check out http://developer.android.com/training/basics/firstapp/creating-project.html to find the official guide to create a project using Android tools.
The AndroidManifest
file is a powerful file in the Android platform that allows us to describe the functionality and requirements of our Android applications.
Create the AndroidManifest.xml
file in the root of the project. The AndroidManifest.xml
file has the following code:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidmavenproject" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.androidmavenproject.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Create a new layout into resources in the activity_main.xml
file located at res/layout
, and define the visual structure of your app. The AndroidManifest.xml
file has the following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.androidmavenproject.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Finally, write the MainActivity.java
class at src/main/com/androidmavenproject
. This class contains the following code:
package com.androidmavenproject; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onStart() { super.onStart(); TextView textView = (TextView) findViewById(R.id.text_view); textView.setText("Hello world!"); } }
In this section, we will learn how to define Maven projects with an XML file named pom.xml
. This file provides the project's name, version, dependencies, and in particular, the Maven Android plugins and its configurations (see also Chapter 1, Maven and Its Philosophy).
Create a file named pom.xml
at the root of the project with the following code:
<?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.androidmavenproject</groupId> <artifactId>android-maven-project</artifactId> <version>0.1.0</version> <packaging>apk</packaging> <name>Android Maven project</name> <properties> <!-- use UTF-8 for everything --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <version>4.1.1.4</version> <scope>provided</scope> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <version>3.9.0-rc.2</version> <extensions>true</extensions> <configuration> <sdk> <path>${env.ANDROID_HOME}</path> <platform>19</platform> </sdk> <deleteConflictingFiles>true</deleteConflictingFiles> <undeployBeforeDeploy>true</undeployBeforeDeploy> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
If you use Eclipse, especially the m2e
plugin, you might get the following error:
Plugin execution not covered by lifecycle configuration: com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.1:generate-sources (execution: default-generate-sources, phase: generate-sources) Plugin execution not covered by lifecycle configuration: com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.1:proguard (execution: default-proguard, phase: process-classes)
Add the following contents to remove the life cycle configuration error caused by the m2e
plugin:
<build> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <versionRange>[3.9.0-rc.2,)</versionRange> <goals> <goal>generate-sources</goal> <goal>proguard</goal> </goals> </pluginExecutionFilter> <action> <execute /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> […]
In this chapter, we will examine only specific tags for an Android application.
The first tag that we will examine is the <packaging>
element that specifies an APK. The APK value is allowed only after including the com.jayway.maven.plugins.android.generation2
plugin.
As you can see, we define the Android plugin, com.jayway.maven.plugins.android.generation2
, in the <build>
section, with its configuration enclosed in the appropriate configuration tags. Here (in the platform
tag), we define the Android SDK platform to use during the build (API Level 4 is platform 1.6).
The following line tells Maven that the plugin contributes to a package and/or as a type handler:
[…] <extensions>true</extensions> […]
We are ready to use the most common goals to build the project; in this section, we will see:
Use the compile
goal to build the compiled .class
files in the target/classes
directory:
$ mvn compile
If you want to work with the .class
files directly, run the package
goal.
To take the compiled code and package it in its distributable format, such as JAR, run any test and use the following goal:
$ mvn package
Performing the preceding command on our project will generate a JAR file named android-maven-project -0.1.0.jar
on the target
directory.
Since we set the value of packaging
to apk
, the result will be an APK file on the target
directory, which is ready to be deployed and launched on a device or emulator.
If you want to install the application via Maven on your Android device, you can use the following command:
$ mvn android:deploy
If more than one device is available, you can specify the relevant device in your pom.xml
file. Maven can also start and stop an Android virtual device automatically for you.
To list all attached devices and emulators found with the Android debug bridge, use the following command:
$ mvn android:devices
If the android.devices
property is not set, it will use all attached devices. To specify a device, set the android.device
property; it is possible to use the special values, usb
and emulator
, as shown in the following code:
<properties> <android.device>usb</android.device> […] </properties>
Finally, you can also start the application using the following command:
$ mvn android:run