Pages

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Thursday, June 2, 2011

Python scripting in Android

Android Scripting Environment (ASE) allow Python script to be run in Android.

Before you can edit and run Python program, you have to Add Interpreter of Python.

Start Android Emulator and ASE installed in the previous article, android-scripting: Android Scripting Environment.

Press MENU key, and select Add Interpreter.

Select Add Interpreter>Python 2.6.2
Wait it to be downloaded and extracted.

Press MENU key again, and select Add Script.


Select Python 2.6.2.


Type the name in the upper box; eg. HelloAndroidPython.py.
Type your Python codes in the lower box. Every Python come with the codes:

import android

droid = android.Android()

It's the android module, which is needed in every Python script that interacts with the available Android APIs.

Add the code below:

droid.makeToast("Hello Android! in Python")



Press MENU and click Save & Run.


Lets see the result:


That's my first Python script in Android:)

------------------------------------------------------------------
For more details of the Python Android API, refer to:
PythonAndroidAPI - A description of the Android API available to Python scripts.

android-scripting: Android Scripting Environment

The Android Scripting Environment (ASE) brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device.

To install and use ASE, it's no need Eclipse as in normal development. What you need is a Android Emulator, and a AVD with SD Card installed. Personally, I prefer to generate a fresh new AVD for ASE. Refer to the article Create Android Virtual Device (AVD) in command line to create a new AVD with 1000M SD Card.

After the AVD generated, you can start Android Emulator using the new AVD:

Start Terminal, switch to the tools folder of the installed Android SDK, it's ~/android-sdk-linux_x86-1.6_r1/tools in my own setup. Type the command:
$./emulator -avd my_android1.6_for_scripting

where my_android1.6_for_scripting is the name of my AVD.

Start Browser and go to android-scripting page:
http://code.google.com/p/android-scripting/
Click the Featured downloads link, ase_r13.apk.

Or go to the link directly:
http://android-scripting.googlecode.com/files/ase_r13.apk

Click on the downloaded file, and accept Install.



After a while, the ASE will be installed. It can be found in the application list.


Click to start ASE, press MENU key start the option menu, you can Add Script, Add Interpreter, start Terminal and call Help in Browser.


In Terminal, you can type any command as in desktop Linux (Shell).


To try the examples, select Add Interpreter>Lua 5.1.4


After the files downloaded and extracted, select hello.user.lua to see the result:



You may ask, why write scripts instead of real Android applications? Admittedly, Android's development environment makes life pretty easy, but you're tied to a computer to do your work. ASE lets you develop on the device itself using high-level scripting languages to try out your idea now, in the situation where you need it, quickly. Have a look at the example Lua and Python scripts to see for yourself.

Popular Posts