Android applications on JPHP

People who write PHP code will do everything not to switch to other languages. Yes, I know that earlier on Habré there was already a way to write Android applications in PHP, but it was decided to adapt it to a faster and faster Java-compatible JPHP.


About JPHP

I assume that you may have a question. And what about JPHP? The answer is right there on Habré:

Well, we will continue. Over the 4 years that have passed since writing these posts about JPHP, this language has changed for the better. For example, our own package manager jppm has appeared about which we will also have to talk about today too.

How it all began

It all started when my friend got the idea to create Android apps on JPHP. I gave him the post that we talked about at the beginning, but he didn’t like this method because it uses regular PHP. Well, we are developers who write in an unknown language JPHP decided to do everything themselves.

Having looked through the Internet, we then did not find another way out other than how to use JavaFXPorts. Yes, and the creator of this language wanted to use it as a GUI engine for JPHP for 2 years, without having done anything.

As they say - "Who else but you"? .. I rushed to globally study the work of the JPHP language.

The first few weeks were unsuccessful. I have already written an automatic generation of the build script for gradle and everything would be fine, apk was going to, but when I started it was an error unknown to me at that time. I immediately realized that she was due to JPHP, but I had not seen one before. Now I can say for sure that this error was due to the generated bytecode. DalvikVM tritely could not run the bytecode generated for the JVM. This has become a huge obstacle. Since PHP files in JPHP applications are not compiled at all. There was an option with phb files, but this did not solve our problem at all. The only way was to write your own compiler, which really worked, but brought a number of limitations related to include and eval, thanks, Dalvik.

After many trial and error, I still managed to make the very first version. Its source is here . This version was not very fast. Yes, and use JavaFX on Android is mazahizm.

By this, I decided to rewrite everything from scratch. I made this decision relatively recently. That's why the JPHP library is a bit damp. But it works.

How it works

It all starts with the fact that jppm collects all your source code and dependencies in one jar file. Then compiling them all the files phpin the classfiles. After that, the resulting jar file is added depending on the gradle. And he already in turn compiles these class files in dex. That's the whole secret.

With the launch of the application all the more difficult. In the AndroidManifest.xmloriginal, only one BootstrapActivitythat loads the entire JPHP. After downloading this BootstrapActivitycan be changed from JPHP. For ActivityI created classes of the same name.

$bootstrapActivity = Application::getMainActivity();

With this code you can get the one BootstrapActivityfrom which the JPHP was loaded.

I think you have already become aware of the work of the JPHP loader.

Small example

For example, in order to create the most primitive clicker you need to use this code:

Application::setMainActivityHandler(fn => { // лямбды в jPHP
       $activity = Application::getMainActivity(); // Получаем BootstrapActivity
       $activity->setTitle("test"); // Добавляем заголовок  
       $activity->setContentView($button = new Button($activity)); // Создаём и добавляем кнопку 
       $button->text = "Hello from JPHP!"; // Ну тут всё понятно
       $button->on("click", function()use($button){ // При нажатии на кнопку ...
          $button->text = "Clicks: " . $GLOBALS['clicks'];

As a result, we got a simple application with a button:


I do not think that my project will be interesting to anyone. Since he does not describe so far and 10% of the Android API. Yes, and writing applications for Android in PHP is not canon. But I think the project will find its audience.

Github project
Github JPHP
Group in VK

Also popular now: