Wednesday, November 14, 2012

Android Programming Hello World

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

The Android system uses a special virtual machine, i.e., the Dalvik Virtual Machine (Dalvik) to run Java based applications. Dalvik uses a custom bytecode format which is different from Java bytecode.
Therefore you cannot run Java class files on Android directly; they need to be converted into the Dalvik bytecode format.

2.5. Just in time compiler on Dalvik

Similar to the JVM, Dalvik optimizes the application at runtime. This is known as Just In Time (JIT) compilation. If a part of the application is called frequently, Dalvik will optimize this part of the code and compile it into machine code which executes much faster.

2.6. Android RunTime (ART)

With Android 4.4, Google introduced the Android RunTime (ART) as optional runtime for Android 4.4. It is expected that versions after 4.4 will use ART as default runtime.
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.

2.7. 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.8. 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. Installation

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.

No comments:

Post a Comment