Dr Rajesh Vasa30 October 2011, 2:34 PM
With 500,000 activations a day, Android is becoming the dominant player in the mobile eco-system, now powering the majority of mobile devices sold.
So, what is Android? How does one start developing for this platform? This masterclass introduces Android from a development perspective, and guides you to setup the development environment. In order to up hold the programmer’s tradition, we will build a “Hello World” application and run it in an emulator.
Android explained
Android is a platform designed for mobile devices that includes a customised Linux kernel, frameworks, libraries and commonly used applications. The Linux kernel manages the hardware and provides security, resource/file/process management, networking and drivers. A range of libraries sit on top of this kernel to provide functionality such as media playback, 2D/3D graphics, font rendering, data management, and web page rendering.

Figure 1: The Android software stack has a number of different components.
Android unlike most other mobile platforms is completely open, allowing complete access for anyone with the relevant programming skills to build and deploy applications. In fact you can deploy an application to your Android phone over a USB cable — no special permissions, certificates or extra rights needed. The freely available development kit provides full access to most of the hardware sensors and software libraries/frameworks that are part of the platform.
Developers have access to a powerful UI layout engine, a local database management system, a locale-aware resource manager for organising graphical/media/string assets, a notification manager to organise internal and external notifications, a powerful life cycle manager to manage the various processes created as part of an application, and an extensive graphics sub-system to build games. Here is a more in-depth description of the Android platform.
Android Device Hardware and the Interaction Model
Android devices have a minimum of 3 functional buttons unlike the iPhone, which has a single physical button. Android devices have a Home, Back and Menu button. The Home button returns the user to the main phone screen — it is very similar in functionality to the iPhone’s. The menu button permits contextual menus to be added to any screen. The Back button has functionality similar to the back button on a web browser, and takes the user back to the previous screen. In terms of usability, the back button is considered to be effective on a mobile device. Although many applications on Android make use of the Menu button, the options provided in this menu need to be discovered by the user — they literally have to press the menu button on the various screens of an application in order to find the options available within the contextual menus.
The Android platform permits hardware vendors to create additional buttons including keyboards and have different form factors with screens of different sizes — there are even differentiations in terms of the availability of the sensors. This rather generous policy has resulted in the hardware vendors creating a broad range of devices requiring software developers to design software that can cope with this fragmented and diverse world. Thankfully, the Android software development framework is designed to deal with a hardware eco-system that is diverse and continuously evolving. One specific approach the framework uses is to allow developers to tag an application with the hardware/software features required in a target phone.
Android platform is still evolving
The platform is still rapidly evolving with a new major release posted nearly every 6 months. In a way, Android is just like Windows with a range of different versions in active use. Currently most mobile devices run either version 2.1 or 2.2 release of the Android platform (fortnightly reports on usage are posted here by Google ). Newer models often use the 2.3 release, while the newly announced Android tablets run the 3.x release which is designed for the larger screen real-estate that is available on those devices.
Applications are forward-compatible (i.e. If developed for 2.1, they will run fine on 2.2 and 2.3), but are not backward compatible. Although, the platform is open source and freely available, most users do not have sufficient technical knowledge, or the desire to update the operating environment — just like most of us not updating the firmware in the fridge. Sadly, to ensure the application is available to a sufficiently large market, developers have to currently target Android 2.2 release. In this masterclass series, release 2.2 will be the assumed primary target.
Android and Java
Android is a bi-lingual platform. Developers have the choice of using either Java or C/C++ as their primary programming language of choice. However, in practice most of the applications are currently built in Java using the Eclipse IDE. The tooling, documentation, and online community support is also built for programming in Java.
Android developers made two critical changes to the Java environment to ensure that it works well on a mobile device. First, they opted to use the Dalvik virtual machine with optimisations for a mobile device rather than the Oracle/Sun Java Virtual Machine technology. Secondly, they rebuilt the standard library and have intentionally omitted many parts that are found in the Oracle/Sun Java. In a nutshell, they eliminated much of the bloat and provided a library/framework that runs smoothly even on an older generation mobile device — especially, for the UI and graphics library.
Java - A Short Overview
Java is a platform that offers a programming language and an extensive standard library/framework to help develop desktop, browser and web applications. The language has a C-like syntax, is object-oriented, and these days is mostly used to develop web applications and back-end enterprise systems. The strength of Java lies primarily in the standard library, its ability to connect with many existing systems, and the availability of a large pool of reliable open-source third-party libraries. Unlike scripting languages like Perl/PHP, programs written in Java are compiled into an intermediate representation, called the bytecode. An operating system specific virtual machine executes this bytecode when we run a Java program. The initial releases of Java were often considered (with good reason) to be slow, but recent optimisations to the virtual machine technology makes Java programs run almost as well as native code.
Setting up the Android Environment
An Android programming environment requires installing a set of tools all of which are available for Windows, OSX and Linux.
They include
- Oracle/Sun Java 6 Software Development Kit (SDK)
- Eclipse IDE
- Eclipse Android Plug-in
The Android SDK provides an emulator to help developers deploy and test their applications without the need for an external hardware device. The emulator is similar to VM-Ware virtualisation solution, in that it emulates the complete mobile device hardware in a host operating system and literally boots-up the Android operating system on the emulated device. The advantage of an emulator is that developers can test an application for a specific operating system release and screen size. For example, we can create and test for an old release like 1.6 with a 3in screen. The downside of an emulator is that it does not have sensors that a physical phone has and the development tools do not offer a sufficiently useful simulation of these sensors.
Do I need an Android device to get started?

To get started, the emulator is sufficient and for the scope of this masterclass series it will be adequate. However, there are a number of aspects unique to a mobile experience that can only be tested on a real device. Consider a simple question that will illustrate such an experience: “Are the buttons and labels appropriately sized and placed for use while walking?”
The only way to properly answer this question is to test with a physical device. There are other issues such as occlusion (where the hand may be covering some part of the screen that is needed to complete a task) that are also hard to test on an emulator. The rest of the masterclass covers the process of installing the tools after which we will create the simple Hello World application.
Installing the Java development kit (JDK)
The JDK provides all the tools needed to develop and run Java programs. Download the JDK here (Alternatively, search for 'JDK' and pick the first link). We only need to download the vanilla JDK without any bundles (see highlighted sections in Figure 2 and Figure 3 below). If you are running OSX, Java is installed as part of the operating system. Run the installer once you have downloaded the appropriate package.

Figure 2: The links that you need to download are highlighted.

Figure 3: Windows JDK versions are highlighted, select Windows x86 if you are not sure.
Installing Eclipse IDE
Step 1: Navigate to eclipse.org and select the Downloads link.

Figure 4: Figure highlighting the Eclipse Download link.
Step 2: There are a number of different versions of Eclipse, download ‘Eclipse IDE for Java Developers’ (see image below). Eclipse is shipped as a zip file. Unzip the file, and move the “eclipse” directory into a location where you store your programs (e.g. C:\Program Files). In the Eclipse directory there is an executable file (eclipse.exe), create a shortcut on your desktop or other appropriate location to this file. Eclipse does not use the registry and will initialise when you run it for the first time.

Figure 5: Download the IDE for Java Developers (as highlighted in the figure)
Step 3: Launch Eclipse via shortcut or directly by running eclipse.exe to verify the installation. The initial launch screen will show a welcome message (see Figure 6). Click on the curved arrow icon (centre right) to move into the project view.

Figure 6: The initial launch screen of Eclipse
Eclipse IDE uses views to organise information about projects and files that are currently open. (see Figure 7 for the views you will see on first launch). You can customise and configure the views extensively. For our development task, we do not need the Task List view, and hence that can be closed. The Package Explorer view will show the various projects as we work through them — this is where we can navigate the files within one or more projects. The Outline view shows the methods and fields in a class or other open entities — this view is helpful to quickly navigate the currently open file. In the bottom, we have other views that are helpful to identify any errors, and also to see messages.

Figure 7: The default views that are presented when Eclipse launches for the first time.
You can add, remove, and anchor the views at various locations of the screen to suit your needs. For example, I prefer to place the Outline view on the bottom left. You can adjust the location of views by dragging the view to the bottom left part of the screen. When you drag you should see an arrowhead indicating where the view will anchor.

Figure 8: Views can be adjusted by dragging and anchoring them.
Installing the Android SDK
Installing the Android SDK
Once you've installed the Android SDK, you can play with various releases of Android including the Honeycomb release (3.1) designed for tablets.
Step 1: Navigate to developer.android.com and click on the SDK link or the Download link. Select the installer for your platform. The Windows installer is highlighted in the image. If you are running OSX or Linux, follow the instructions here.

Figure 9: Windows Android Installer
The installer, when run for the first time, will scan to find the JDK (see Figure 10). If the installer complains that it cannot find the JDK even though you have installed it, then press the back button and click ‘Next’, this should resolve the problem. The installer has a bug and sometimes it does not find the JDK properly. Make a note of the directory/folder where you install the Android tools, as this is needed later to configure the Eclipse IDE plug-in.

Figure 10: Android installer detects the JDK if it is installed.
Step 2: Once the installer has copied all of the tool files into the target directory, it will start the SDK Manager automatically. The SDK manager downloads the Android OS platforms and gives you the option to select different releases to download. So, you can choose to download just a specific set of releases — for instance 2.2 and 2.3, or you can download all of the releases. In this step, if you ‘Accept All’ (bottom right) it will download all releases.

Figure 11: SDK Manager set to download all platform releases of Android.
The recommended option is to download everything, but this will download around 1GB of platform files. If you prefer to reduce the download size, then select the following options:
- Android SDK Platform-tools
- Documentation of Android SDK
- SDK Platform Android 2.3.3, API 10
- SDK Platform Android 2.2, API 8
- SDK Platform Android 2.1, API 7
- Samples for SDK API 10
- Samples for SDK API 8
- Samples for SDK API 7
- Google APIs by Google Inc., Android API10
- Google APIs by Google Inc., Android API8
- Google APIs by Google Inc., Android API7
- Google USB Driver package
- Android Compatibility package

Figure 12: SDK Manager selections set to download only a sub-set of the platforms.
The download progress window will show the approximate time to complete the download and indicate the various platforms that have been downloaded and installed. These are stored in the ‘platforms’ sub-directory (in the location where you originally place the Android tools).

Figure 13: The progress bar shows information of the various packages as they are installed.
Step 3: Once you have downloaded the various platform tools, we need to create a virtual Android device using the SDK Manager and run this in the emulator.
The process is as follows - Select ‘Virtual devices’ from the list on the left hand side of the window (see Figure 14). Select ‘New…’ to create a new virtual device. We will create a virtual device for the Android 2.2 platform (see Figure 15). Provide a value for the SD Card, as a number of sample applications expect this and will not run properly otherwise. A value of around 256MB is sufficient for the SD Card. The larger this value, the larger the image of the virtual machine on your computer. Finally, enable the Snapshot feature as his will allow you to restart a virtual machine quickly.

Figure 14: The SDK Manager with no virtual devices configured.

Figure 15: Create an Android Virtual Device for the 2.2 release with 256MB SD Card
Step 4: Start the Virtual Machine that you have created. The first time you start a virtual device, it takes several minutes and may be well over 5 minutes (see Figure 16). If the snapshot feature is enabled, the next start up will be a much faster process. Why so slow? The virtual device creates a full features ARM powered virtual hardware machine, then installs the operating system on that virtual hardware, and then it boots up. Google has promised to optimise this process in future releases.

Figure 16: The initial boot-up is a very slow process and will last several minutes. Go ahead, take a break and enjoy your favourite beverage.
Once Android fully boots up and launches, you will be presented with an initial screen where you can explore the virtual Android platform. If you are new to Android, click the grid icon (bottom centre) to see the applications installed by default in the virtual device.

Figure 17: Initial start screen of the virtual Android Device.
Step 5: Search for ‘APC Magazine’ in the Google search bar inside the Virtual Android device. If your network connection is working, it should connect to the net and complete the search (see Figure 18). Press the back button (the one with the curved arrow on the right hand panel) to return to the home screen.

Figure 18: Search inside the Android Emulator
You can also explore the various menu options from the Home screen. If you press the Menu button, these options will come up from the button part of the screen (see Figure 19). Explore the settings panel, or add additional widgets using the Add option. You can explore and see the option in the contextual menu in the browser as well.

Figure 19: Android devices offer a contextual menu for most screens.
As a final step, shutdown the emulator, and the SDK manager. We need to do this before installing the Eclipse Android plug-in.
Installing the Eclipse Android Plug-in
Eclipse is designed to download, install and configure plug-ins, similar to add-ons in Firefox. Google provides an excellent Android plug-in for Eclipse. We need to provide Eclipse with a location to download the Android plug-in.
Step 1: In order to install the plug-in, select ‘Help > Install New Software’. Click on the ‘Add’ button and provide the name and location. Use ‘Android’ as name, and the location is: https://dl-ssl.google.com/android/eclipse. Eclipse will go to this location and will display the option that you have ‘Developer Tools’ to install. Click ‘Next’ and Eclipse will download, install and configure the Android development plug-in. Once the plug-in has been installed, restart Eclipse IDE (the installer will prompt you for this action).

Figure 20: The Android Plug-in installation option.
Step 2: Start Eclipse again and point the Android plug-in to the location where we installed Android SDK. In order to do this, select ‘Window > Preferences’, and select the Android tab on the left panel (see Figure 21). From here provide the path to where the SDK was installed and click ‘Apply’. If the SDK is found, the target details will automatically be populated into the list.

Figure 21: The window where we set the SDK location for the Android Plug-in.
That is it. You are ready to build apps for Android.
Hello World on Android
In respect of the age-old tradition, our very first application for Android will be a “Hello World”. We will use Eclipse IDE and the Android Plug-in to complete this task. So, fire up Eclipse and let’s create our first application in 3 steps.
Step 1: ‘File > New Project’, expand the Android options. Select ‘Android Project’ and then click ‘Next’ (see Figure 22).

Figure 22: Selecting a New Android Project.
Step 2: We now need to provide some information about the project (See Figure 23). Let’s start with the project name — ‘HelloAndroid’. Select a Build Target of Android 2.2. The Application also needs a name — call it ‘HelloAndroid’. Every application in Android has a package name — call it ‘apc.examples’. The package name must have a dot in it due to the naming conventions that are mandated by the Android platform. Every Android application is made up of Activities that correspond to the various screens in the application. We will create one primary activity — ‘HelloAndroidActivity’ (this should be automatically created for you). Leave the rest of the fields unchanged. Click ‘Finish’.

Figure 23: Providing information about an Android project.
Step 3: Once the project is created. Select ‘Run > Run’ (or click the Green button with a little arrow). Since it is the first time we are running an application, Eclipse IDE will ask a ‘way’ to run the application that you have just created. Select the ‘Android Application’ option and click ‘OK’. This will launch the Android emulator, and run the Droid2.2 virtual device that we created earlier. Eclipse plug-in will search for the most appropriate virtual device to run the application, if more than one compatible virtual machine is found, it will ask the user to select the device that they want to target.

Figure 24: Eclipse IDE requests the configuration to use when running an application.
The console view (at the bottom of Eclipse IDE) will show progress information. The application itself is first packaged as an APK file (Android Package), and deployed to the virtual device. The Eclipse Android plug-in then launches this application. In order to exit the application, press the Back button on the emulator (curved arrow button).

Feel free to explore the files in the Hello Android project. Android development is built around a set of simple conventions to help organise a project and increase productivity. We will explore these conventions and build a slightly more interesting application in the next part of the masterclass series.
NEXT: Building a simple Android app