Python for Android
Within the tools directory, only two programs are of interest to you. The adb utility allows you to transfer files to (and from) your Android emulator (more on this utility later). The android tool lets you prepare and run an emulator for any number of the current Android releases, and that's what you do next:
$ cd Android/tools $ ./android
The android command starts the Android SDK and AVD Manager, which is a tool that creates Android emulators for any number of Android virtual devices. At the moment, there are no virtual devices, so you need to create one. However, before this can happen, you need to install a target Android API package. To do that, click on the Installed Packages tab, then press the Update All button. In the dialog that appears, click Accept All and then press the Install button.
The resulting download takes a while, so drag yourself away from your computer and head off into the kitchen to make a nice cup of coffee. Depending on your download speed, you may have time to make a quick sandwich too.
With the download(s) complete, let's create an Android Virtual Device (AVD). An AVD is a virtual version of an Android device that you can run on the emulator. Select the Virtual Devices tab, then click on the New button. A dialog appears that you can use to set the characteristics of your AVD. Figure 1 shows that I've given my AVD a name (LJapp), selected an API target (API Level 7, which is Android 2.1) and set my virtual SD card to 256 meg. When you have made your selections, click on the Create AVD button to initialize your AVD.
If you return to the Virtual Devices tab, your LJapp AVD now should exist. Select the AVD and press the Start button, then select the Launch button from the displayed dialog. The emulator starts to load, and after a few seconds, you see the opening screen for your emulated Android device (Figure 2). Note that the emulator runs much more slowly than the actual physical phone, so it may take a while to get to the Android startup screen. Your mileage will vary, depending on the speed of your desktop's processor.
Note: throughout the remainder of this article, I use the word “tap” to refer to using the mouse to click within the emulator. There is no mouse pointer on your physical Android device (that's what your finger is for), but the emulator needs something, so the mouse pointer “emulates” your finger, hence the use of the word tap to describe the action required. When you see me use the word tap, think click.
Getting the SL4A on your emulator is straightforward. Start by tapping the Android browser and surfing to the SL4A Web site: code.google.com/p/android-scripting.
When the SL4A Web page appears, tap on the square graphic (the QR Code) to access the download URL for SL4A. The download should begin immediately, and once it completes, tap the downloaded package name (which is sl4a_r3.apk at the time of this writing) to install SL4A. Go ahead and tap on the Install button, then tap on the Open button to start the app. You'll be asked to participate in usage tracking (it's up to you, and it's optional), and then SL4A informs you that you have “No matches found” under your Scripts listing. That's okay; you've yet to install any scripts. SL4A now is ready for Python.
Return to the Android browser and double-tap the screen to enlarge the Web page. Locate the Featured Downloads part of the page on the right, and tap on the download arrow beside the python_for_android_r1.apk link. As before, let the download complete before tapping on the downloaded file to install it. When you're ready, tap the Install button to confirm the installation of the interpreter. When this completes, tap on the Open button, then tap on the big Install button to complete the installation. The Python for Android app downloads, extracts and installs the Python support files from the SL4A Web site and adds them into SL4A. The entire process should take only a minute or two to complete. If it takes longer than a few minutes, take a deep breath, and remember that the emulator is much slower than your physical device.
When the installation completes, you'll be presented with an Uninstall button. Don't tap this button! If you do, you'll remove the Python interpreter that you've just installed. Instead, tap on the emulator's home button (the little house icon) to return to the Android main screen, tap the tab at the bottom of the screen to see the list of installed apps, and note that SL4A has been added to your emulator (Figure 3).
Go ahead and tap the SL4A icon that now displays the list of installed Python scripts. Tapping the hello_world.py script pops up the SL4A run menu (Figure 4). Looking from left to right, the menu icons have the following meaning: 1) run the script at the Python shell, which is useful when debugging; 2) run the script “natively”; 3) edit the script in SL4A's built-in text editor, which is useful only for the most trivial of changes; 4) rename the script; and 5) delete the script. To test your installation quickly, tap the second menu icon, which I like to refer to as the “run wheel”. After a second or two, a message saying “Hello, Android!” appears on screen for a few seconds then disappears.
If that worked, tap the test.py script, then tap the run wheel. This runs a longer-running script that showcases some of the facilities available to you as a Python programmer working on Android. Again, bear in mind that the emulator runs slowly, so be patient and wait for the various Android interface elements to appear.
Webinar: 8 Signs You’re Beyond Cron
11am CDT, April 29th
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.Join us!
- New Products
- Users, Permissions and Multitenant Sites
- Not So Dynamic Updates
- March 2015 Issue of Linux Journal: High-Performance Computing
- Flexible Access Control with Squid Proxy
- Security in Three Ds: Detect, Decide and Deny
- Tighten Up SSH
- DevOps: Everything You Need to Know
- Solving ODEs on Linux
- Non-Linux FOSS: MenuMeters