Archive for July, 2010

Android development: the lack of integration of the SDK into Eclipse

Saturday, July 17th, 2010

For those of you in a hurry, the bottom line of this article is that if you want to develop on Andoid, do not install just any Eclipse IDE (and most likely not the latest one).
For Android SDK Revision 8 use Eclipse 3.5 Galileo – do not use the latest Eclipse 3.6 Helios.

Eclipse and the power of integrating an SDK to an IDE: why not for Android?

The Android SDK is based on Java and its preferred IDE is Eclipse.

Eclipse is a powerful and flexible IDE not limited to Java, providing neat integrations with several popular languages like PHP or ActionScript.

You can see this integration power in action when developing in ActionScript:
Adobe provides a version of Eclipse coming with the whole Flex framework and libraries, so the only thing you need to do to get up to speed writing Flash or AIR applications is to download one single file that embeds everything you need, from the IDE to the SDK.

This is where Google didn’t do a good integration job with their Android SDK as they did not fully integrate it to Eclipse.

In the big picture you need to:
1/ Download and install a vanilla Eclipse package that has nothing to do with Android (and already there are different versions of Eclipse to choose from, disconcerting for a newbie)
2/ Install the Android SDK separately and struggle your way into your Eclipse interface to make the integration happen)

And because several versions of Eclipse are available, not all of them will work well with the SDK.

For instance the Android SDK Revision 8 works on Eclipse 3.5 (nicknamed Galileo) but a new version of eclipse came out after, Eclipse 3.6 (aka Helios) breaking things with the Android SDK.
Now you have to find your way in the archives of Eclipse to download a previous version (the download page will of course

Here is my journey into the Android world, and how I got repelled installing the latest Android SDK revision 8 and trying to make it work on the latest Eclipse IDE.

This lack of integration, even at your very first contact with Android, seems to mirror the major problems of the platform:
- too many heterogeneous devices
- too many Android versions
Meaning sometimes too much freedom kills your productivity.

It’s exactly the story of Buridan’s donkey who died of indecision for having to chose between eating hay or drinking water.
Here new developers have to make choices they shouldn’t have to make (and hopefully not die of hunger because of indecision ;-).
Google should release a well integrated IDE for new developers starting with Android.
And it would not prevent experienced developers to have the freedom they want.

To Google: why not use the power of Eclipse and neatly embed your whole SDK into Eclipse, and release a single package to install that would contain both the IDE and the SDK?

First Android contact and how to waste time just getting the tools working

To install Android you start from here: http://developer.android.com/sdk/index.html, providing this overview:

1/ Prepare your development computer and ensure it meets the system requirements. [meaning install Eclipse]
2/ Install the SDK starter package from the table above. (If you’re on Windows, download the installer for help with the initial setup.) [meaning install the actual Anroid SDK package]
3/ Install the ADT Plugin for Eclipse.
4/ Add Android platforms and other components to your SDK.

All these 4 steps could have been collapsed into a single one (download and install an Android specific version of Eclipse) had Google integrated the SDK to Eclipse.

In addition, we also need to make sure Java is properly installed,

1/ Install Java

As we’ll be developing in Java, you need the Java JDK and not the JRE only.

Here we’re using JDK 6 Update 21.

Sun/Oracle explain it as such:

What Java Do I Need?
You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.

1/ Install Eclipse

Download the Eclipse Java EE package (for experts, compare the different Eclipse versions here), a Java-specific version of Eclispe as Android development is mainly Java.
Unzip the file (eclipse-jee-galileo-SR2-win32.zip) and move the resulting eclipse directory in Program Files (yes Eclipse doesn’t even have a proper Windows installer).

Be sure to download the version 3.5 (aka Galileo) of Eclipse and NOT 3.6 (Helios) or you will run into big troubles later and won’t be able to build and run the sample Hello World project in the emulator.

Android even caution users not to use the latest version of Eclipse (Galileo/3.6) in the SDK System Requirements sections:

Caution: There are known issues with the ADT plugin running with Eclipse 3.6. Please stay on 3.5 until further notice.

Any Java flavored version of Eclipse is fine. Android recommends “A Java or RCP version of Eclipse is recommended.”

Now launch C:\Program Files\eclipse\eclipse.exe to launch eclipse and check everything work fine.

I first had the following error:

Java was started but returned exit code=13
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xms512m
-os win32
-ws win32
-arch x86
-name Eclipse

This was because I didn’t put the eclipse directory in C:\Program Files\ but instead it was in another directory somewhere else on the machine.

Some blogs suggest running eclipse with the -clean switch to solve that problem but it didn’t work. However simply moving it to C:\Program Files\ solved the problem.

So be sure to install Eclipse in “C:\Program Files\” (a problem we wouldn’t have if Eclipse had a Windows installer…)

2/ Install the Android SDK

Now install the SDK for Android.

We’re using android-sdk_r06-windows.zip.
Unzip it in C:\Program Files\android-sdk-windows and launch it (SDK Setup.exe)

This launches the “Android SDK and AVD Manager” which downloads the SDK tools, documentation, sample code, Google APIs and USB drivers.
It actually installs several versions of the tools.

Downloading SDK Platform Android 2.2, API 8, revision 2
Installing SDK Platform Android 2.2, API 8, revision 2
Installed SDK Platform Android 2.2, API 8, revision 2
Downloading Samples for SDK API 8, revision 1
Installing Samples for SDK API 8, revision 1
Installed Samples for SDK API 8, revision 1
Downloading Google APIs by Google Inc., Android API 8, revision 2
Installing Google APIs by Google Inc., Android API 8, revision 2
Installed Google APIs by Google Inc., Android API 8, revision 2
Downloading SDK Platform Android 2.1, API 7, revision 2
Installing SDK Platform Android 2.1, API 7, revision 2
Installed SDK Platform Android 2.1, API 7, revision 2
Downloading Samples for SDK API 7, revision 1
Installing Samples for SDK API 7, revision 1
Installed Samples for SDK API 7, revision 1
Downloading Google APIs by Google Inc., Android API 7, revision 1
Installing Google APIs by Google Inc., Android API 7, revision 1
Installed Google APIs by Google Inc., Android API 7, revision 1
Downloading SDK Platform Android 2.0.1, API 6, revision 1 (Obsolete)
Installing SDK Platform Android 2.0.1, API 6, revision 1 (Obsolete)
Installed SDK Platform Android 2.0.1, API 6, revision 1 (Obsolete)
Downloading Google APIs by Google Inc., Android API 6, revision 1 (Obsolete)
Installing Google APIs by Google Inc., Android API 6, revision 1 (Obsolete)
Installed Google APIs by Google Inc., Android API 6, revision 1 (Obsolete)
Downloading SDK Platform Android 2.0, API 5, revision 1 (Obsolete)
Installing SDK Platform Android 2.0, API 5, revision 1 (Obsolete)
Installed SDK Platform Android 2.0, API 5, revision 1 (Obsolete)
Downloading Google APIs by Google Inc., Android API 5, revision 1 (Obsolete)
Installing Google APIs by Google Inc., Android API 5, revision 1 (Obsolete)
Installed Google APIs by Google Inc., Android API 5, revision 1 (Obsolete)
Downloading SDK Platform Android 1.6, API 4, revision 3
Installing SDK Platform Android 1.6, API 4, revision 3
Installed SDK Platform Android 1.6, API 4, revision 3
Downloading Google APIs by Google Inc., Android API 4, revision 2
Installing Google APIs by Google Inc., Android API 4, revision 2
Installed Google APIs by Google Inc., Android API 4, revision 2
Downloading SDK Platform Android 1.5, API 3, revision 4
Installing SDK Platform Android 1.5, API 3, revision 4
Installed SDK Platform Android 1.5, API 3, revision 4
Downloading Google APIs by Google Inc., Android API 3, revision 3
Installing Google APIs by Google Inc., Android API 3, revision 3
Installed Google APIs by Google Inc., Android API 3, revision 3
Downloading SDK Platform Android 1.1, API 2, revision 1 (Obsolete)
Installing SDK Platform Android 1.1, API 2, revision 1 (Obsolete)
Installed SDK Platform Android 1.1, API 2, revision 1 (Obsolete)
Downloading Usb Driver package, revision 3
Installing Usb Driver package, revision 3
Installed Usb Driver package, revision 3
Downloading Documentation for Android SDK, API 8, revision 1
Installing Documentation for Android SDK, API 8, revision 1
Installed Documentation for Android SDK, API 8, revision 1
Updated ADB to support the USB devices declared in the SDK add-ons.
‘adb kill-server’ succeeded.
ADB: * daemon not running. starting it now *
‘adb start-server’ succeeded.
ADB: * daemon started successfully *

3/ Install the ADT plugin for Eclipse

The ADT plugin for Eclipse integrates the Android SDK into Eclipse.

Eclipse 3.5 (Galileo) installation instructions:
1. Start Eclipse, then select Help > Install New Software.
2. In the Available Software dialog, click Add….
3. In the Add Site dialog that appears, enter a name for the remote site (for example, “Android Plugin”) in the “Name” field.
In the “Location” field, enter this URL: https://dl-ssl.google.com/android/eclipse/
Note: If you have trouble acquiring the plugin, you can try using “http” in the URL, instead of “https” (https is preferred for security reasons).
Click OK.
4. Back in the Available Software view, you should now see “Developer Tools” added to the list. Select the checkbox next to Developer Tools, which will automatically select the nested tools Android DDMS and Android Development Tools. Click Next.
5. In the resulting Install Details dialog, the Android DDMS (Android Dalvik Debug Monitor Service) and Android Development Tools features are listed. Click Next to read and accept the license agreement and install any dependencies, then click Finish.
6. Restart Eclipse.

4/ Configure the ADT Plugin

Once you’ve successfully downnloaded ADT as described above, the next step is to modify your ADT preferences in Eclipse to point to the Android SDK directory:

Select Window > Preferences… to open the Preferences panel (Mac OS X: Eclipse > Preferences).
Select Android from the left panel.
For the SDK Location in the main panel, click Browse… and locate your downloaded SDK directory.
Click Apply, then OK.

We’re now all ready for a Hello World sample app!

The Hello World app runs in the Android Emulator.
Before you can launch the emulator, you must create an Android Virtual Device (AVD).

An AVD defines the system image and device settings used by the emulator.

5/ Create an AVD

In Eclipse, choose Window > Android SDK and AVD Manager.
Select Virtual Devices in the left panel.
Click New.
The Create New AVD dialog appears.

Type the name of the AVD, such as “my_avd” (no space allowed in the name)
Choose a target. The target is the platform (this is the version of the Android SDK, such as 2.2 here) you want to run on the emulator.
You can ignore the rest of the fields for now.

Click Create AVD.

6/ Hello World Android Project

Now Create a New Android Project in Eclipse:

1.
From Eclipse, select File > New > Project.
If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled “Android” which should contain “Android Project”.
After you create one or more Android projects, an entry for “Android XML File” will also be available.

2.
Select “Android Project” and click Next.

3.
Fill in the project details with the following values:
Project name: HelloAndroid
Application name: Hello, Android
Package name: com.example.helloandroid (or your own private namespace, for instance com.bottomlessinc.helloandoid)
Create Activity: HelloAndroid

Here is a description of each field:

Project Name
This is the Eclipse Project name — the name of the directory that will contain the project files.

Application Name
This is the human-readable title for your application — the name that will appear on the Android device.

Package Name
This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.
Your package name must be unique across all packages installed on the Android system; for this reason, it’s important to use a standard domain-style package for your applications. The example above uses the “com.example” namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that’s appropriate to your organization or entity.

Create Activity
This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android’s Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn’t need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.

Min SDK Version
This value specifies the minimum API Level required by your application. For more information, see Android API Levels.

Other fields:
The checkbox for “Use default location” allows you to change the location on disk where the project’s files will be generated and stored.
“Build Target” is the platform target that your application will be compiled against (this should be selected automatically, based on your Min SDK Version).

4. Select the target. None was selected by default so we selected Android 2.2

Click Finish.

I got this error:

Project ‘HelloAndroid’ is missing required source folder: ‘gen’ HelloAndroid
The project cannot be built until build path errors are resolved

Right click on the error, select “Quick Fix”. Click “Finish” in the Quick Fix popup that already has “Configure build path..” selected.
It pops up the “Properties for HelloAndroid” dialog in the tab “Order and Export”.
HelloAndroid/src and HelloAndroid/gen were already selected, but Android 2.2 was not.
Select it, click ok, and the error goes away.

Now open the code located in src/com.bottomlessinc.helloandroid/HelloAndroid.java it looks like this:

package com.bottomlessinc.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        tv.setText("Hello, Android");
        setContentView(tv);
    }
}

The class is based on the Activity class.

An Activity is a single application entity that is used to perform actions.
An application may have many separate activities, but the user interacts with them one at a time.
The onCreate() method will be called by the Android system when your Activity starts — it is where you should perform all initialization and UI setup. An activity is not required to have a user interface, but usually will.

Then hit the run button.
This is where it crashes if you installed Eclipse 3.6 (Helios).

I got this error with Eclipse 3.6 Helios:

An internal error occurred during: “Compute launch button tooltip”.
org/eclipse/debug/internal/ui/stringsubstitution/SelectedResourceManager

and couldn’t get Eclipse to compile the project and launch the emulator.

So be sure in the previous steps you use the older version 3.5 of Eclipse, not 3.6.

What a waste of time that could have been avoided if Google packaged properly the Android SDK in its own Eclipse based package.

Now we have our basic Hello World application working, we can learn more about developing with Android.

7/ Some definitions to jump start you on the Android platform

adb shell (Android Debug Bridge) to access linux

Android uses SQLite DB like Firefox and iPhone

Android uses the WebKit library as browser engine, also used in Chrome, Safari and iPhone.

The DVM (Dalvik VM) is basically Google’s implementation of Java optimized for small devices.
It uses .dex files compiled at runtime from .class and .jar.
The Core java libs are different from Java SE (standard edition) and Java ME (Mobile ed.) but similar.

The Activity Manager records the sequence of screens in the app in the Application Stack.
The user can go back to the previous screens with the back button like in a browser.

Each UI screen is represented by an Activity class.
An app is one or more activities plus a Linux process that contains them.
Each Activity is responsible for saving its own state.

Here is the life cycle of an Android Activity:

Android Activity Life Cycle

onPause() is where the data should be saved.

Emulator swith to landscape: CTRL+F11 (or 7, or 9)

Intents: for instance the “send email” intent can be called, or you can register to that intent to replace the default email program.
Service: runs in the background like a daemon. Many services are already built in with lots of APIs to access them.
Content providers: used to share data between apps (ex: contacts)
Resources: graphics in res/drawable, XML UI in res/layout. Android creates a R class for all resources (ex: R.layout.main). Check the built in resources.
Common permissions (asked in the Android-Manifest.xml, granted at runtime depending on settings and user prompts): INTERNET, READ_CONTACTS, ACCESS_FINE_LOCATION

8/ Installing the Facebook SDK

The Facebook SDK is hosted on github.
Unfortunately Eclipse doesn’t have any integration with Git (shame shame shame).
As it just got integration with SVN in the latest release (shame shame shame), don’t expect well integrated Git support any time soon! (shame shame shame)

To work it around, use the equivalent of TortoiseSVN: ToirtoiseGit.

Unfortunately the installation is as painful as the Android one.

Before installing tortoisegit, you must install msysgit first, then tortoisegit.
“Please install msysgit 1.6.1 or above before install tortoisegit http://code.google.com/p/msysgit”
After installing, if you try directly to do a git clone (equivalent of a SVN checkout), you will see this error:

MSysGit have not installed Correctly or MSysGit Path setting error
Click Yes to open setting dialog to setup MSysGit Path

You need to edit the tortoiseGit settings, on the first page (General), point the MSysGit Path to your _bin_ directory of msysgit:
C:\Program Files\msysgit\bin

If you merely point it to C:\Program Files\msysgit, you will have this error when trying to clone again: “git have not installed” which is a known issue.

Right click in C:\Program Files\, “Git Clone…”, enter the url of the fb sdk (git://github.com/facebook/facebook-android-sdk.git) and the directory target (for instance C:\Program Files\android-facebook-sdk to be consistent with android-sdk-windows)

Unfortunately It all got messy and didn’t work, but this worked:
1/ install Git-1.7.0.2-preview20100309.exe (the Full installer for official Git 1.7.0.2)
2/ Check the option to include the Git GUI in the right click context of Explorer
3/ Right click/Git GUI here and enter the URL of the Facebook API: http://github.com/facebook/facebook-android-sdk.git

Well, what a pain.

Working with Android is unnecessarily painful, so please Google, get your developer tools to the level of those for the iPhone or Windows Mobile.

And please Eclipse, don’t wait years before integrating with Git.

Linux Screen cheat sheet: how to keep your terminal sessions connected no matter what

Friday, July 16th, 2010

Screen is a command line utility allowing you to launch shells that will never die (unless you explicitly ask for it of course).

The two most common use case are:

1/ Prevent losing work when the connection with the server is lost

For instance if you connect with SSH then start working in the terminal, if the connection with the server is lost, you could be in the middle of something important and information is lost.
If you’re editing a file not saved yet in vi, you’ll have to recover.

Instead, once connected on the server, start right away another bash shell, launching it using screen:

bash@localhost# ssh remotehost
bash@remotehost# screen -S myunbreakablesession bash

From here, all the commands you type are actually executed in the screen session you aptly named “myunbreakablesession”.

If the connection with the server is lost, ssh again to remotehost then “attach” to the screen session named “myunbreakablesession” again.

You “attach” (meaning you reconnect to it) to a screen using the “screen -r myunbreakablesession ” command.

In case you don’t remember how you called your screen session, you can list all the sessions with the “screen -ls” command as below:

bash@remotehost# screen -ls
There are screens on:
        23393.myunbreakablesession      (Attached)
1 Socket in /var/run/screen/S-root.
bash@remotehost# screen -r myunbreakablesession

2/ Continue exactly what you were doing when switching from one machine to another

Let’s say you work from two different places (workplace and home for instance) but want to seamlessly continue what you were doing from one place to the other.

You can use the ability to attach and detach to screen sessions for that purpose.

You can “detach” the screen (meaning you get out of it without closing it) by pressing CTRL-a then ‘d’ (maintain CTRL down the hit ‘a’ while CTRL still down, then release all, then hit ‘d’ only).

For instance from work you do:

bash@work# ssh remotehost
bash@remotehost# screen -S myunbreakablesession bash
bash@remotehost# scp big_database_backup.sql.gz root@otherhost:~/
<Detach the screen session here by pressing CTRL-a then d>
[detached]
bash@remotehost#

Then you move to another computer, at home for instance:

bash@home# ssh remotehost
bash@remotehost# screen -ls
There are screens on:
        23393.myunbreakablesession      (Attached)
1 Socket in /var/run/screen/S-root.
bash@remotehost# screen -r myunbreakablesession
<you can now check the status of the scp command>
bash@remotehost#

This is extremely useful when downloading huge files, or uploading big files like Sun core dumps to Sunsolve, or transferring big backups between servers like ZFS snapshots.

If the connection is lost to the terminal, the transfer is still alive running in the screen session.
Once you get home from work you can check on the status of the transfer by attaching to the session.

To summarize, here are the important commands to remember

# Create a screen session:
screen -S <name> <command>
# Detach from a screen session:
CTRL-a d
# List all screen sessions:
screen -ls
# Attach to a screen session:
screen -r <name>
# Create another session from an existing screen session:
CTRL-a c
# Switch back to the previous screen session:
CTRL-a CTRL-a
# Go to the next screen session (it will loop and show the first one when the end is reached):
CTRL-a n
# Go to the previous screen session (it will loop and show the last one when the beginning is reached):
CTRL-a p
# Attaching to a screen session that was not Detached (basically take over, kicking out the user currently attached):
screen -r -D <name>

More information can be found in this Linux Journal article.