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
    PATH="/opt/android-sdk-linux_86/tools/:$PATH"
fi

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:

android_without_eclipse_01

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

android_without_eclipse_02

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/
a_rocking_app/
a_rocking_app/build.xml
a_rocking_app/local.properties
a_rocking_app/build.properties
a_rocking_app/default.properties
a_rocking_app/src
a_rocking_app/src/com
a_rocking_app/src/com/android
a_rocking_app/src/com/android/arockingapp
a_rocking_app/src/com/android/arockingapp/ARockingApp.java
a_rocking_app/AndroidManifest.xml
a_rocking_app/bin
a_rocking_app/libs
a_rocking_app/res
a_rocking_app/res/layout
a_rocking_app/res/layout/main.xml
a_rocking_app/res/drawable-mdpi
a_rocking_app/res/drawable-mdpi/icon.png
a_rocking_app/res/values
a_rocking_app/res/values/strings.xml
a_rocking_app/res/drawable-hdpi
a_rocking_app/res/drawable-hdpi/icon.png
a_rocking_app/res/drawable-ldpi
a_rocking_app/res/drawable-ldpi/icon.png

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
[...]
BUILD SUCCESSFUL
Total time: 6 seconds

This should give us the following application files:

$ find . -name "*apk"
./bin/ARockingApp-debug-unaligned.apk
./bin/ARockingApp-debug.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.

android_without_eclipse_03

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
Success

The emulator shows an icon of the app.

android_without_eclipse_04

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.

android_without_eclipse_05

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.

android_screenshots

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”.

ASE_hort_cut_1

In the menu that appears choose “Shortcut”.

ASE_hort_cut_2

From that list select “ASE”.

ASE_hort_cut_3

Now select the script you want to run …

ASE_hort_cut_4

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

ASE_hort_cut_5

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

ASE_hort_cut_6

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.

ASE_00

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

import this

ASE_01

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

ASE_02

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”)

ASE_03

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()