October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Build Your First PHP for Android Application

  • September 20, 2010
  • By Keith Vance
  • Send Email »
  • More Articles »

Google's open source Android mobile operating system is taking the smartphone market by storm. Unlike Apple, which has stringent guidelines and requirements for developers who want to offer their applications on the iPhone App Store, Google has left the Android platform wide open. You can even write Android applications in PHP now. The folks at Irontech have created a PHP port to run on Android, and with the Scripting Layer for Android (SL4A), you can build PHP Android applications.

In this article, I'll explain how to install, set up and use PHP for Android and SL4A, I'll present a demo application as an example, and I'll give a first-hand account of the PHP for Android developer experience.

Installing PHP for Android

To install PHP for Android, you have to have a phone or emulator running Android version 1.5 or higher and you must enable "Unknown Sources" under Application settings. After you have that set, you simply install the SL4A environment and the PHP for Android APK.

Installing the SL4A is straightforward, but after you install the PHP for Android application, you need then click "install" again for it to be fully installed and functioning. If you have trouble with the installation, there's a handy video demonstration available on Vimeo to walk you through the process.

 


Visit the Android Dev Center

 

Setting Up the PHP for Android Development Environment

If you installed PHP for Android, theoretically, you can write PHP Android applications with your phone. But for all practical purposes, that doesn't work very well. What you should do is download the Android SDK, set up an emulator, and write code using your favorite editor.

After you've downloaded the SDK, extract it in a directory of your choosing, run the Android application located in the tools directory, and set up an emulator. From the Android SDK and AVD Manager menu, select Virtual Devices and click the New button. Name your new emulator (e.g. "Droid2") and select Android 2.2 as the target. Enter 10 MiB for SD Card size and click Create AVD.

Now that you've got the Droid emulator set up, click the Start button. Here's where things get a little tricky, because you can't just copy files to the virtual device you just set up. You have to set up port forwarding and push your PHP script to the virtual device using a program called adb, which is part of the Android SDK. It is located in the tools directory too.

Next, you will start a server on the virtual device. You will connect with this server to send your scripts. The following steps will get you up and running as quickly as possible (You can read the full documentation for this process here).

  1. With your new virtual device running, go to the Applications screen and click SL4A.

  2. In the SL4A screen, click the Menu button, select View and choose Interpreters.
  3. Click Menu again, select Start Server and choose Private.
  4. Drag the Android notification bar down and you should see SL4A Service. (Click the service and note the port number your server is listening on, e.g. 47000.)
  5. Open up a shell or command prompt and set up port forwarding using the adb tool. For example, enter the command adb forward tcp:9999 tcp:47000, replacing 47000with your port number.
  6. Set up the AP_PORT environment variable. On UNIX or Mac, run export AP_PORT=9999. On Windows, type set AP_PORT=9999.
  7. To test your script with your emulator, just run adb push my_script.php /sdcard/sl4a/scripts, replacing my_script.php with the script you wrote.

You can also set this up to work with an actual phone. Just follow all of the steps you did with your emulator on your phone. To make things easier, you also should set up an ANDROID_HOME environmental variable that points to your Android SDK location and add the tools subdirectory to your path.


Tags: PHP, Android

Originally published on http://www.developer.com.

Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel