Pages

Subscribe:

Thursday, February 24, 2011

PHP on Android: Run SL4A + PHP in Android Emulator via Eclipse.


This tutorial assumes that you have set up Eclipse with Android SDK as described here http://developer.android.com/sdk/installing.html. Make sure to install Android SDK 1.6. You will need to create an Android Virtual Device (AVD) that runs on platform 1.6 – we will do this later.
There are really two stages to this project: get SL4A (Scripting Languages For Android) working in the emulator and then install PhpForAndroid.apk on your virtual device. Let’s get started.

1. Install TortoiseHG for Windows from here http://mercurial.selenic.com/downloads/ We need this to download the source of SL4A that is kept in a Mercurial repository. TortoiseHG is a program to manage Mercurial repositories.
2. Go to the directory where you want to download the source of SL4A, I suggest your Eclipse workspace. Right click and click on TortoiseHG -> Clone.. In the source enter the url that appears under Command Line access here http://code.google.com/p/android-scripting/source/checkout, currently https://android-scripting.googlecode.com/hg/android-scripting
3. Open Eclipse. Click File->Imports. Then Select an Import Source -> General -> Existing Projects into Workspace. Then Next. Select root directory. Browse to the folder where you downloaded the SL4A source (folder called android-scripting, by default). That should automatically import all the files.
4. Now, if you see that there are error indicators next to the folders (little red x’s) in the Package Explorer, then this is most likely a build path issue.
In Eclipse, go to Window->Preferences->Java->Build Path->Classpath Variables. Click New. Enter Name: ANDROID_SDK. Path: browse to the folder where the Android SDK is located.
For example, I downloaded and unpacked the SDK to E:\AndroidSDK\, so I automatically got this: E:\AndroidSDK\android-sdk-windows. So in the Path section I browsed to E:\AndroidSDK\android-sdk-windows. Click OK. That should set the correct build path and remove the errors.
If you still see error messages, then right click on the file (in the Package Explorer) and select Build Path -> Configure Build Path. Then inspect the path. Sometimes it might be set to a non-existent sdk version (happened to me with the BluetoothFacade).
5. Time to launch the emulator!! Now, I only tried 2 AVD’s: 2.1, which failed and then AVD 1.6 which worked. So I suggest you make sure you have 1.6 AVD ready to go. If not, we’ll make one in a second.
So, in Package Explorer select ScriptingLayerForAndroid (it might have a warning sign next to it but that won’t stop it from running). Click Run As.. -> Run Configurations. The Name will say ScriptingLayerForAndroid, click on the Target tab, then click on Manager … button and Android SDK and AVD manager appears.
Click New.. Enter a Name such as Android_1_6 and select a Target of Android API 1.6. Set the SD Card size to 9 or more.
Click Create AVD.
Make sure the new device appears and then close the window. Back in Run Configurations window, select Automatic and check mark Android_1_6. Click Run. Pray. The emulator should start and install the SL4A.
Once the emulator is open, open the SL4A app and play around. Use this as your guide http://code.google.com/p/android-scripting/wiki/UserGuide. Basically, the Menu button (right pane on the emulator) is your friend.
6. OK, so now I assume your SL4A app is installed and open. We are going to install the PHP package now.
Click on Menu (right side pane), then View->Interpreters. Right now it will just say Shell. Click on Menu->Add. Then click on PHP 5.3.3. The browser will open and try to download http://php-for-android.googlecode.com/files/phpforandroid_r1.apk Well, as of the writing of this tutorial, it’s not going to find the file. So, click on Menu->Go and fix the download path to http://php-for-android.googlecode.com/files/phpforandroid.apk Click Go. Once the package is downloaded click on Install.
Then, once you are back at (or open again) SL4A you will see a bunch of PHP scripts. Click on hello_world.php then select the round wheel icon. And there you go, you have PHP running on Android.

0 comments:

Post a Comment