[IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

Embed Size (px)

Citation preview

  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    1/16

    Android applications using Python and SL4A, Part1: Set up your development environment

    Configure your Windows workstation to run Scripting Layer forAndroid

    Skill Level: Intermediate

    Paul Ferrill ([email protected])CTOATAC

    19 Dec 2011

    This series of articles explores how to use Python and Scripting Layer for Android(SL4A) to build applications for the Google Android platform. This article, the first inthe series, shows what you need to do to get everything installed and running.

    About this seriesPart 1, this article, kicks off the series by showing you how to installeverything necessary to get started writing Python scripts on yourAndroid device. Part 2 will present useful scripting examples to getreal work done. It will also explore some of the available AndroidAPI calls, including the various windows. Finally, the series willexplore how to build a complete user interface just like you would inthe Java language.

    A common misconception about developing for the Google Android platform is thatyou have to write your code in the Java language. The truth is, you actually have amultitude of options through the Scripting Layer for Android (SL4A) project. SL4Astarted out as a 20% project by Google employee Damon Kohler. That was almosttwo years and four major versions ago.

    SL4A provides a platform for several scripting languages, including Beanshell, Lua,Perl, Python, and Rhino. There's also support for basic shell scripting. Today, thePython portion of the SL4A project has developed into a project of its own, due inpart to the popularity of Python and the desire to decouple the releases of new

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 1 of 16

    mailto:[email protected]://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/mailto:[email protected]
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    2/16

    Python functionality from the main SL4A release cycle.

    This article focuses on using Python to write applications for the Android platform.Python is a great tool for writing both simple scripts and complex, multi-threadedapplications. The great thing about having Python on Android is the opportunity to

    use the untold thousands of lines of code already written and freely available.Python is an easy language to learn if you've never used it before, and you will findmany resources available on the Internet to help get you up to speed.

    Installation and setup

    You must download and install several prerequisites before you can start developingwith SL4A. The first is a full Java Development Kit (JDK). You can find the latestversion on the Oracle Developer site.

    Next you need the Android software development kit (SDK). Two downloadchoices are available on the main Android developer site: a .zip file and an .exe file.When you download and run the .exe file, you'll be presented with a screen whereyou must choose which versions of the SDK and support files you want to install(see Figure 1).

    Figure 1. Choose which Android SDK options you want to download

    For this article, I installed and tested everything on a Windows 7 64-bit machine.

    Because this article is about developing applications for the Android platform usingPython, you obviously need to install Python on your development machine.Windows does not come with Python installed. As of this writing, the SL4A Pythonversion is 2.6.2. Download either the 32- or 64-bit version of Python 2.6 to stay

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 2 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    3/16

    compatible.

    It's a good idea to add a few links to the Android SDK in your PATH statement tomake it easier to launch the SDK Manager and other tools. To do this in Windows 7,perform these steps:

    1. Press the Windows key, and then click Search.

    2. In the text box, enter Environment.

    3. Click Edit the system environment variables.

    4. In the window that opens, click Environment Variables, then select thePATH variable in the User variables list.

    5. Click Edit, and then add the path to your Android SDK tools directory.

    The string you need to add looks like this:

    ;C:\Users\paul\Downloads\android-sdk-windows\platform-tools

    You must add the semicolon (;) before the new path to append a new directory.Once that's entered, click OK three times.

    Avoid spaces in directory namesSome of the tools, such as the emulator, may not launch if youinstall the SDK into a directory with spaces in the name. Thisincludes the default location of C:\ProgramFiles\android-sdk-windows.

    Installing SL4A on an Android device is similar to the process for any other Androidapplication. You can scan the QR code on the main SL4A project site with yourdevice to download the SL4A installation file. It should automatically launch whenthe download is finished. At this point, you should see an installation screen like theone in Figure 2.

    Figure 2. SL4A installation screen

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 3 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    4/16

    Clicking Install starts the process.

    The final step is to install the Python interpreter on your device. You can do sousing any of several methods. From the emulator, you can enter sl4a download inthe browser's search box (Figure 3).

    Figure 3. The SL4A download screen

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 4 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    5/16

    Clicking the PythonForAndroid_r4.apk link starts the download. To actually launchthe installer, view the notification screen by clicking and dragging from the top of theemulator screen toward the bottom of the screen. Clicking the Download completemessage launches the Python for Android installer (Figure 4).

    Figure 4. Python for Android initial installation screen

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 5 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    6/16

    Clicking Install launches a process that downloads and unpacks several .zip files.For the purposes of this article, simply click Install on the primary installation screen(Figure 5).

    Figure 5. Python for Android primary installation screen

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 6 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    7/16

    You should see three separate progress windows. The first shows the download,and then the extraction of the files onto the SD card. If everything works, an"Installation Successful" message appears.

    Android SDK basics

    There are two basic methods for testing your Python code using SL4A: using anemulator or using an actual physical device. The Android SDK provides basicemulator capability and the tools to create an emulated device with the samecharacteristics as a physical device. In some cases, as with the Samsung tabletadd-on, you have a preconfigured emulator available for your use.

    The SDK Manager functions as both an update manager and a virtual devicecreation tool. Each time you launch SDK Manager, it connects to the Androiddeveloper site to check for new releases. (You can bypass this process by clicking

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 7 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    8/16

    Cancel.) At this point, you should see the Android SDK and AVD Managerwindow, shown in Figure 6.

    Figure 6. Android SDK and AVD Manager

    Selecting Virtual devices in the directory tree displays all previously definedAndroid virtual devices (AVDs) in the details pane. To create a new emulator device,click New. In the Create New Android Virtual Device (AVD) window, provide therequired information in the Name, Target, and SD Card Size fields. Figure 7 showsthe entries for my test device. The name must not contain spaces, and you shouldallow at least 100MB for storage. Choose the appropriate Android version numberfor your target device. This drop-down list displays only the available optionspreviously downloaded.

    Figure 7. The Create New AVD Wizard

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 8 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    9/16

    Next, click Create AVD. A pop-up window provides the details of your new device.

    To launch any of the available emulator images, select the desired target, and thenclick Start. In the Launch Options window, you can proceed with defaults forscreen size, or you can select the Scale display to real size check box and choosesomething larger. A value of 7 seems to work well (see Figure 8). To launch theemulator with a clean slate, select the Wipe user data check box.

    Figure 8. AVD launch options

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 9 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    10/16

    Another indispensable tool provided with the Android SDK is the Android DeviceBridge (ADB). This tool provides such functions as installing applications (.apk files),copying files to or from a device, and issuing shell commands on the device. Youalso use ADB to actually launch SL4A on a device so that you can executeprograms from your workstation. To establish communication between your hostworkstation and a device, you must use ADB to forward TCP/IP traffic from port9999 to a specific port on the device. Open a Command window, and enter thefollowing command line:

    $ adb forward tcp:9999 tcp:42306

    The second port number comes from the device. With the latest version of the SL4A,you can set this number in the preferences. For the standard release, you have touse the number SL4A gives you.

    Now, launch SL4A, and then click Menu. From the six options at the bottom of thewindow, click View, then click Interpreters (Figure 9).

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 10 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    11/16

    Figure 9. Launch a remote server from the SL4A Interpreters menu

    Click Menu once more, then click Private to launch a private server.

    For a real device, the difference is that Private starts the server using the USB port,and Public uses Wi-Fi. If you view the notifications page again, you'll see that theSL4A service is running (Figure 10).

    Figure 10. Android notification screen

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 11 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    12/16

    Click the message to see the actual port number assigned. In this case, you use portnumber 42306 for the second TCP value in the adb forward command. Now,you're ready to actually write some Python code and test it on the device.

    Hello World in Python for Android

    It's almost obligatory in any introductory programming article to write a "hello world"program. I do that here to demonstrate the number of ways you can write and testyour Python code using SL4A. Here's what the code looks like:

    import androiddroid = android.Android()result = droid.makeToast('Hello, world!')

    Every SL4A scripting language uses an import fileandroid.py for Python, in thiscaseto set up the interface between the script and the underlying Androidapplication programming interfaces (APIs). You can enter this code directly on yourdevice either in the interpreter (refer back to Figure 9) or by using the editor. To usethe interpreter, from the Interpreters screen, launch the Python interpreter byselecting Python 2.6.2. On the resulting screen, you can enter the code above; afterthe last line, you should see a pop-up window with the words "Hello, world!"

    Typing on either an emulated or real device can be tedious. Python's IDLE consoleand editor prove indispensable when combined with the ADB tool to write code on a

    PC and test it on an Android device. The only thing you'll need is a local copy of theandroid.py file. You can either extract it from the python_extras_r14.zip file availableon the SL4A downloads page or transfer it from the device using the adb pullcommand. It's also handy to have a directory named SDCARDat the root of yourprimary system drive to mirror what's on your emulated device. This makes thingseasier from a file path perspective whenever you run a script on the local machinethat needs to access the file system. Figure 11 shows the Hello World script in the

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 12 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    13/16

    IDLE console.

    Figure 11. Hello World in the Python IDLE console

    If you launched the server and executed the adb forward command, you shouldsee no error and the "Toast" message shown in Figure 12.

    Figure 12. Hello World pop-up message

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 13 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    14/16

    In Windows, you can launch an edit window in IDLE by clicking File > New Window.This window gives you a complete edit and test capability from your developmentmachine to either an emulated or real Android device.

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 14 of 16

    http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    15/16

    Resources

    Learn

    Visit the SL4A Google Project site.

    Check out the various SL4A Google groups.

    Learn more at the Python for Android Project page.

    Find the Python resources you need at Python.org.

    Read more developerWorks articles by Paul Ferrill.

    In the developerWorks Open source zone, find hundreds of how-to articles andtutorials, as well as downloads, discussion forums, and a wealth of otherresources for developers.

    Check for mobile updates on the developerWorks Mobile development blog.

    You'll find more Android-related articles on developerWorks.

    Stay current with developerWorks technical events and webcasts focused on avariety of IBM products and IT industry topics.

    Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBMproducts and tools, as well as IT industry trends.

    Watch developerWorks on-demand demos ranging from product installation andsetup demos for beginners, to advanced functionality for experienceddevelopers.

    Follow developerWorks on Twitter.

    Get products and technologies

    Download SL4A, or visit the unofficial release site.

    Download Python for Android.

    Download the Android SDK.

    Download the latest JDK.

    Download and try the IBM Mobile Technology Preview, a set of code samples

    and services to help you get started building mobile applications that extendand integrate into the enterprise. The preview includes a RESTful notificationservice; PhoneGap, an open source framework for building hybrid mobile apps;a lightweight WebSphere Application Server runtime; and sample code to letyou see how it all works.

    IBM WebSphere Application Server Feature Pack for Web 2.0 and Mobileincludes the IBM Dojo 1.7 Toolkit, new mobile and rich Internet application (RIA)

    ibm.com/developerWorks developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 15 of 16

    http://code.google.com/p/android-scripting/http://groups.google.com/group/android-scripting/topics?pli=1http://code.google.com/p/python-for-android/http://python.org/http://www.ibm.com/developerworks/views/global/libraryview.jsp?site_id=1&contentarea_by=All%20Zones&sort_by=Date&sort_order=2&start=1&end=6&topic_by=-1&product_by=-1&type_by=All%20Types&show_abstract=true&search_by=paul%20ferrill&industry_by=-1&series_title_by=http://www.ibm.com/developerworks/opensource/index.htmlhttp://www.ibm.com/developerworks/views/opensource/libraryview.jsphttp://www.ibm.com/developerworks/views/opensource/libraryview.jsphttps://www.ibm.com/developerworks/mydeveloperworks/blogs/mobile/?lang=enhttp://www.ibm.com/developerworks/views/web/libraryview.jsp?site_id=1&contentarea_by=All%20Zones&sort_by=Date&sort_order=2&start=1&end=52&topic_by=-1&product_by=-1&type_by=All%20Types&show_abstract=true&search_by=android&industry_by=-1&series_title_by=http://www.ibm.com/developerworks/offers/techbriefings/events.htmlhttp://www.ibm.com/developerworks/offers/techbriefings/index.htmlhttp://www.ibm.com/developerworks/offers/lp/demos/index.htmlhttp://www.twitter.com/developerworks/http://code.google.com/p/android-scripting/downloads/listhttp://code.google.com/p/android-scripting/wiki/Unofficialhttp://code.google.com/p/python-for-android/downloads/listhttp://developer.android.com/sdk/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.htmlhttps://www.ibm.com/developerworks/mydeveloperworks/blogs/ibmmobile/entry/welcome_to_ibm_mobile_technology_preview?lang=enhttps://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=swg-wasfpweb20http://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=swg-wasfpweb20https://www.ibm.com/developerworks/mydeveloperworks/blogs/ibmmobile/entry/welcome_to_ibm_mobile_technology_preview?lang=enhttp://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.htmlhttp://developer.android.com/sdk/index.htmlhttp://code.google.com/p/python-for-android/downloads/listhttp://code.google.com/p/android-scripting/wiki/Unofficialhttp://code.google.com/p/android-scripting/downloads/listhttp://www.twitter.com/developerworks/http://www.ibm.com/developerworks/offers/lp/demos/index.htmlhttp://www.ibm.com/developerworks/offers/techbriefings/index.htmlhttp://www.ibm.com/developerworks/offers/techbriefings/events.htmlhttp://www.ibm.com/developerworks/views/web/libraryview.jsp?site_id=1&contentarea_by=All%20Zones&sort_by=Date&sort_order=2&start=1&end=52&topic_by=-1&product_by=-1&type_by=All%20Types&show_abstract=true&search_by=android&industry_by=-1&series_title_by=https://www.ibm.com/developerworks/mydeveloperworks/blogs/mobile/?lang=enhttp://www.ibm.com/developerworks/views/opensource/libraryview.jsphttp://www.ibm.com/developerworks/views/opensource/libraryview.jsphttp://www.ibm.com/developerworks/opensource/index.htmlhttp://www.ibm.com/developerworks/views/global/libraryview.jsp?site_id=1&contentarea_by=All%20Zones&sort_by=Date&sort_order=2&start=1&end=6&topic_by=-1&product_by=-1&type_by=All%20Types&show_abstract=true&search_by=paul%20ferrill&industry_by=-1&series_title_by=http://python.org/http://code.google.com/p/python-for-android/http://groups.google.com/group/android-scripting/topics?pli=1http://code.google.com/p/android-scripting/
  • 7/29/2019 [IBM] Android Applications Using Python & SL4A, Part 1 - Set Up Your Development Environment [2011]

    16/16

    building blocks, and a Dojo-based diagram component. With accompanyingRational tools, the Feature Pack helps you take WebSphere applicationsdeveloped originally for desktop browsers and adapt and deploy them to mobiledevices.

    Evaluate IBM products in the way that suits you best: Download a product trial,

    try a product online, use a product in a cloud environment, or spend a few hoursin the SOA Sandbox learning how to implement Service Oriented Architectureefficiently.

    Discuss

    Get involved in the developerWorks community. Connect with otherdeveloperWorks users while exploring the developer-driven blogs, forums,groups, and wikis.

    About the authorPaul Ferrill

    Paul Ferrill has been writing in the computer trade press for more than25 years. He got his start writing networking reviews for PC Magazineon products like LANtastic and early versions of Novell Netware. Paulholds both BSEE and MSEE degrees and has written software for morecomputer platforms and architectures than he can remember.

    developerWorks ibm.com/developerWorks

    Set up your development environment Trademarks Copyright IBM Corporation 2011 Page 16 of 16

    http://www.ibm.com/developerworks/downloads/index.htmlhttp://www.ibm.com/developerworks/downloads/soasandbox/index.htmlhttp://www.ibm.com/developerworks/communityhttp://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/legal/copytrade.shtmlhttp://www.ibm.com/developerworks/ibm/trademarks/http://www.ibm.com/developerworks/communityhttp://www.ibm.com/developerworks/downloads/soasandbox/index.htmlhttp://www.ibm.com/developerworks/downloads/index.html