Saturday 9 June 2012

Getting Started with Android


This tutorial is a quick start guide for installing Android and building a Hello World application using Android SDK. I've tried to keep it as brief as possible, for a detailed description please refer to the Android's official installation instructions [1].

The recommended practice for developing Android applications is using Eclipse and ADT plugin.  Android required at least Eclipse Helios and Java 6, and I assume that these are already installed on your system. If not, download and install Eclipse Classic [2] and JDK 6 [3] following the instructions specific to the OS you are using.

Install ADT plugin

1. In Eclipse, select Help > Install Software.
2. Click on Add button to add a new repository.
3. Enter the following url in Location field and click OK button:
https://dl-ssl.google.com/android/eclipse/
4. Select select all the items from the list press Next.
5. Follow the onscreen instructions to install the ADT plugin.

Configure SDK with ADT plugin

After installing ADT plugin when Eclipse restarts it asks for configuring Android SDK with the plugin. If you already have SDK and platform tools installed, you can choose the "Use existing SDKs" option and provide the path to the location of SDK. I installed configured it using the other option "Install new SDK" and selected both latest version and Android 2.1 supported by ~97% phones and tablets (as reported by eclipse). Follow the on screen instructions for installation. You can skip this step at this point and configure it later by selecting Window > Android SDK Manager from menu.

Create an AVD

Before transferring application to an android phone and running it there, you would like to test it on an emulator. The settings of a device that needs to be emulated are defined in an AVD(Android Virtual Device). You must create an AVD before launching the emulator. To create an AVD, select Window > AVD Manager and click New button. Define the settings of AVD and click Create AVD button. The AVD will be listed in the Android Virtual Device Manager window.

Say Hello Android

Now we are done with the setup part, lets get started with sample Android application that prints Hello World on screen:
1. In Eclipse, select File > New > Project
2. Select Android Project from the list, press Next.
3. Enter name of the project HelloWorld, press Next.
4. Choose an SDK from list, press Next.
5. Enter Application Name (HelloWorld), package (com.techobian.hello), select Create Activity checkbox and enter activity name (HelloWorldActivity) , press Finish.
6. Open HelloWorldActivity.java and replace its code with the following:
 package com.techobian.hello;

 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class HelloWorldActivity extends Activity {  
   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     TextView tv = new TextView(this);  
     tv.setText("Hello Android!!");  
     setContentView(tv);  
   }  
 }

7. Right click on project in Package Explorer and select Run As > Android Application from Context Menu. The emulator will take some time to load. Once done you will get a screen saying Hello World!!!

Say hello to device

So the app is now running fine on emulator, but before releasing it to you would like to test it on a real device. Here are the things to do:
1. Enable debugging on the device: On the device, go to Settings > Applications > Development and turn on USB Debugging.
2. Set up your system to detect the device: Skip this step if you are using Mac OS. On windows machine you need to install OEM USB Driver [4]. If you are using Ubuntu, you need to add udev rules file that contains USB configuration for each device on which you want to test. Add the following rule in the rules file /etc/udev/rules.d/51-android.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666", GROUP="plugdev"

This rule is for vendor Samsung, replace 04e8 with your vendor's id [5]. You can add multiple rules here if you are using different devices for testing. After saving our changes in the rules file, make the file readable by all users:
chmod a+r /etc/udev/rules.d/51-android.rules

3. Connect your device to the system and check whether the device is detected properly by running the following command from platform-tools/ directory in your SDK:
adb devices

4. Right click on your project and select Run As > Android Application and it runs on your device. You may also be presented with Device Chooser using which you can select the device for running the application. When you run the application on device, it also gets installed on the device.

REFERENCES

[2] http://www.eclipse.org/downloads/
[3] http://www.oracle.com/technetwork/java/javase/downloads/index.html
[4] http://developer.android.com/sdk/oem-usb.html
[5] http://developer.android.com/guide/developing/device.html#VendorIds