Python for Android
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.
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.
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.
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.
Special Reports: DevOps
Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!
With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...
- Hash Tables—Theory and Practice
- The Ubuntu Conspiracy
- Making a PHP Site on Linux Work with a Microsoft SQL Server Database
- A First Look at IBM's New Linux Servers
- Vigilante Malware
- Disney's Linux Light Bulbs (Not a "Luxo Jr." Reboot)
- Vagrant Simplified
- System Status as SMS Text Messages
- Dealing with Boundary Issues
- Bluetooth Hacks