"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Saturday, 29 September 2012

The many steps of an Android Hello-World


As I promised on my last post, I couldn't resist not to write at least a hello-world application on my new phone. I went so through the, a bit long, task of installing the Android developer tools and writing my first program with it. I followed, step-by-step, instructions provided from Google Android SDK how-to pages. As a start I installed on my desktop computer but, soon, I'll try installing Android SDK on the EEEPC too.

Installation

The first step I took has been, of course, installing Eclipse IDE, a quite plain install from the software centre apart from some compatibility problem with Oracle Java 7. I had to revert to OpenJDK as default JVM in order to make Eclipse start.
The second step has been downloading and extracting Google's Android SDK. I though, at the beginning, about installing on a system folder like “/opt”, until I discovered that Android's Eclipse plug-in takes care of downloading and upgrading files on the SDK folder. I so went back to installing it on my home folder since running Eclipse as root user every time you have to do an upgrade is far from being advisable. By the way installing the SDK has only matter of extracting the downloaded file:
tar -xf android-sdk_r20.0.3-linux.tgz
Then I started Eclipse and selected the “Install new software” option from the “Help” menu opening the plug-in installation form. Here I added Google plug-in update URL (with the “Add...” button) and selected all the “Developer tools” items.
After some usual license agreement the download and install process started until it eventually asked for Eclipse restart.
Once Eclipse started back I've been asked to select the SDK versions to download from a package manager window. I selected the suggested versions, latest 4.1 and the universally available 2.2 plus the 2.3 installed on my phone.
The plug-in started so the long process of downloading all is necessary for Android development in any of the versions selected. Must be noted that, while downloading files, I've been asked for authentication to download some (Motorola-specific) files stored on Motorola servers. I simply selected the “Cancel” option a tenth of times and proceeded without any problem. 

First Application

As plug-in installation at last completed I started the new Android application wizard. I've been so asked for application name and other building details.
Then a form asked for application icon attributes.
Followed by information to create an initial activity (the base of an Android application GUI).
after that I've been asked to downloaded some missing dependencies.
I'll have to keep it in mind to be online while creating new projects when I'll install Android SDK on the EEEPC.
This plug-in comes out with a “hello-world ready” application. At last here is my first project.

First run (on a virtual device)

In mobile development there are two ways to test your program: you can deploy it on a real device, like your phone, or you can test it on a virtual one emulated by the development computer. Virtual devices allow you to test your application on every imaginable hardware but are definitively slower than real device testing which is, on the other side, limited to what you physically own. To deploy and test my application I had so to prepare a AVD (Android Virtual Device) first. The plug-in provides a AVD manager interface from where is possible to define the virtual device details
So here is my hello-world running on AVD:

First run (on a real device)

Of course only running on a real device gives you the “real taste” of your application. Google how-to is quite easy on this point: just connect your device with USB, enable the “USB Debug” on your phone settings and run your application from Eclipse … All I got was a device selection window with a unrecognised device (with serial number “??????????????”) I couldn't select. After a brief search on the 'net I got an answer on this Stack Overflow post. I checked so the phone serial number with the “lsusb” command and I got the following line (among the others):
Bus 002 Device 003: ID 1004:61fc LG Electronics, Inc.
Then I created android the rules file
sudo gedit /etc/udev/rules.d/51.android.rules
with the following text inside (all in one line)
SUBSYSTEM=="usb", ATTR{idVendor}=="1004", MODE="0666", GROUP="plugdev"
after restarting my computer the device was eventually recognized
and I could run see my application running directly on my phone