Developing applications for the Google’s Android phone on Ubuntu 9.10

Developing applications for the Google’s Android phone on Ubuntu 9.10

The Eclipse IDE and Ubuntu is a very powerful combination for professional development work on the Android Phone.

This guide will show you how to install the Eclipse IDE and Android Development ToolKit on Ubuntu Linux.
You will also learn how to create a basic Android application and run it in the emulator.
This is your target.

Failure is not an option here.

Pre-requirements

• Basic knowledge of Unix/Linux would be nice. Study pocket guide.pdf
• You should also know how to program in Java or similar languages. Study http://java.sun.com/tutorial

We are now going to install these programs

• The Ubuntu Linux Desktop.
• Sun’s Java Runtime (JRE) and Development Kit (JDK).
Java Runtime is only needed to run the Eclipse IDE. Android has its own optimized Java VM (Dalvik).
• The Eclipse IDE.
• The Android Software Developer’s Kit (SDK).
• The Android Developer Tool (ADT), a special Eclipse plugin.
———

0) Install Ubuntu Desktop

Start by installing the lastest Desktop version of Ubuntu Linux. Follow this guide…
You may either choose 32 or 64bit Ubuntu. Both systems suit well.
Fulfill all 10 steps 😉

Ubuntu Linux has become the #1 development platform for Google’s Android.
And Android itself runs on the Linux kernel too.

Many benchmarks witness.. of Ubuntu’s powerfulness and suitability for this task.
———

1) Install Sun’s official Java

Install Sun’s Java Runtime Engine (JRE) and Java Development Kit (JDK). Also remove OpenJDK and other incompatible Java solutions.
Start gnome-terminal from Ubuntu’s main menu Applications -> Accessories -> Terminal and run these commands one by one (do not type the $).

$ sudo apt-get remove -y –purge openjdk-6-jre openjdk-6-jre-lib openjdk-6-jdk gcj

$ sudo apt-get install –reinstall -y sun-java6-jre sun-java6-jdk sun-java6-fonts
If you need to confirm the license, press TAB-key or arrow-keys to move the cursor in the text window.
———

2) Download and install the latest Eclipse IDE

The Ubuntu’s Eclipse package does not have all required features. Remove it !
$ sudo apt-get remove eclipse

We have to download the latest Eclipse IDE (named Galileo) from eclipse.org.
Browse to  http://www.eclipse.org/downloads and download the “Eclipse IDE for Java EE Developers (188 MB)”
Take either 32bit… or 64bit… Linux version depending on your Ubuntu OS.

Type uname -a command if in doubt. It should report i686 (for 32bit) or X86_64 (for 64bit Linux).

Extract the tar.gz file to your $HOME folder.
See picture 2a.

It should create an “eclipse” directory in your $HOME area (like /home/username/eclipse/).

Then check the eclipse directory.
$ ls -l $HOME/eclipse/

drwxrwsr-x  2 moma moma   4096 2009-06-19 08:49 about_files
-rw-rw-r–  1 moma moma  14932 2009-06-11 21:59 about.html
-rw-rw-r–  1 moma moma  83657 2009-06-19 08:49 artifacts.xml
drwxrwsr-x  4 moma moma   4096 2009-06-19 08:49 configuration
drwxrwsr-x  2 moma moma   4096 2009-06-19 08:49 dropins
-rwxr-xr-x  1 moma moma  68333 2009-05-20 00:04 eclipse       <– Eclipse executable.
-rw-rw-r–  1 moma moma    345 2009-06-19 08:49 eclipse.ini
-rw-rw-r–  1 moma moma  16536 2005-02-26 00:53 epl-v10.html
drwxrwsr-x 22 moma moma   4096 2009-06-19 08:49 features
-rw-rw-r–  1 moma moma   9022 2006-12-11 17:04 icon.xpm
-rwxr-xr-x  1 moma moma 335360 2006-12-11 17:04 libcairo-swt.so
-rw-rw-r–  1 moma moma   6506 2005-03-17 23:12 notice.html
drwxrwsr-x  4 moma moma   4096 2009-06-19 08:47 p2
drwxrwsr-x 12 moma moma  24576 2009-06-19 08:49 plugins
drwxrwsr-x  2 moma moma   4096 2009-06-19 08:49 readme

Looks good to me.
———

3) Create a launcher icon for the Eclipse IDE

The latest Eclipse version, when ran on Ubuntu 9.10, has a bug related to the event-system. Read this bug report…
But you can easily avoid this error by setting an environment variable before starting the IDE.

This one-liner shows how to avoid the error.
$ GDK_NATIVE_WINDOWS=true $HOME/eclipse/eclipse

I provide here a simple shell script (eclipse.sh) that you can use directly. Put it to the eclipse/ directory.
Get the script.
$ cd $HOME/eclipse
$ wget http://www.futuredesktop.org/karmic/eclipse.sh

Make the script executable
$ chmod +x eclipse.sh

Then start the Eclipse IDE by running
$ cd $HOME/eclipse
$ ./eclipse.sh

It’s best to create a launcher for it. Press the right-mouse-button on the desktop surface and select “Create Launcher…” from the popup-menu.
The command should point to eclipse.sh. The eclipse directory contains also an icon bitmap (icon.xpm). Point the icon location to that directory.

See picture 3a.
———

4) Download Android SDK (Software Developer’s toolKit)

Android SDK contains the application framework; widgets, views and other Java classes.
It also comes with many tools (in the tools/ directory) and most importantly, the Android Emulator.

Browse to http://developer.android.com/sdk/ and get the very latest Android SDK (Software Development toolKit).
Take the newest SDK version for Linux (i386). It will work on all Linux versions.

I simply click the android-sdk-linux_x86-1.6_r1.tgz link to start the download and accept the license agreement.
This latest SDK supports both version 1.5 and 1.6 of Android platforms.

Again, extract the files to your home area.
See picture 4a.

It should create an “android-sdk-linux_x86-1.6_r1” directory like /home/username/android-sdk-linux_x86-1.6_r1/.

Check the files.
$ ls -l $HOME/android-sdk-linux_x86-1.6_r1

drwxrwx—  4 moma moma 4096 2009-09-15 20:14 add-ons
drwxrwx— 13 moma moma 4096 2009-09-03 20:08 docs
-rw-rw—-  1 moma moma  176 2009-09-03 20:08 documentation.html
drwxrwx—  4 moma moma 4096 2009-09-15 20:14 platforms   <– SDK and samples
-rw-rw—-  1 moma moma  194 2009-09-03 20:08 RELEASE_NOTES.html
drwxrwx—  3 moma moma 4096 2009-09-03 20:08 tools   <– some important tools.

Looks good.

Notice: You do not need to remember and write long file and path names on the command line.
Press TAB-key and command line will auto-complete file and path names among other values.
———

5) Add tools/ to your $PATH

I am considering to remove this point.
Move to step 6.

The $HOME/android-sdk-linux_x86-1.6_r1 directory contains a subdirectory called tools (in my case it is /home/moma/android-sdk-linux_x86-1.6_r1/tools/).
If you want to access these tools from any other directory, you must add the tools/ path to the global $PATH variable.

Edit your $HOME/.bashrc resource file
$ gedit $HOME/.bashrc
and add the following blue line to it.

export PATH=${PATH}:$HOME/android-sdk-linux_x86-1.6_r1/tools/

Of course replace the name android-sdk-linux_x86-1.6_r1 with correct value in your case.
Gnome-terminal reads the .bashrc at start. The change take effect after your close and restart your terminal windows.

Then check the $PATH with
$ echo $PATH
———

6) Create a Virtual Device

You will have to create at least one Virtual Device to run Android application in the emulator.
This device definition determines the phone characteristics in the emulator.
See: http://developer.android.com/guide/developing/tools/avd.html

Do this:
Cd into the Android SDK’s tools directory
$ cd $HOME/android-sdk-linux_x86-1.6_r1/
$ cd tools

And create an Android Virtual Device (AVD) for an ordinary Android phone. Give it name “my_avd”.
$ ./android create avd –target 2 –name my_avd

Android 1.6 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
Created AVD ‘my_avd’ based on Android 1.6, with the following hardware config:
hw.lcd.density=160

The phone emulator will now use this (first) as default AVD.

List all command line options
$ ./android create avd

List all Virtual Devices
$ ./android list avd
It should list “my_avd” which you just created.
———

7) Install Android plugin for the Eclipse IDE

In this step we will install the ADT-plugin (Android Developer Tools plugin) in the Eclipse IDE.
It will make Android development and debugging very easy and straightforward.

Now start the Eclipse IDE and select Help -> “Install New Software…” from the menu.
See picture 7a.

The “Install” dialog for new software should appear.

Then press the [Add…] button and fill in these values.

Name: Android plugin
Location: https://dl-ssl.google.com/android/eclipse/

See picture 7b

Then stay in the same dialog and wait 5 – 10 seconds. You should first see that the status changes to “Pending…”.
It will now communicate with the Google’s site and download the most recent plugin.
See picture 7c.

Wait until the status or text changes to “Developer Tools”.

Then checkmark, select the “Developer Tools” and all its subitems as shown in the picture 7d.

Click the [Next…] button to move forward.
See picture 7e.
Do nothing in this dialog.

Again, click the [Next..] button to move to the final dialog and accept/confirm the license text.
See picture 7f.

Click the [Finish] button.
———

8) Let Eclipse know the location of Android SDK

Eclipse IDE has to know the location of Android SDK.
It’s very easy to set.

Start the Eclipse IDE and select Window -> Preferences from the main menu.
See picture 8a.

Select “Android” from the list.
Then click the [Browse…] button and set the path of SDK Location.
See picture 8b.

Of course this path must be the same you gave in step 4.
———

Congratulations.
You are now ready to start app development on the Android phone.
Besides having a lot of fun you may even earn some money.

9) Create your first Android application

In this step we’ll create a basic Android application (Hello Android) and run it in the emulator.

This example is a copy of http://developer.android.com/guide/tutorials/hello-world.html
Study it first.

Now, start the Eclipse IDE.

From the main menu select File -> New -> Project.
See picture 9a.

Further, select “Android Project” from the list.
Picture 9b.

In the project dialog, punch in the following values.

Project name: Hello Android 1.6
Build target: checkmark Android 1.6    (all new phones are Android 1.6 compatible)
Application name: Hello Android   (this will apear as application title in the phone)
Package name: com.testcompany.helloandroid   (you might use your own company domain here)
Create activity: HelloAndroid    (this must be a valid Java class name. Extends the Activity class)
Min SDK version: 4    (I think this means Android v1.6)

See picture 9c.
Click the [Next] and [Finish] buttons to complete the project.

Expand the project items.
Open HelloAndroid.java file (in src/com.testcompany.helloandroid/) and copy/paste the following code to it.
Replace the old content.
If you changed the package name (com.testcompany.helloandroid) in the previous step, change it here too.

// ——-
package com.testcompany.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);
TextView tv = new TextView(this);
tv.setText(“Hello, Android”);
setContentView(tv);
}
}
// ——-

The final result should look like this.
See picture 9d.

Note that the package name at the top must match the package name in the res/AndroidManifest.xml file.

Finally compile and run the application in the emulator.

Select Run -> “Run” from the menu (or press CNTR + F11 keys).
In the “Run As” dialog choose “Android Application”.

It should compile and start the emulator for Android Phone. It may take a few seconds to fire the emulator at the first time.

The final result should be this.
Picture 9e.

You may need to press (or click) the [Menu] button in the emulator.
Do not close the emulator. All subsequent runs will reload your application automatically.
Press CNTR + F11 to change the phone’s orientation.

Learn more about the Android emulator: http://developer.android.com/guide/developing/tools/emulator.html
———

10) Running apps on a real hardware

We will now prepare your Android phone for application development and debugging.

10a) First, set your phone for USB debugging.
In your Android phone, go to Settings -> Applications -> Development menu, and make sure “USB debugging” is checked (activated).
See picture 10a.

10b) Connect the phone to your computer via USB cable.

Then in your Ubuntu Linux.

10c) Type the lsusb command to list all USB devices.

$ lsusb
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Bus 001 Device 003: ID 0bb4:0c01 High Tech Computer Corp.  <— T-Mobile G1 Android phone (if I had one).
Bus 001 Device 003: ID 0bb4:0c02 High Tech Computer Corp.  <— HTC Hero Android phone.

Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

The listing shows that HTC Hero has VendorID 0bb4 and ProductID 0c02. (0bb4:0c02)
The listing looks correct.

10d) In Ubuntu, create a new rules file for these vendor:device IDs.
Type this command to create the file.

$ sudo gedit /etc/udev/rules.d/51-android.rules
Add the following blue lines (rules) to it and save the file. If your lsusb command reports other, newer product IDs for vendor 0bb4, add them also to the file.

SUBSYSTEMS==”usb”, ATTRS{idVendor}==”0bb4″, ATTRS{idProduct}==”0c01″, MODE=”0666″
SUBSYSTEMS==”usb”, ATTRS{idVendor}==”0bb4″, ATTRS{idProduct}==”0c02″, MODE=”0666″

See picture 10d.

10e) Re-connect your phone.
Your phone should be detected if you unplug and reconnect it to the computer.

10f) Now turn to the Android’s adb tool and list all devices and emulators.

Cd into the tools/ directory.
$ cd $HOME/android-sdk-linux_x86-1.6_r1/tools

List all Android type devices and emulators.
$ ./adb kill-server
$ ./adb devices
List of devices attached
emulator-5554   device     <– emulator instance/window.
HT98AL801622    device     <– HTC Hero phone !

You can now access your phone from the Eclipse IDE and run/debug applications on it.

I like to set the “Deployment Target Selection Mode” to Manual.
Start Run -> “Run Configurations…” dialog from the Eclipse’s menu. Select your Android application at the left side,
and click to the [Target] page. Set the “Deployment Target Selection Mode” to Manual.
This will let you choose between the devices (your phone and emulators) where to deploy and run your program.
—-

10b) Connecting Android-phone to a computer.
Accessing music and photos etc.

It is quite easy access the the phone’s memory card from Linux.

First, connect the phone to your computer via USB cable.

Then switch on the phone and enable the connection (mounting).

Do this:
On the phone, use your finger + pull and expand the notification area so you can see all messages.
Notice that the notification list begins from the very top, and you must pull downward to see it.

One of the messages should concern the “USB-connection”.
Select the “USB connection” message. Then click the [Mount] button.
See picture 10b1.
It will enable file transfer between the phone and computer.

See pictures 10b3 – 10b5.

Now you can access (read and write) files
from/to your phone and the Linux Desktop.
Your photos and videos And music files

Notice: Unmount the memory card after usage.
It ensures that all data is written to the device.
See picture 10b6.

I usually also right-click the memory card icon on my desktop and select “Unmount” or
“Unmount Volume” from the menu. Then it should be safe to unplug the USB-cable.
———

11) More exercises and training

The Android SDK itself comes with many good samples.

The samples lay in the /home/yourusername/android-sdk-linux_x86-1.6_r1/platforms/android-1.6/samples/ directory.
See: http://developer.android.com/guide/samples/index.html

Start Nautilus file manager (from Ubuntu’s Places -> “Home Folder” menu 😉 and investigate the files.
See picture 11a.

Study and run them one by one.

Do this to run the examples:
In the Eclipse IDE, close all projects by using the File -> “Close All” menu.

Then create a new Android Project from the File -> New -> Project -> “Android Project” menu.

In the project dialog, select “Create from existing source” and point the Location to example’s root directory (where you find the AndoidManifest.xml file !).
See picture 11b.

Expand the project items (files).

An important tip: In some of the src/*.java files, the import list is a bit wrong. Open each of the src/*.java files and press CNTR + SHIFT + O keys to auto-update the “import” list.
Save the files. You have to fix the import list before compiling.

Then run the project.

Notice: The ApiDemos sample… is really really important. It demonstrates almost every aspect of the toolkit. It’s THE bible and THE source !

Notice: You can also import projects using the File -> Import…, menu. Then from the list choose General -> Existing Projects into Workspace.

Examples on the developer.google.com.

© All Right Reserved 2019-2020 futuredesktop.org