Android is getting popular among mobile users very rapidly, and the fact that Android’s user base is increasing the demand for more usable applications on Android is increasing too. The demand for more applications is causing more and more developers to get into Android development, unfortunately until now the only way to develop for Android was to develop in Java.
But thanks to Google that one of their employee has come up with a solution that allows not only Python programmers but Perl, lua and bash programmers to develop on Android. The new project that has come out from Google is called SL4A and it allows you to use Python to develop for Android. You can get to know more about SL4A from the home page of the project on Google Code. This article tells you the exact steps you can take to setup Android SDK, Install SL4a and Python for Android and then Write your first Android Application in Python and run it.
Setup Android SDK and Android Virtual Device.
First you will have to download latest Android SDK from Google and setup your Android Virtual Device. If you like you can directly develop your Android application on your phone device. Skip to “Installing SL4A and Python for Android” if you want to directly develop on your phone.
1. Download Android SDK. For Linux download the tar ball and for Windows download the executable (recommended).
2. Install the SDK. You might also want to read the Installing the SDK document on Android’s developer site.
On Linux:
Extract the tarball in any directory of your choice.
On Windows:
Run the executable to install the Android SDK.
3. Go into the “tools” directory inside the extracted or installed directory.
4. Run the “android” executable.
On Linux:
$ ./android
On Windows:
Double click the executable to run it.
5. The android executable will start the Android SDK and AVDManager.
6. Click on the Create New AVD to setup the new AVD for your Android application development.
7. When the new AVD is created, select it and click on Start. You will see a Android device emulator screen and an Android Logo, the Android OS is booting, you will have to wait for some time. After some time you will get a fully loaded Android OS on your Android emulator to develop and test your Android Application.
Installing SL4A and Python for Android
Scripting Layer For Android is the main Android Application you need to be able to run your Python applications on Android, so first you need to install SL4A on either your Android emulator or on the real Android device in case if you are going to do the development directly on your device. Follow the steps below to install the SL4A on your Android.
1. Open Android Browser and browse to the SL4A home page: http://code.google.com/p/android-scripting/.
2. Tap on the QR code icon (the big square box) to download the SL4A application.
3. Tap on the download notification to install the app.
4. Once the installation is complete tap OK and then go back to SL4A home page on your Android Browser.
5. Tap on Python for Android in the Featured Downloads section to download Python for Android.
6. Tap on the download notification to install Python for Android.
7. Once the Installation is complete go to SL4A application which is located under your applications menu. You will see some python scripts that got Installed along with Python for Android.
8. Tap the test.py, a menu will popup.
9. Tap the second icon from left. The test.py script will execute and will demonstrate some of the possibilities of the SL4A API available for your Python programs.
Note: The most left icon is to run the application in terminal, the second from left is to run the application normally, the third from left is to edit the script, the fourth from left to rename the script and the last one from left is to delete the script.
Writing the First Android Application in Python
Now that our Android is ready and you have already tested that everything is working fine, its time to write your first Python application. You will write a very simplest version of hello world, this application will ask your name and then it will greet you. Thats it.
1. Copy the following code to greet_app.py on your computer.
import android app = android.Android() name = app.dialogGetInput("Enter your name:", "Please enter your name: ").result app.dialogDismiss() app.dialogCreateAlert("Greetings!!!", "Hello %s, how are you?" % name) app.dialogSetPositiveButtonText('OK') app.dialogShow() app.dialogGetResponse().result app.dialogDismiss() |
2. Goto “platform-tools” directory which is found under the Android SDK directory.
3. Copy (push) the greet_app.py to your Android by running the following command.
$ ./adb push /sdcard/sl4a/scripts/
4. Press the menu button on your Android device or Android emulator, and tap Refresh.
5. greet_app.py will appear in the script listing, tap the greet_app.py script.
6. The SL4A run menu will appear, click the second icon from left.
7. A popup will appear asking you for your name, enter your name and click OK.
8. Another popup will appear with a greeting message for you.
Congratulations!!! You are done here. Its now up to you how you make use of the Android API available via SL4A. Best of luck.
You should subscribe to the feed to stay updated about the next article that will describe how you can build a stand alone .APK application from your Python script.
nice 🙂
[Reply]
Thanks! I look forward to more.
[Reply]
Immensely useful man.. thanks 🙂
[Reply]
I have a question. If I build my app in python, does this mean that users wanting to download my app will need to take the extra step of downloading and installing SL4A on their devices?
That is a huge disadvantage if that is the case.
[Reply]
Hameedullah Khan Reply:
May 11th, 2011 at 11:32 pm
No they don’t have to. You can package your Python script as a standalone .APK file which will install on any android device and will run. The user will not have to worry about SL4A.
I am working on the howto that will explain the process of building the .APK of your Python script. Will publish it soon.
[Reply]
Amir Reply:
May 15th, 2011 at 3:12 pm
Hameedullah, I will be eagerly looking forward on your howto article on converting Python scripts to an .APK.
Thank you
[Reply]
Hameedullah Khan Reply:
May 16th, 2011 at 5:01 am
Its almost ready just some formatting needs to be done. I am hoping to publish it today or at most tomorrow.
Great, i am waiting for this post.
[Reply]
Neat. Good stuff -looking forward to your next article.
[Reply]
Great, I am looking forward your new tutorials.
[Reply]
Eagerly awaiting that post…
[Reply]
Still waiting for the post…
[Reply]
A slight typo, although I think most people who got to this stage would figure it out:
./adb push /sdcard/sl4a/scripts/
should be:
./adb push //greet_app.py /sdcard/sl4a/scripts
Many thanks for the tutorial. I had great fun a few years ago playing with pyS60, and I hope to have as much fun here.
[Reply]
Awesome! Waiting for the next article!
Btw, consider adding Disqus for people to comment on your pages?
http://www.disqus.com
[Reply]
I’ve been waiting for long time.
[Reply]
Hi,
Thanks a lot for your articel. Well, what about GUI programing in ANDROID with python? Possible???
Kind regards,
Mucip:)
[Reply]
I only got my Android phone 2 days ago. I’ve got greet_app.py working in the emulator on my PC, but how do I transfer it (and other scripts) to my phone?
David
[Reply]
Daniel Reply:
July 18th, 2011 at 8:14 am
Install sl4a on your phone and then its the same as in the emulator
[Reply]
python script -> stand-alone APK: http://code.google.com/p/android-scripting/wiki/SharingScripts
[Reply]
Is it possible to make a gui on android with python?
[Reply]
Hi ,give me some good website to create an app which is based on GPS,using python..
[Reply]
Can I publish the app in google play store??
[Reply]
Thanks for a great article on how to set up Python for Android development. I have a question though. I want to develop my app using pysdl2. I’ve found information on other sites about how to build SDL apps for Android but it seems inconvenient to build a full package every time I want to test it. I wonder if there is a way to test SDL apps with this virtual device?
[Reply]