Feasibility of developing Android applications using PHP

Source: Internet
Author: User
You may have heard of such a thing as PFA (PHPforAndroid) before. it can be concluded that this project is no longer active and failed, because it depends on SL4A (ScriptingLayerforAndroid) update and maintenance have been stopped, and it depends on the running mode of SL4A... you may have heard of PFA (PHP for Android) before. we can conclude that this project is no longer active and failed, because the dependent SL4A (Scripting Layer for Android) has stopped update and maintenance, in addition, it depends on the running mode of SL4A, which is also different from the local Browser/Server mode of PHPDroid mentioned below.

I personally used the cross-compilation tool chain musl-cross-compilers on Ubuntu to build a binary PHP interpreter for the Linux ARM architecture by referring to the DroidPHP tutorial. the DroidPHP tutorial uses the arm-none-linux-gnueabi tool chain. I found it difficult, and then I saw PocketMine, A MineCraft Android server built with PHP7 uses musl-cross-compilers, so I also use this tool chain. I made some changes, such as modifying the shell path of the libc library and resolv related to DNS. conf and hosts are mainly used to enable PHP to run on Android.

Now we have a PHP interpreter that can run on Android. next we will look at how to build the App. because the PHP interpreter has built a cli http Server since 5.4, which is a single-process Web Server implemented by C that supports PHP programming. it is very lightweight and officially positioned as a development test. it also supports the following feature: you can specify a PHP route script for it at startup:
php -S 127.0.0.2:8080 -t /www auth.php
For example, the auth. php in the PHPDroid package is as follows:


  

We also know that PHP also has built-in SQLite engine, and a file is a database, which is very convenient to manage. Visual Management tools can use free and open-source cross-platform SQLiteStudio. now with the HTTP server and SQLite database, some common extensions are also enabled during compilation, as shown below:

bcmath calendar Core ctype curl date dom exif filter ftp gd hash json libxml mbstring mcrypt openssl pcre PDO pdo_sqlite Reflection session SimpleXML sockets SPL sqlite3 standard swoole xml xmlreader xmlwriter zip zlib

With the above, how can we perform graphical interaction? I think everyone has come up with the built-in Android WebView. I personally think that the browser is the most successful cross-platform GUI application in history. since Android 4.4, the underlying layer of WebView is driven by Chromium, and the performance is quite good. if you want to call some local functions written in Java, WebView always provides the addJavascriptInterface feature. you can inject Java objects into WebView for JS calls, for example:

Java code: webview. addJavascriptInterface (new Object () {@ JavascriptInterface public String getprop (String prop) {return java. lang. system. getProperties (). getProperty (prop) ;}}, "android"); HTML code in WebView: OS. name

That is to say, you can use WebView to call some local Apis. of course, you must first write those functions in Java.

Now the entire running mode of PHPDroid is clear. when the application is started, run start. sh randomly generates and records the UserAgent (ua. php), find the available port and record it (port. php), and then start the PHP built-in HTTP server and record the PID (used to disable). before starting WebView in Java, read the UserAgent randomly generated and setUserAgentString settings. PHP built-in server routing auth. php compares UserAgent and rejects requests from other applications (such as browsers) on the local machine.
The scripts are as follows:

Start. sh #! /System/bin/shcd $1/php/binchmod 700 busyboxif [! -F php]; then. /busybox xz-d file.tar. xz. /busybox tar xf file.tar & rm file.tar chmod 700 lib/ld php watcher qrencode. /busybox sed-I "s @/data/net. php. phpdroid @ $1 @ g "php. ini. /busybox sed-I "s @/storage/self/primary @ $2 @ g" php. ini cp resolv. conf $1/php/www cp hosts $1/php/wwwfi # generate UserAgent randomly. /php-c php. ini ua. php # obtain available ports. /php-c php. ini port. php # Copy the root directory of the website to the SD card during development and debugging for convenient modification #-t $1/php/www Website root directory #-t $2/phpdroid development and debugging website root directory if [! -D $2/phpdroid]; then cp-R $1/php/www $2/phpdroid; fi # Start the PHP service $1/php/bin/php \-c php. ini \-S 127.0.0.2: 'Cat $1/php/bin/port' \-t $2/phpdroid \ $1/php/bin/auth. php \>/dev/null 2> & 1 & # Record PHP PIDecho $!> Pid # listen and find the file auth. if php is deleted, the PHP process $1/php/bin/watcher $1/php/bin/auth will be disabled. php>/dev/null 2> & 1 & # Record watcher's PIDecho $!> Pid_watcherreturn 0stop. sh #! /System/bin/shua = $1/php/bin/uaif [-e $ ua]; then rm $ uafiport = $1/php/bin/portif [-e $ port]; then rm $ portfipid = $1/php/bin/pidif [-e $ pid]; then kill-9 'cat $ pid 'rm $ pidfipid = $1/php/bin/pid_watcherif [-e $ pid]; then kill-9 'cat $ pid 'rm $ pidfireturn 0ua. php 

Now, we can see that you have basically understood the idea of using PHP to develop Android applications.
If you are interested, download the packaged APK and try it out:
Phpdroid_20160703.apk (5.8 M)
PHP roid_20160703.7z (4.7 M)
Apk contains PHP's latest 7.0.8 and high-performance network programming extensions Swoole,
In addition, there are BusyBox and qrencode for generating the QR code.
The 7z package is the project source code, mainly MainActivity. java and assets data.
Note: The APK package is in start. sh to adjust the root directory of the website to the phpdroid Directory of the SD card, which is mainly used to facilitate user experience. when you connect to the mobile phone via USB, put the php file you have written into it to run the test.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.