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:
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:
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:
Then, a new folder chapter1
is created. Go to this folder. You should find the tree of a classic Android project:
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:
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.