Wednesday, 14 January 2015

Android Development - Tutorial

Android Development - Tutorial
Based on Android 4.4
Lars Vogel  Version 11.8
Development with Android and Eclipse
This tutorial describes how to create Android applications. It primarily uses the Eclipse IDE for development. It is based on Android 4.4 (KitKat).
Table of Contents
1. What is Android?
1.1. The Android operating system
Android is an operating system based on the Linux kernel. The project responsible for developing the Android system is called the Android Open Source Project (AOSP) and is primarily lead by Google.
The Android system supports background processing, provides a rich user interface library, supports 2-D and 3-D graphics using the OpenGL-ES (short OpenGL) standard and grants access to the file system as well as an embedded SQLite database.
An Android application typically consists of different visual and non visual components and can reuse components of other applications.

1.2. Task
In Android the reuse of other application components is a concept known as task. An application can access other Android components to achieve a task. For example, from a component of your application you can trigger another component in the Android system, which manages photos, even if this component is not part of your application. In this component you select a photo and return to your application to use the selected photo.
Such a flow of events is depicted in the following graphic.
Defining an Android tasks
1.3. Android platform components
The Android system is a full software stack, which is typically divided into the four areas as depicted in the following graphic.
Android software layers
The levels can be described as:
  • Applications - The Android Open Source Project contains several default application, like the Browser, Camera, Gallery, Music, Phone and more.
  • Application framework - An API which allows high-level interactions with the Android system from Android applications.
  • Libraries and runtime - The libraries for many common functions (e.g.: graphic rendering, data storage, web browsing, etc.) of the Application Framework and the Dalvik runtime, as well as the core Java libraries for running Android applications.
  • Linux kernel - Communication layer for the underlying hardware.
The Linux kernel, the libraries and the runtime are encapsulated by the application framework. The Android application developer typically works with the two layers on top to create new Android applications.
1.4. Google Play
Google offers the Google Play service, a marketplace in which programmers can offer their Android applications to Android users. Customers use the Google Play application which allows them to buy and install applications from the Google Play service.
Google Play also offers an update service. If a programmer uploads a new version of his application to Google Play, this service notifies existing users that an update is available and allows them to install the update.
Google Play provides access to services and libraries for Android application programmers, too. For example, it provides a service to use and display Google Maps and another to synchronize the application state between different Android installations. Providing these services via Google Play has the advantage that they are available for older Android releases and can be updated by Google without the need for an update of the Android release on the phone.
2. Android Development Tools
2.1. Android SDK
The Android Software Development Kit (Android SDK) contains the necessary tools to create, compile and package Android applications. Most of these tools are command line based. The primary way to develop Android applications is based on the Java programming language.
2.2. Android debug bridge (adb)
The Android SDK contains the Android debug bridge (adb), which is a tool that allows you to connect to a virtual or real Android device, for the purpose of managing the device or debugging your application.
2.3. Android Developer Tools and Android Studio
Google provides two integrated development environments (IDEs) to develop new applications.
The Android Developer Tools (ADT) are based on the Eclipse IDE. ADT is a set of components (plug-ins), which extend the Eclipse IDE with Android development capabilities.
Google also supports an IDE called Android Studio for creating Android applications. This IDE is based on the IntelliJ IDE.
Both IDEs contain all required functionality to create, compile, debug and deploy Android applications. They also allow the developer to create and start virtual Android devices for testing.
Both tools provide specialized editors for Android specific files. Most of Android's configuration files are based on XML. In this case these editors allow you to switch between the XML representation of the file and a structured user interface for entering the data.
2.4. Dalvik Virtual Machine
Currently Android versions use the Dalvik virtual machine. The latest Android versions introduced a new runtime the Android RunTime.
2.5. Android RunTime (ART)
With Android 4.4, Google introduced the Android RunTime (ART) as optional runtime for Android 4.4. It is uses as default runtime for all Android versions after 4.4.
ART uses Ahead Of Time compilation. During the deployment process of an application on an Android device, the application code is translated into machine code. This results in approx. 30% larger compile code, but allows faster execution from the beginning of the application.
This also saves battery life, as the compilation is only done once, during the first start of the application.
The dex2oat tool takes the .dex file created by the Android tool change and compiles that into an Executable and Linkable Format (ELF file). This file contains the dex code, compiled native code and meta-data. Keeping the .dex code allows that existing tools still work.
The garbage collection in ART has been optimized to reduce times in which the application freezes.
2.6. How to develop Android applications
Android applications are primarily written in the Java programming language.
During development the developer creates the Android specific configuration files and writes the application logic in the Java programming language.
The ADT or the Android Studio tools convert these application files, transparently to the user, into an Android application. When developers trigger the deployment in their IDE, the whole Android application is compiled, packaged, deployed and started.
2.7. Conversion process from source code to Android application
The Java source files are converted to Java class files by the Java compiler.
The Android SDK contains a tool called dx which converts Java class files into a .dex (Dalvik Executable) file. All class files of the application are placed in this .dex file. During this conversion process redundant information in the class files are optimized in the .dex file.
For example, if the same String is found in different class files, the .dex file contains only one reference of this String.
These .dex files are therefore much smaller in size than the corresponding class files.
The .dex file and the resources of an Android project, e.g., the images and XML files, are packed into an .apk (Android Package) file. The program aapt (Android Asset Packaging Tool) performs this step.
The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the adb tool.
3. Security and permissions
3.1. Security concept in Android
The Android system installs every Android application with a unique user and group ID. Each application file is private to this generated user, e.g., other applications cannot access these files. In addition each Android application is started in its own process.
Therefore, by means of the underlying Linux kernel, every Android application is isolated from other running applications.
If data should be shared, the application must do this explicitly via an Android component which handles the sharing of the data, e.g., via a service or a content provider.
3.2. Permission concept in Android
Android contains a permission system and predefines permissions for certain tasks. Every application can request required permissions and also define new permissions. For example, an application may declare that it requires access to the Internet.
Permissions have different levels. Some permissions are automatically granted by the Android system, some are automatically rejected. In most cases the requested permissions are presented to the user before installing the application. The user needs to decide if these permissions shall be given to the application.
If the user denies a required permission, the related application cannot be installed. The check of the permission is only performed during installation, permissions cannot be denied or granted after the installation.
An Android application declares the required permissions in its AndroidManifest.xml configuration file. It can also define additional permissions which it can use to restrict access to certain components.
Note
Not all users pay attention to the required permissions during installation. But some users do and they write negative reviews on Google Play if they believe the application is too invasive.
4.1. Install Android Developer Tools
4.1.1. Download packaged Android Developer Tools
Google provides a packaged and configured Android development environment based on the Eclipse IDE called Android Developer Tools. Under the following URL you find an archive file which includes all required tools for Android development: Getting the Android SDK.
4.1.2. Stand-alone ADT installation
Extract the zip file and start the Android Developer Tools (Eclipse) which are located in the eclipse folder. You can do this by double-clicking on the eclipse native launcher (e.g., eclipse.exe under Windows).
4.1.3. Update an existing Eclipse IDE
See ??? for a description on how to update your existing Eclipse IDE to perform Android development.
4.2. Other Eclipse installation options
The simplest way to start Android development with Eclipse is to download a complete and pre-configured bundle as described in Section 4.1.1, “Download packaged Android Developer Tools”. It is also possible to update an existing Eclipse installation. Please see Android installation for a detailed description
5. Android device emulator and Android Virtual Devices
5.1. Android emulator and Android Virtual Device
The Android SDK contains an Android device emulator. This emulator can be used to run an Android Virtual Device (AVD), which emulates a real Android phone. Such an emulator is displayed in the following screenshot.
A running AVD
AVDs allow you to test your Android applications on different Android versions and configurations without access to the real hardware.
During the creation of your AVD you define the configuration for the virtual device. This includes, for example, the resolution, the Android API version and the density of your display.
You can define multiple AVDs with different configurations and start them in parallel. This allows you to test different device configurations at once.
5.2. Android device emulator shortcuts
The following table lists useful shortcuts for working with an AVD.
Table 1. Android device emulator shortcuts
Shortcut
Description
Alt+Enter
Maximizes the emulator.
Ctrl+F11
Changes the orientation of the emulator from landscape to portrait and vice versa.
F8
Turns the network on and off.

5.3. Google vs. Android AVD
During the creation of an AVD you decide if you want to create an Android device or a Google device.
An AVD created for Android contains the programs from the Android Open Source Project. An AVD created for the Google API's contains additional Google specific code.
AVDs created for the Google API allow you to test applications which use Google Play services, e.g., the new Google maps API or the new location services.
5.4. Speed optimization
During the creation of an emulator you can choose if you either want Snapshot or Use Host GPU enabled.
Note
The dialog implies that you can select both options, but if you do, you get an error message that these options can not be selected together.
If you select the Snapshot option, the second time you start the device it is started very fast, because the AVD stores its state if you close it. If you select Use Host GPU the AVD uses the graphics card of your host computer directly which makes the rendering on the emulated device much faster.
Startup options of the emulator

5.5. Intel system image
It is possible to run an AVD with an image based on the ARM CPU architecture or based on the Intel CPI architecture.
An Android virtual device which uses the Intel system image is much faster in execution on Intel / AMD hardware compared to the ARM based system image. This is because the emulator does not need to translate the ARM CPU instructions to the Intel / AMD CPU on your computer.
This image can be installed via the Android SDK Manager as depicted in the following screenshot.
Intel emulator
Note
An Intel image is not available for all API levels.
Note
At the time of this writing your also need to download and install extra drivers for MS windows.
Intel emulator
After the download you find the driver in your Android installation directory in the extras/intel folder. You need to install the drivers by running starting the .exe file.
Warning
This additional installation step is required on Window to accelarate the Intel emulator. Only downloading the driver via the Android does not make a difference.
After the download you can create a new AVD based on the intel emulator. The emulator does not start faster but is way faster during the execution of your Android application.
Intel emulator
Tip
After the download you may need to restart your development environment to be able to create an AVD with the intel emulator.
Tip
Linux requires a more complex setup. For a detailed installation description see the Intel emulator installation guide which also includes detailed instructions for Windows.
5.6. Alternative emulator
There are alternatives to the default Android emulator available. For example, the Genymotion emulator is relatively fast in startup and execution of Android projects.
6. Exercise: Create an Android Virtual Device (AVD)
6.1. Target
In this exercise you create and start an AVD. Even if you have a real Android device available, you should get familiar with the creation and usage of AVDs. Virtual devices give you the possibility to test your application for selected Android versions and a specific configurations.
6.2. Create an AVD
Define a new Android Virtual Device (AVD) by opening the AVD Manager via Window → Android Virtual Device Manager and by pressing the New button.
Create a new AVD
Enter values similar to the following screenshot.
Settings for a new AVD
Tip
Ensure that the Use Host GPU option is selected. This allows the AVD to use the graphical processing unit (GPU) of your computer and makes rendering much faster.
Afterwards press the OK button. This will create the AVD configuration and display it under the list of available virtual devices.
6.3. Start your AVD
Select your new entry and press the Start... button. Select Launch in the following dialog.
Settings for a new AVD
Warning
Do not interrupt this startup process, as this might corrupt the AVD. The first start may take up to 10 minutes on an older machine. On a modern machine it typically takes 1-3 minutes for a new AVD to start.
After the AVD has started, you can control the GUI with the mouse. The emulator also provides access to the phone buttons via a menu on the right side of the emulator.
Create a new AVD
Tip
Once started, don't stop the AVD during your development. If you change your application and want to test a new version, you simply re-deploy your application on the AVD.
7. Exercise: Create an Android application in Eclipse
7.1. Using the Android project wizard
The Android tooling in Eclipse provides wizards for Android applications. In this exercise you use the project creation wizard to create an Android application based on a template.
Note
The purpose of this exercise is to demonstrate the development process. The created artifacts are explained later in this book.
7.2. Create an Android project
To create a new Android project select File → New → Other... → Android → Android Application Project from the menu. Enter the fitting data from the following table in the first wizard page.
Table 2. Setting for your Android project
Property
Value
Application Name
Test App
Project Name
com.vogella.android.first
Package Name
com.vogella.android.first
API (Minimum, Target, Compile with)
Latest


No comments:

Post a Comment