Friday, 11 May 2012

How to Run Your Own Python Scripts on an Android Device

While I don't usually post my programming activities on this blog, I have been dedicating increasing amounts of time dedicated to it. Especially now that I have just acquired my first android phone (sayonara iPhone) I have been wanting to run my own scripts on it. Now since I don't have much (any) experience programming java, the native programming language for android, I decided to set about figuring out how to run scripts in a language that I do know, hence python for android. Android app development is possible with just your home computer with some free software and you are free to run it on your device without a jailbreak that might void your warranty (unlike another popular smart phone).

Firstly, I will show you how to set up your android device to run Python scripts using a google product called Scripting Language 4 Android (SL4A). This is fairly straightforward.
Firstly, you need to make sure that you allow your phone to install software from unknown sources (in settings). Then, in your android internet browser, navigate to code.google.com/p/android-scripting. Then click on the QR code (black and white box) and proceed with downloading and installing SL4A.
Next, you are going to want to go back to the page and on the left hand side under featured>downloads, you'll want to download and install PythonForAndroid_r4.apk (latest at time of writing). Once the download of this is complete DO NOT CLICK UNINSTALL, it's easy to do, that's why I'm warning you.

Where to click in the SL4A download site


OK, so now the phone is set up to run python scripts and if you now navigate to SL4A (comes up just like a freshly downloaded app should), as in the image you should be able to see all of the pre-loaded python scripts.

SL4A and Python for Android icons
important Python scripts
In the image I have highlighted two scripts in particular. The first is the Hello World app, very straightforward, it will just print "Hello World!" in your notifications bar. The second is more interesting, test.py will show you a run through of features available to you as an Android Python programmer. It is definitely worthwhile to run this app and study the code carefully.

It doesn't seem to want to let me take a screen capture of it, but when you click on one of the scripts to run it, there are five icons that appear and the functions of them are as follows (from right to left): Run python script in terminal, Run python script, Edit python script, Rename python script and, finally, Delete the script.

OK, that's all well and good for running Python apps on the device, but for development the inbuilt script editor probably isn't the best way of doing it (unless you enjoy coding on a tiny screen and you own a bluetooth keyboard and mouse). So, for editing and quickly testing your scripts you are going to want to set up the android SDK and start up an emulator. I am not going to get into setting up the android SDK on this tutorial as it's different between operating systems and I'll be here all day. I may do in the future but for now I will only say it's a case of downloading eclipse from www.eclipse.org and the android SDK from http://developer.android.com/sdk/index.html. However, if you don't know what you're doing just do a google search for "set up android SDK on (insert operating system here)". There are loads of tutorials on blogs and youtube and developer.android.com itself.

Now, once you have the Android SDK set up and eclipse is running, you're going to need to set up an android emulator and do what is described above for your handset on the emulator (and bear in mind that the emulators run much more slowly than your device should). To do this, open eclipse (once the SDK is set up) and click window(eclipse on mac)>AVD manager. Then click New... and you should be on the setup page for an Android Virtual Device. Name it whatever you like and under target choose whichever android OS version you desire. Set storage to whichever value you like also (over 9MiB) and click Create AVD. That's it! once that is finished click start while the AVD of your choosing is highlighted and you will have your very own android emulator running on your system.

Now that an Android Virtual Device is set up and running on your system, you are going to need to install SL4A and python (same as described above but in the emulator) and you are also going to have to know how to transfer python scripts for testing out your applications (this is where I cant help windows and mac users, but you can do a search on it, or upload the python script to a site or email and download it to the emulator). Now, when setting up the SDK you downloaded a directory where it lives, right? Inside this directory (android-sdk-linux on my machine) there is a folder called 'platform-tools' and the tool we are looking for is called 'adb'. This tool will allow us to push files to the android emulator, and here's how:

First, copy your python script to the android SDK directory.
Then use the command:

$ tools/adb push (your file).py /sdcard/sl4a/scripts

to push the file to your emulator.
Once this is complete, open SL4A and you should now see your Python script sitting there ready to be run. Congratulations, you are running your own python script on android! Nice.

One more thing, always be sure to send your scripts (whether on the emulator or your handset) to sdcard/sl4a/scripts. If you don't, you wont be able to run them from sl4a. Thank you, goodnight.







6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks, Thomas! This article made it really easy to install Python on my Android phone. Awesome post :)

    ReplyDelete
  3. Very interesting, I like the way you write in PC and run in the device with that adb comand, very good, thanks...

    ReplyDelete
  4. Hey Thomas, I followed the same methodology on the physical device, also I can see all the preloaded, ".py" scripts, but while running the script,it just show a black screen no output for hello program.

    ReplyDelete
    Replies
    1. Did you save it in the sl4a/scripts folder?
      I still have sl4a running on my phone, but it has been a good few years since I wrote this so maybe things have moved on.
      Try running one of the notification or built in scripts.

      Delete