so in this post I wanna tell you how you can use your favourite scripting language to write programs that run on your Android mobile phone. You know the typical way to program apps for android is writing them in java using the Android SDK. In combination with eclipse and the android development plug-in for eclipse that’s one of the easiest way to start your Android development.
But if you are not familiar with Java you probably have some difficulties to get along with learning Java first, before you can begin developing for Android.
In this post I show you a way you can use scripting languages and start directly with Android development
There is a framework you need to install on your phone called “SL4A” (Scripting Layer for Android)
Currently (MAR2012) the following Scripting Languages are supported:
- BeanShell 2.0b4
- Lua 5.1.4
- PHP 5.3.3
- Perl 5.10.1
- Python 2.6.2
- Rhino 1.7R2
Since I require Python to work on my Android phone I will describe the steps necessary for that, but all other languages should be the same or at least very similar.
If you work with an emulator you need to provide a SD Card (Image) to it, otherwise SL4A is not installable. You simply can create an imagefile with the mksdcard tool in you Android SDK folder (android-sdk-linux/tools/mksdcard). The command for that is
mksdcard -l nameForSDCard 1024M targetFile.img
That creates a SD Card with 1GB in size as, which you can afterward use to start your emulator with. If you haven’t yet created a virtual device check this blog post for more details: https://norwied.wordpress.com/2012/03/28/android-avd-manager/
You can start the emulator from terminal:
emulator -avd <avd-name> -sdcard <filepath>
In my case this will do the trick:
emulator -avd VNexusS -sdcard android/testSDCard.img
2) Installation of SL4A and python
Go to the website https://code.google.com/p/android-scripting/ and use any QR Reader app on your phone to get the URL for downloading the file “sl4a_r4.apk” to your mobile phone. (If you don’t have a QR Reader yet, I can recommend QR Reader for Android)
After download you can continue with installing. After application is installed, click “Open”. The usage of Tracking can be “Refused”.
Click on the “Menu”-Button and select “View”, next select “Interpreters”. Now you see, in the beginning only the Shell Interpreter is installed. We now install an interpreter for python. Click “Menu” and select “Python 2.6.2” starts the download for the python interpreter. Open the drop-down menu from the top border and click the downloaded file to install it. Next click “Open” and here “Install”, what starts the download and installation of the necessary components.
3) Execute a first script
Having that, you can go back to the Interpreters and now there is also the python interpreter listed too. Click it opens a normal python command prompt as on a normal computer with python, and you can start programming. Going one step further back to “Scripts” you see various examples in python that can directly be executed on your phone, awesome ;D
By the way: you can switch the display orientation of your emulator from landscape to portrait and back with Crtl+F12.
So cool, hope to see you back for my next blog post. Hopefully this one was a help for you.