Book Image

Android Application Development with Maven

Book Image

Android Application Development with Maven

Overview of this book

Table of Contents (14 chapters)

Creation of a new project


Now, we create a new, basic project. Often, you may prefer to do this within your IDE; anyway, creating a project with Maven and its artifacts and then importing the new project into the IDE are more elegant practice: this will ensure the project matches Android standards and is not IDE-dependent. Moreover, by default, creating an Android project in an IDE and then adding Maven support to this require some tricks and hacks.

The first step needs a bit of work: determining the platform.version properties of your Android install. Go to one among the installed platforms folder. If you have downloaded only the latest SDK version, then it should be in the ANDROID_HOME/platforms/android-21 folder. Open the file source.properties. Search for Platform.Version and Pkg.Revision properties. In the following sample file, the respective values are 4.4.2 and 3:

AndroidVersion.ApiLevel=21
Layoutlib.Api=12
Layoutlib.Revision=2
Pkg.Desc=Android SDK Platform 5.0.1
Pkg.License=(…)
Pkg.LicenseRef=android-sdk-license
Pkg.Revision=2
Pkg.SourceUrl=https\://dl-ssl.google.com/android/repository/repository-10.xml
Platform.MinToolsRev=22
Platform.Version=5.0.1

This allows us to conclude that the Platform.Version value is 5.0.1_r2. This is actually the combination of the properties: Platform.Version and Pkg.Revision. Note this value well as we will need to use it in a few places.

For the following Maven commands, you are assumed to have set the ANDROID_HOME environment variable; otherwise, you will need to suffix all the commands with the property -Dandroid.sdk.path=/path/to/Android/SDK/install. Now, we need to install the android.jar file as any regular Maven artifact in our local repository:

mvn install:install-file  \
-Dfile=%ANDROID_HOME%\platforms\android-21\android.jar \
-DgroupId=com.google.android \
-DartifactId=android \
-Dversion=5.0.1_r2  \
-Dpackaging=jar \
-DgeneratePom=true

Unfortunately, you will have to perform this operation for each Android platform version your application will support. Yet, for Android artifacts prior to 4.1.1.4 (included), the corresponding archives are accessible via Maven Central Repository.

Note

In a later chapter, we will see how to automate the installation of Android artifacts in local repository.

Open a terminal, run the command as follows:

mvn archetype:generate \
  -DarchetypeArtifactId=android-quickstart \
  -DarchetypeGroupId=de.akquinet.android.archetypes \
  -DarchetypeVersion=1.1.0 \
  -DgroupId=com.packt.androidMaven \
  -DartifactId=chapter \
  -Dversion=1.0-SNAPSHOT \
  -Dplatform=21 \
  --batch-mode \
  --quiet

Then, a new folder chapter1 is created. Go to this folder. You should find the tree of a classic Android project:

├───assets
├───res
│   ├───drawable-hdpi
│   ├───drawable-mdpi
│   ├───drawable-xhdpi
│   ├───drawable-xxhdpi
│   ├───layout
│   ├───menu
│   ├───values
│   ├───values-sw600dp
│   ├───values-sw720dp-land
│   ├───values-v11
│   └───values-v14
└───src
    └───main
        └───java
            └───com
                └───packt
                    └───androidMaven

At the root of the project is the Project Object Model (POM), serialized as a pom.xml file. Beware that the pom.xml file is a representation of the actual POM, but discrepancies do exist between the actual POM and the pom.xml file.

Open the POM file in write mode with any regular text editor. Check the <platform.version> tag. This contains the same value as retrieved earlier (in our case: 5.0.1_r2); if it does not, then set it.

You can run a successful mvn setup clean installation. A folder target containing a chapter1.apk archive should be created. Theoretically, this APK file (short for, Android PacKage) can run on a compatible Android device, such as a smart phone, a tablet, or even a smart watch.

Tip

Debug Certificate expired

If you get a build failure with an error similar to the following:

[ERROR] Failed to execute goal com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.2:apk (default-apk) on project helloWorld: Debug Certificate expired on 02/02/13 00:10 -> [Help 1]

Then, do not worry. Delete the debug.keystore file that is located in ~/.android/or %USERPROFILE%\.android folder. This may fix most of the cases; if it does not, do not panic. Had your Android SDK been installed in parallel with a former version, another .\android\debug.keystore file may remain there. Delete it and relaunch the build.