Python for Android

Think Java's the only game in town when it comes to programming Android apps? Think again.

Mobile app development for smartphones is hot. This is no more prevalent than in the Android space where the activity level oftentimes is frenzied. However, when it comes to building a “real” Android app, it seems there's only one programming choice: Java (although it is possible with a lot more work to use C/C++ with Android's Native Development Kit). That said, Google wisely chose the popular Java programming technology upon which to base its Android SDK, which runs a customized VM.

By and large, this has been a smart strategy, as (unlike another popular smartphone) there's no need to own specific hardware and software to get started with app development on Android. All you need is a PC (or laptop) running Linux, Windows or Mac OS X, together with a copy of Java and the free Android SDK. Google provides emulator downloads for all the Android platform releases, and there's even a free plugin for Eclipse to start you off and point you in the right direction.

That's great—assuming, of course, you're a fan of Java. If, like me, you'd rather eat glass than sit down to write some Java code in Eclipse, it would appear that you are out of luck when it comes to implementing your next project on Android. But, this is not the case. There's a rather wonderful project called the Scripting Layer for Android (SL4A) that is bringing scripting languages to the Android platform and providing a working alternative to Java development.

In this article, I walk through the steps involved in preparing your computer for Android development with SL4A, then show how to write, test and run a simple script written in Python on your Android device.

Introducing SL4A

The Scripting Layer for Android is one of the many projects to see life as a direct result of Google's policy of allocating 20% of its employees' time to “pet projects”. Damon Kohler works for Google, and he created SL4A to scratch his own itch when it came to programming Android. SL4A provides a high-level interface to Android's underlying Java technologies, exposing a subset of the API to scripting languages.

Python was one of the first languages to be supported on SL4A, but contributed interpreters also are available for Perl, JRuby, Lua, BeanShell, JavaScript and Tcl. In its default state, SL4A comes pre-installed with a working version of the shell. SL4A's API is designed to be portable across scripting languages, so if you like what you see in this article but wish I'd used Perl instead of Python, the API calls I use here should work exactly as shown with the Perl interpreter. It's true that I'm very much a fan of Perl, but I'm equally comfortable in Python, and, as Python is the SL4A “standard”, I stick with Python throughout this article. Just be aware that you don't have to.

Getting Ready

To get started, you need a copy of the Android SDK running on your computer, and you need a Java VM. Although you aren't going to program your Android app with Java, you still need a Java runtime upon which to execute the Android emulator, which is part of the SDK. The inclusion of the Android emulator gives you a sandboxed testbed to play in while you create your app.

Before proceeding, you need to make sure a Java VM is installed on your Linux system. On my system (running a recent Xubuntu), I entered the following commands and was told neither program was available to me:

$ javac -version
$ java -version

Xubuntu suggested that I might like to use apt-get to install openjdk-6-jdk, which sounded reasonable to me, so that's what I did:

$ sudo apt-get openjdk-6-jdk

If you are running a distro that's not derived from Debian, search your software repository for a similar package and install it before proceeding.

With Java in place, it's now time to get the Android SDK. Downloads of the SDK are available for Mac OS X, Windows and Linux. Pop on over to Google's Android Developer site (see Resources) and grab the latest SDK tarball for Linux.

Installing the Android SDK

With the SDK downloaded, simply unpack the tarball within a directory of your choice (the filename you have may be different from the one I use here, but don't worry, yours is likely a later version of the SDK):

$ tar zxvf android-sdk_r07-linux_x86.tgz

This command created a new directory called android-sdk-linux_x86, which I renamed to Android. This newly created directory has a bunch of subdirectories within it. Don't be overwhelmed by all the stuff that's in there. Only one subdirectory is of interest to you at this stage, and it's called tools.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Looks like Paul's been time

Anonymous's picture

Looks like Paul's been time travelling again. i like the article though. It covers exactly what i have spent the past hour searching for.

Well at least you got the

Jayson's picture

Well at least you got the info a week early! ;) This is a very comprehensive article about app programming. While I don't have the technical expertise to craft the SL4a technique without conquering a steep learning curve, it does open my eyes to a new way to do things. This is particularly helpful since I've been learning how mobile solutions are being tapped into by users. As one can imagine, a company that does mobile marketing(example: is going to have different needs then a company that specializes in manufacturing, or a user who wants to simplify his/her "mobile" life. Right now I fall into the latter category, but that doesn't mean I won't have a vastly different agenda in regards to app programming in the future.

Time will tell if Android development via Python is something I will tap into, but it's nice to have the programming app horizon broadened. Options are good!


DaNmarner's picture

this essay is posted on "Apr 30, 2011 By Paul Barry in Mobile". And today is Apr 23.

I noticed that to. I wonder

Justin C's picture

I noticed that to. I wonder what planet he was on when he published the article.