Scripting on Android

Hello there,
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
  • JRuby
  • 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.

1) Preparations
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.
Cheers πŸ˜‰
norbert

Advertisements

8 thoughts on “Scripting on Android

  1. My brother suggested I may like this blog. He used to be totally right. This publish actually made my day. You cann’t imagine simply how so much time I had spent for this info! Thank you!

  2. Your style is really unique in comparison to other people I have read stuff from. Many thanks for posting when you have the opportunity, Guess I’ll just book mark this page.

    I used to be able to find good info from your blog articles.

  3. Pingback: run SL4A python script from within Android app « my tiny TechBlog

    • But sometimes I am not quite sure whether that are posts form real people or just some spamming bots that want to place some anyoning ads on my posts :/

  4. I am really inspired along with your writing talents and also with the format for your blog. Is that this a paid topic or did you modify it yourself? Either way stay up the nice quality writing, it’s rare to see a nice weblog like this one today..

    • I do not get paid for writing that, I just wanna share my findings and hopefully they are helpful to somebody. It’s great when someone leaves me some comments so I know that someone is interested in all that stuff πŸ˜€

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s