Building Android Apps: Creating a Dynamic Manifest File

1/8/2011 3:36:50 PM
Now that you’re comfortable with how the offline app cache works, let’s apply it to the Kilo example we’ve been working on. Kilo consists of quite a few files and manually listing them all in a manifest file would be a pain. Plus, a single typo would invalidate the entire manifest file and prevent the application from working offline.

Running PHP Scripts on Your Web Server

PHP is a versatile web-scripting language, and is supported by most web hosting providers. This means that on most web servers, you can create a file whose name ends with the extension .php, add some PHP code to it, visit it in your web browser, and it will just work. If you’ve been using a web server on your personal computer to serve up pages to your Android phone, you’ll need to get set up to run PHP scripts. If you’re running a web server on Windows, see for downloads and information. You may also want to use a solution such as EasyPHP or check out the Wikipedia page on this topic at

PHP is easy to install on Linux. For example, Ubuntu users can type sudo aptitude install apache2 php5 at a shell prompt. To enable PHP in a user’s personal public_html directory, edit the file /etc/apache2/mods-available/php5.conf as root and follow the instructions inside it to comment out a series of lines (by putting a # in front of each one).

Macs come with PHP installed, but you need to take a step to enable PHP like you did in Mac OS X and the .htaccess File:

  1. Open Applications UtilitiesTerminal and type these commands (you’ll need to type your password when prompted):

    cd /etc/apache2
    sudo pico httpd.conf

  2. Press Control-W. This brings up the option to search the file. Type php5 and press Return. This brings you to a line that should look like this:

    #LoadModule php5_module        libexec/apache2/

  3. Using the arrow keys, move to the beginning of the line and delete the # comment character, which is preventing this line from having any effect.

  4. Press Control-X to exit, answer Y to save changes, and press Return to save the file.

  5. Next, start System Preferences, go to Sharing and, if needed, click the lock icon labeled “Click the lock to make changes” and type your password when prompted.

  6. Clear the checkbox next to Web Sharing and then check it again. Now PHP should be enabled on your Mac’s web server.

  7. Create a file in the Sites subdirectory of your home folder named test.php with these contents:


  8. Finally, visit the following URL in your browser: http://localhost/~YOURUSERNAME/test.php. Replace YOURUSERNAME with your username, but don’t delete the ~ (you can discover your username at the Terminal by typing echo $USER and pressing Return). If PHP is working, you’ll see a table displaying your PHP version number and a lot of other information about your PHP installation. If it is not working, you’ll see nothing but a blank page. Visit for links to documentation and help with using PHP.

Figure 1. A single fallback image will show up in place of multiple images when the user is offline

Figure 2. Hosted images will show up normally when the user is online

To address this issue, we’re going to write a little PHP file that reads the contents of the application directory (and subdirectories) and creates the file list for us. Create a new file in your Kilo directory named manifest.php and add the following code:

header('Content-Type: text/cache-manifest');

$dir = new RecursiveDirectoryIterator(".");
foreach(new RecursiveIteratorIterator($dir) as $file) {
if ($file->IsFile() &&
$file != "./manifest.php" &&
!strpos($file, '/.') &&
substr($file->getFilename(), 0, 1) != ".") {
echo $file . "\n";

To the browser, manifest.php will look like this:



Try loading the page yourself in a browser (be sure to load it with an HTTP URL such as http://localhost/~YOURUSERNAME/manifest.php). If you see a lot more files in your listing, you may have some extraneous files from the jQTouch distribution. The files LICENSE.txt, README.txt, and sample.htaccess are safe to delete, as are the directories demos and extensions. If you see a number of directories named .svn, you may also safely delete them (unless you have put your working directory under the SVN version control system, in which case these files are important). Files beginning with a . will not be visible in the Mac OS X Finder or Linux File Manager (but you can work with them at the command line).

Now open index.html and add a reference to manifest.php in the head element like so:

<html manifest="manifest.php">

Now that the manifest is generated dynamically, let’s modify it so its contents change when any of the files in the directory change (remember that the client will only redownload the application if the manifest’s contents have changed). Here is the modified manifest.php:

header('Content-Type: text/cache-manifest');

$hashes = "";

$dir = new RecursiveDirectoryIterator(".");
foreach(new RecursiveIteratorIterator($dir) as $file) {
if ($file->IsFile() &&
$file != "./manifest.php" &&
substr($file->getFilename(), 0, 1) != ".")
echo $file . "\n";
$hashes .= md5_file($file);
echo "# Hash: " . md5($hashes) . "\n";

Here’s an example of what the manifest looks like with this change (some of the lines have been truncated for brevity):

# Hash: ddaf5ebda18991c4a9da16c10f4e474a

The net result of all of this business is that changing a single character inside any file in the entire directory tree will insert a new hash string into the manifest. This means that any edits we make to any Kilo files will essentially modify the manifest file, which in turn will trigger a download the next time a user launches the app. Pretty nifty, eh?

Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
Popular Tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone