In this chapter, we will see how to download, install, and set up a development environment, including the essential elements: Java Development Kit (JDK), Apache Maven, Android SDK, and an Integrated Development Environment (IDE). There will be pictures and illustrations provided to help you progress as quickly as possible.
Throughout this work, we will assume that you have a basic knowledge of Java, system, Maven, and IDEs and are familiar with terms such as: compiler, environment variables, repository, plugin, goal, build, and so on.
Prior to anything else, download and install a JDK7 and optionally a Java Runtime Environment (JRE). Both of them are downloadable from the Oracle website: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html. As a reminder, the JDK is a collection of tools needed to develop, compile, and monitor a Java application in the development state, whereas a JRE is needed to run a Java-compiled class or an archive. Keep in mind also that at the time this book was written, Android was not officially supporting JDK8 and that Oracle had already announced the end of public releases of JDK7. We hope that now that you read these lines, Android is compatible with the latest JDK. If not, then you should pick and install the latest available update of JDK7.
%JAVA_HOME%\bin parameter to your
C:\>java -version java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode)
Download Maven 3.2.1 or greater from the Apache Maven website: http://maven.apache.org/download.cgi. Install this by uncompressing it in a folder of your choice. Set the environment variable
M2_HOME to the right location, such as
%M2_HOME%\bin parameter to your
PATH variable. Check
mvn setup is executable by running the command
mvn -version . Here is an example of the expected output:
C:\win32app\Console-2.00.b148>mvn -version Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00) Maven home: C:\win32app\apache-maven-3.2.1 Java version: 1.7.0_45, vendor: Oracle CorporationJava home: C:\win32app\jdk1.7.0_45\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Two ways exist to install the SDK: the former is faster but it hides several operations you should be aware of as an Android developer. The latter takes more time but it introduces you to the tools and concepts coming with the SDK. Besides, this allows you to customize your installation, such as the folders. If you are a beginner, we encourage you to follow the first approach. You can always tune Android SDK at a later time when you feel more confident and you are familiar with the available settings.
You can download and install the official Android Studio from the Android website: https://developer.android.com/sdk/index.html. Android Studio is a suite that includes an integrated development environment (Android IDE), based on IntelliJ IDEA, with Android SDK tools, as well as other stuff like an embedded emulator system image and platform tools.
For Windows users, after downloading the installer (
.exe file), run it and just follow the instructions provided by the installer. You can keep the default values suggested if you are a beginner or if you are not sure how each setting affects the installation. When you're done, you can run Android Studio by clicking on the icon created by the installer.
For Linux users, unzip the file you downloaded to your favorite application directory. Then, navigate to the
android-studio/bin/ directory in a terminal and execute
studio.sh command. Regardless of your operating system, always double-check before the installation that you have enough disk space. It varies from OS to OS.
Alternately, if you already have an installed IDE or text editor, you can install the SDK manually. The following operations are long and tedious; however, any real Android developer should experience them at least once.
Download Android SDK from Android website: https://developer.android.com/sdk/index.html#Other. Again for Windows users, it is highly recommended to download the executable installer and follow the steps provided. Linux users can uncompress the downloaded zipped file to the folder of their choice, let's say
/home/dev/android-sdk folder. In both cases, let's call the location where android SDK installed:
By default, the SDK has the following top-level directory tree:
%ANDROID_HOME%\tools\: This folder gathers general utilities needed to develop, test, and debug an application.
%ANDROID_HOME%\platform-tools\: This folder contains other utilities, such as
fastboot, needed by developers to design, code, and debug an application on an actual device.
%ANDROID_HOME%\add-ons\: This folder is initially empty. It will be filled in later on, in order to provide support to call and use Google APIs, for instance the API allowing applications to communicate and display data from Google Maps.
%ANDROID_HOME%\AVD Manager.exe: This parameter allows us to manage the Android Virtual Devices (AVD), which are mere emulators. On first launch, no AVD is available. The following screenshot shows the Android Virtual Device Manager:
To launch the SDK manager, if you are a Windows user, run
%ANDROID_HOME%\SDK Manager.exe command. If you are working on Linux, open a terminal and navigate to the tools/directory in the Android SDK and then execute:
In a first step like that shown in the following screenshot, install the packages suggested by the SDK Manager: SDK Tools, SDK Platform tools, the current API, and so on:
At the time of writing, the last version of Android is 5.0.1 Lollipop. The API level is different from the grand public version: Lollipop corresponds to API 21, as well as Android KitKat 4.4.2 did to API 19, Android 4.0 / IceCreamSandwich did to API 17, or Android 2.3.X / Gingerbread to API 10, and so on.
Accept the license agreements as shown in the next screenshot, and let SDK Manager download. This may take a while for the default set of packages , and will take longer if you add other packages to the install list.
Once this is done, the directory tree will change a lot. The different folders are filled in with the elements selected in the preceding screenshot, such as Google APIs, drivers, documentation, sources, build tools, and so on.
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
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:
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 22.214.171.124 (included), the corresponding archives are accessible via Maven Central Repository.
In a later chapter, we will see how to automate the installation of Android artifacts in local repository.
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
ââââ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
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.
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
%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.
Unless weird exceptions arise, Maven features are fully integrated within the three major IDEs of the market: Eclipse, IntelliJ IDEA, and NetBeans. So, on this side, nothing special needs to be done. However, integrating Android SDK to the IDEs may need some additional operations.
In this section, we will go through the steps you need to follow in order to set up Maven and Android SDK to be used with Eclipse IDE. Some months ago, Eclipse was Google's first choice for creating the Android Studio but recently IntelliJ won the battle. Users that want to use Eclipse Luna for Android development need to follow some steps that we will describe in the following section.
Besides, Eclipse embeds a built-in version of Maven (3.2.1 with Eclipse 4.4 Luna). Yet, you can use a specific version of Maven: go to Window | Preferences | Maven | Installations | Add and browse to the right location | OK, as shown in the following screenshot:
If you try to filter the catalog with the keyword "android", the list will probably show nothing; so, we need to add the archetype we want to use for the project creation. Click on the Add Archetype button and fill in the archetype details as shown in the next screenshot and click on the OK button:
Now, the archetype should be available and selected in the dialog box we are looking at. Click again on Next and enter the same data we have specified to maven when we created the project using command lineâsee the following screenshot. Don't forget to change the platform property value to 21 as this is the SDK version we have installed so far. The default, which is 16, will cause the maven goals to fail because the corresponding SDK is not yet installed in our environment:
mvn clean install
You can also run the same goal using Eclipse and that's all. We have achieved the same result using our favorite IDE with just a few steps and we are now ready to start developing, testing, and running our first android application with Maven.
Android Studio is promoted by Google as IDE for Android projects. Actually, Android Studio can be seen as a fork of main IntelliJ IDEA tool. Therefore, installation and configuration for both of them looks very similar or identical.
For pure Android projects, IntelliJ IDEA Community is sufficient: it includes core features such as Maven, Git, and Android support. Anyway, if you intend to develop enterprise projects linked with other Java/JEE frameworks and multitier architecture, you should consider installing the Ultimate version. Besides, beware that, at least so far, Android Studio is proposed as the suggested IDE from Google to develop Android applications.
Unlike, do uncheck Gradle plugin as shown in the following screenshot:
One might wonder why we need to disable Gradle to support Maven for Android development? Actually, by default, Android Studio considers Android/Maven projects as Eclipse-Android projects that are to be imported as regular Eclipse projects and then migrated to Gradle. This is the reason why we need to disable Gradle. Yet, this disables Android support, too. Don't worry, we will restore it later.
Besides, Android Studio should retrieve the Maven install location (based on the environment variable
M2_HOME). If it does not, or if you need to use a custom location or version, you can navigate to Maven submenu inside the the settings (Ctrl+Alt+S) and override locations for Maven install, settings, and repository as shown in the following screenshot:
Go to File |Import Project as shown in the following screenshot:
Go to the settings (Ctrl+Alt+S) | Plugins, restore the disabled plugins: Android Designer, Android Support, Google Cloud Tools for Android Studio, and Gradle.
Restart Android Studio. The IDE informs you that the Android framework is detected. Click on Configure as shown in the following screenshot:
Open the project structure (Ctrl+Alt+Shift+S) | SDKs. Click on the + sign (shortcut: Alt+Insert) | Add a new SDK | Select Android SDK | Browse to
ANDROID_HOMEvariable | OK | Confirm both Java SDK and Build target | OK:
NetBeans is the third commonly used IDE in Java development community. Originally maintained and promoted by Sun, now a division of Oracle, NetBeans still has features that fits the needs of Android development.
Download NetBeans from https://netbeans.org/downloads/. Install it.
Like Eclipse, a default version of Maven is embedded; yet, you can override it: Tools | Options | Java | Maven | Executions | Maven Home.
Android plugin for NetBeans is NBAndroid. It is not available through default update centers. Besides, the features of free version are poor and limited. Anyway, a 1-month evaluation plan is offered.
Go to Available Plugins, check Android and NBAndroid Extensions (NBAndroid Gradle Support is not needed, unless you think of using both Maven and Gradle), and then Install. Accept the license, dismiss the warning about nonsigned modules, and restart NetBeans.
Go to Tools | Options | Miscellaneous | Android | set Android SDK location as shown in the following screenshot:
Thus, in this opening chapter, we learned or revised how to install Maven and the Android SDK, and integrate them with an IDE. By now, you should be able to:
Set up the JDK
Set up Maven
Set up Android SDK
Set up a development environment
Importing any Android project into major IDEs based on Maven
The next chapter will teach you the basic steps to develop a simple Android application using maven and so will use most of the things we discussed. If you don't feel confident in your current knowledge, then feel free to read this chapter again.