Tag Archives: Android

Set up an Eclipse-free environment for Android application development

If you want to start to develop applications for Android you need to install the Android SDK. The installation instructions advise you to install Eclipse and to add the Android Development Tools (ADT) plugin. Not everybody is a fan of this dinosaur-like IDE and luckily it is not really needed. The plugin is just a wrapper for some tools that you can use in the command line, too. Here I will give you a description about how to setup an Eclipse-free, lightweight and transparent developing environment in Ubuntu GNU/Linux and how to use some to the SDK tools.

At first check if you have JDK and ant installed.

$ dpkg -l | egrep "jdk|ant1" | cut -c -30
ii  ant1.8
ii  ant1.8-optional
ii  default-jdk               
ii  openjdk-6-jdk             
ii  openjdk-6-jre             
ii  openjdk-6-jre-headless    
ii  openjdk-6-jre-lib

If anything is missing fill the gap. Then choose a suitable version of the Android SDK and download it:

$ wget http://dl.google.com/android/android-sdk_r06-linux_86.tgz

Unpack the downloaded file and move the resulting folder to location that you prefer for this purpose:

$ tar xfz android-sdk_r06-linux_86.tgz
$ mv android-sdk-linux_86 /opt/

Let’s have a quick look into our new tool box.

$ ls /opt/android-sdk-linux_86/tools/
adb         dmtracedump  hierarchyviewer  mksdcard           traceview
android     draw9patch   hprof-conv       NOTICE.txt         zipalign
apkbuilder  emulator     layoutopt        source.properties
ddms        etc1tool     lib              sqlite3

To make these tool available without their full path we add their folders path to the PATH environment variable in our .bashrc, .profilerc or a similar file:

if [ -d "/opt/android-sdk-linux_86/tools/" ] ; then

The first program will use it the AVD Manager that we launch via the SDK. We need it to install further components e.g. the Android platform 2.1 files.

$ android
Starting Android SDK and AVD Manager
No command line parameters provided, launching UI.
See 'android --help' for operations from the command line.

… starts a gui:


… and there we can choose the component to install. We add the package 7 with Android 2.1.


After this we have even more tools:

$ /opt/android-sdk-linux_86/platforms/android-7/tools/
aapt  aidl  dexdump  dx  lib  NOTICE.txt

Having all our tools at hand we now create our first project

$ android create project \
     --name ARockingApp \
     --target 1 \
     --path ./a_rocking_app \
     --package com.android.arockingapp \
     --activity ARockingApp

What does this command create?

$ find a_rocking_app/

This is the basic scaffold of an Android app and without any changes we can build the app using ant (actually you don’t have to use ant but run dx & Co. manually). Use the option debug to sign the app (and maybe read some background about signing apps).

$ ant debug
Buildfile: /home/kuf/jo/a_rocking_app/build.xml
Total time: 6 seconds

This should give us the following application files:

$ find . -name "*apk"

To run the app we create a new virtual device by using the SDK. Call android and click on “New” under “Virtual devices”. Give the device a name and select a target platform. Click on the newly created device and then on “Start”. Depending on your hosting system it might take a while until the emulator shows a proper home screen.


To get a list of running device use adb (Android Debug Bridge) with the option devices.

$ adb devices
List of devices attached 
emulator-5554	device

Now we know to which device we can install our new app to. As we have only one the option “-s” is not really needed but I add it here to show its usage:

$ adb -s emulator-5554 install bin/ARockingApp-debug.apk
205 KB/s (13207 bytes in 0.062s)
	pkg: /data/local/tmp/ARockingApp-debug.apk

The emulator shows an icon of the app.


If you click on this icon the app starts. As we haven’t changed anything in the source code after creating the app project we should see the default welcoming text.


Now you can fire up your favorite text editor and hack your first proper application!

Take a screenshot of an Android device using the SDK

Assuming you have installed the Android SDK you can use the Dalvik Debug Monitor Server (DDMS) to take screenshot of your Android phone or emulated devices. You have to activate USB debugging on the device (Settings > Applications > Development > USB debugging) and connect it to your computer. Then fire up the DDMS:

$ ddms

There simply select the device. After that you should find the option “Screen Capture” in the “Device” tab.


After clicking it you get a screenshot of your device which you can modify and save.

Create a launch short cut for an Android Scripting Environment script

Once you have written a useful ASE script you might want to have an icon on your home screen to launch the script instead of opening ASE first and selecting the script there. For this you can add a shortcut to your home screen. Go to your home screen, click the menu button and select “Add to Home”.


In the menu that appears choose “Shortcut”.


From that list select “ASE”.


Now select the script you want to run …


and decide if you want to run it in the shell or in the background.


Now you can find an icon on your screen that launches your script.


First steps with the Android Scripting Environment

The Android Scripting Environment (ASE) offers an easy way to generate (small) programs for Android phones. The quickest way to install ASE is via the barcode reader application and the bar code given on the ASE homepage that links to the installation package. After the installation of ASE you need to add an interpreter for the programming language which you like to use. Currently Python, JRuby, Perl, Lua, Tcl, BeanShell, Shell (installed per default) and JavaScript are available. For installing an interpreter press the menu button, select “View”, select “Interpreters”, press the menu button, select “Add” and select a language.

The following examples will show how to write some small programs using Python (currently version 2.6.2). The ASE Python interpreter comes with many “batteries included” (= standard libs) and some handy extras like BeautifulSoup (HTML/XML parser) and xmpppy (guess :)).

In general you can generate scripts on your phone directly in ASE. Alternatively use your preferred editor on you PC/Mac, mount the phone and copy the file to /<PATH_TO_THE_DEVICE>/ase/scripts/. In the latter case you need to unmount the device before you run the scripts.

Let’s start with a classic: To create a program that prints the Zen of Python open ASE, click the menu button, select “Add” and select “Python 2.6.2”. Now there is a display with two text fields – one for the filename, the other one for the script content.


Name the file “zen.py” (or whatever you like) and change the content to this:

import this


Now press the menu button and select “Save & Run”. The file is executed in the shell and you should see output like this:


Congratulations! You run your first Python script on an Android device. So far we have not used any of the Android specific features. To see the options have a look at the API referefence (push the menu button and select “API Browser”)


The following example makes use of the vibration functionality of the phone. Open a new file, give it a name and change the text to this and run it as done before.

import android
import time

droid = android.Android()
droid.vibrate() # vibrate - default time = 300 ms
time.sleep(2) # wait 2 second
droid.vibrate(500) # vibrate again - this time 500 ms

To use Adroid specific functions you need to import the “android” library and create an object of the Android class which offers the methods described in the API reference. As you can see you can import standard libraries like “time”, too.

One very cool feature is the option to use the camera of the phone as a barcode scanner. Create a new file with the following content, execute it and put any product with an barcode in front of the camera.

import android

droid = android.Android()
gtin = droid.scanBarcode().result["extras"]["SCAN_RESULT"]
print gtin

The GTIN of the product is printed to the shell. I make use of such a script to generate data for my project ProductTransparency. To send the output of a barcode scanning process to a server you can use urllib:

import android
import urllib

server_url = "http://add-your-server-url-here.net"
droid = android.Android()
gtin = droid.scanBarcode().result["extras"]["SCAN_RESULT"]
f = urllib.urlopen(dblink, gtin)
print f.read()