Outils pour utilisateurs

Outils du site


numero61:python_p._7-9

Ceci est une ancienne révision du document !


This time, we’ll set up the Android SDK on our Linux desktop. We’ll also create a virtual Android device, install SL4A and python on it, and do a quick test.

Please be aware, this is not something you would want to do for machines that have less than 1 GB of ram. The emulator eats up a huge amount of memory. I’ve tried it on a laptop running Ubuntu with only 512 MB of ram. It WILL work, but it is REALLY slow.

Here’s a quick list of what we’ll do. We’ll go step-by-step in a minute. • Install the Java JDK6. • Install the Android SDK starter pack. • Create and setup AVDs. • Test AVD, and install SL4A and Python.

In reality, we should also install Eclipse and the Android ADT plugin for Eclipse, but, since we won’t be dealing with Eclipse in this set of articles, we can bypass that. If you want to include those steps, head over to http://developer.android.com/sdk/installing.html to see all the steps in the suggested order. Let’s get started.

STEP 1 - Java JDK 6

From everything I’ve read and tried, it must be the actual Sun release. OpenJDK is not supposed to work. You can find information on this on the web, but here’s the steps that I did. In a terminal, type the following…

sudo add-apt-repository ppa:ferramroberto/java

sudo apt-get update

sudo apt-get install sun-java6-jdk

Once everything here is done, you will want to edit your .bashrc file to set “JAVA_HOME” so everything runs correctly. I used gedit and, at the bottom of the file, I added the following line…

export JAVA_HOME=”/usr/lib/jvm/java-6-sun-1.6.0.06”

Save the file and move on to step 2.

STEP 2 - Android SDK Starter Pack

Now the actual “fun” begins. You’ll want to go to developer.android.com/sdk/index.html. This is where the SDK is located. Download the latest version for Linux, which, at the time of this writing, is android-sdk_r18-linux.tgz. Using Archive Manager, unpack it somewhere convenient. I put it in my home directory. Everything runs directly from this folder, so you really don’t have to install anything. So the path for me is /home/greg/android-sdk-linux. Navigate to this folder, then go to the tools folder. There you will find a file called “android”. This is what runs the actual SDK. I created a launcher on my desktop to make it easy to get to.

Now the boring part. Run the android file, and the Android SDK Manager will start. It will go out and update the platforms that are available. I will warn you now that this process will take some time, so don’t bother if you don’t have a lot of time to deal with it. For the sake of brevity, I would suggest you get only one platform to start. A good one to begin with is the Android 2.1 platform, since, for the most part, if you develop for an older platform, there should be no problem running on a newer platform. You also need to get the Tools set as well. Simply check the box next to those two items, then click on the install button. Once you get the platform of your choice, and the tool set, you are almost ready to create your first virtual machine.

STEP 3 - Create and set up your first AVD

Back in the Android SDK Manager, select Tools from the main menu, then select Manage AVDs. This will open a new window. Since this is the first time, there won’t be any virtual devices set up. Click on the “New” button. This opens yet another window where we define the properties of the virtual Android device. Here’s the steps that you should use to set up a simple Android emulator device: • Set the name of the device. This is important if you have more than one device. • Set the target platform level. • Set the size of the SD card (see below). • Set the skin resolution. • Create the device.

So, In the name text box, type “Test1”. Under the target combo-box, select Android 2.1 - API Level 7. In the text box for “SD Card:” enter 512 and make sure the dropdown shows “MiB”. Under “Skin”, set the resolution to 800×600. (You can play with the other built-in sizes on your own.) Finally, click the “Create AVD” button. Soon, you’ll see a message box saying that the AVD was created.

STEP 4 - Testing the AVD and installing SL4A and Python

Now, finally, we can have a bit of fun. Highlight the AVD you just created and click on the Start button. In the dialog box that pops up, simply click the “Launch” button. Now, you have to wait a few minutes for the virtual device to be created in memory, and the Android platform to be loaded and started. (We’ll talk about speeding this process up in later runs.)

Once the AVD starts up and you have the “home” screen up, you will install SL4A. Using the browser or the google web search box on the home screen, search for “sl4a”. Go to the downloads page, and you’ll eventually find the web page for the downloads at http://code.google.com/p/android-scripting/downloads/list.

Scroll down the page until you get to the sl4a_r5 link. Open the link and tap on the “sl4a_r5.apk” link. Notice I said “tap” rather than “click”. Start thinking about using your finger to tap the screen rather than clicking the mouse. It will make your programming transition easier. You’ll see the download start. You may have to pull down the notification bar at the top to get to the downloaded file. Tap on that, then tap the install button.

Once the file is downloaded, you’ll be presented with the option to open the downloaded app or to tap “Done” to exit the installer. Here we will want to tap “Open”.

Now SL4A will start. You’ll probably see a dialog asking if you will agree to usage tracking. Either accept or refuse this - it’s up to you. Before we go any farther, you should know some keyboard shortcuts that will help you move around. Since we don’t have a “real” Android device, buttons like Back, Home, and Menu, aren’t available. You’ll need them to navigate around. Here’s a few important shortcuts.

Back - Escape Home - Home Menu - F2

Now we will want to download and install python into SL4A. To do this, first tap Menu (press F2). Select “View” from the menu. Now select “Interpreters”. It looks like nothing happened, but tap Menu again (F2), then select “Add” from the popup. Now scroll down and select “Python 2.6.2”. This will download the base package for Python for Android. Install the package, then open it. You will be presented with four options. Install, Import Modules, Browse Modules, and Uninstall Module. Tap on Install. This will start downloading and installing all the pieces of the latest Python for Android. This can take a few minutes.

Once everything is done, tap Back (escape key) until you get to the SL4A Interpreters screen. Now everything is loaded for us to play in Python on Android. Tap Python 2.6.2, and you’ll be in the “standard” Python shell. This is just like the shell on your desktop. Type the following three lines, one at a time, into the shell. Be sure to wait for the “»>” prompt each time.

import android

droid = android.Android()

droid.makeToast(“Hello from Python on Android”)

After you type the last line and press Enter, you’ll see a rounded corner box at the center bottom of the shell that says “Hello from Python on Android”. That’s what the “droid.makeToast” command does.

You’ve written your first Python script for Android. Neat, huh?

Now let’s create a shortcut on the Android home screen. Tap the Home key (Home button). If you chose the 2.1 platform, you should see a slider bar on the far right of the screen. If you chose another platform, it might be a square or rectangle consisting of small squares. Either way, this gets you to the Apps screen. Tap that, and find the SL4A icon. Now perform a “long tap” (long click), which will create a shortcut on the Home screen. Move the shortcut wherever you want it.

Next, we will create our first saved script. Go back into SL4A. You should be presented with the sample scripts that come with Python 4 Android. Tap the Menu button and select “Add”. Select “Python 2.6.2” from the list. You’ll be presented with the script editor. At the top is the filename box with “.py” already filled out. Below that is the editor window that already has the first two lines of our program entered for us. (I included them below in italics so you can check it. We also used these two lines in our first sample.)

import android

droid = android.Android()

Now, enter the following two lines to the python script.

uname = droid.dialogGetInput(“What’s your name?”)

droid.makeToast(“Hello %s from Python on Android”) % uname.result

The first new line will create a dialog box (droid.dialogGetInput()) that asks for the user’s name. The response is returned to our program in uname.result. We’ve already used the droid.makeToast() function.

Name the file andtest1.py, then tap Done, and tap “Save & Run”. If everything worked, you should see a dialog box asking for your name. After you enter it, you should see the alert at the bottom of the screen saying “Hello Your Name from Python on Android”.

That’s all for this time. For now, there’s a TON of documentation about SL4A for free on the web. You can play a bit on your own until next time. I’d suggest that you start by going to http://code.google.com/p/android-scripting/wiki/Tutorials.

ANNONCE

Santa Clara Convention Center

O'Reilly are looking forward to celebrating Velocity's 5th Year with you June 25-27, at the Santa Clara Convention Center. You'll meet the smartest people working in web performance and operations at the O'Reilly Velocity Conference. Web and mobile users expect better performance than ever before. To meet, and exceed, their expectations, you need to master a daunting array of web performance, operations, and mobile performance issues. Velocity offers the best opportunity to learn the newest info on what you need to know to build a faster and stronger web.

Take advantage of this rare opportunity to meet face-to-face with a cadre of industry leaders who are taking web performance and operations to the next level. Velocity packs a wealth of big ideas, know-how, and connections into three concentrated days. You'll be able to apply what you've learned immediately and you'll be well prepared for what lies ahead with four in-depth tracks covering the key aspects of web performance, operations, mobile performance, and Velocity culture.

Velocity has sold out the last two years, so if you want to reserve your spot at Velocity 2012, register now and save an additional 20% with code FULLCIR.

numero61/python_p._7-9.1338040268.txt.gz · Dernière modification : 2012/05/26 15:51 de andre_domenech