top of page

Using the Runtime Converter to target Android

The Runtime Converter is designed for converting servers and command line tools, but it can also be useful for Android developers who want to convert PHP code. Java on the server and desktop, provided by Oracle or OpenJDK, and the version of Java provided by Android have only very minor differences.

What will work

All of your code will convert and compile for Java on Android. You will need to include via gradle (directions below) the runtime converter support library.

What will not work

Some elements of the support library rely on native code. Native code is not compiled for Android because it is not a major development goal for the Runtime Converter. For example, the function "hexdec" is not currently provided for the Runtime Converter Library via a Java implementation, but rather uses JNI binary code.

A list of functions implemented in Java vs. native is not publicly available, but you can see the list of non-Java functions in your converted project by browsing to "globalNamespace.functions.NamespaceGlobal". This file is found at "src/main/java/com/project/convertedCode/globalNamespace" in your project zip.

Additionally, you will not be able to use your converted project as if it were a web-sever and to take requests. In theory it is possible to implement, but in practice that would be a large development project. You would need to provide an instance of "Request" and "Response" from "javax.servlet.http" and either manage routes or embed Java web server like tomcat.

Implementing Functions

To work around missing functions on the Android platform, you can implement the needed functions. The functions are instances of "com.runtimeconverter.runtime.functions.Callable" class. Specifically, you should subclass "com.runtimeconverter.runtime.functions.FunctionBaseRegular" and implement the method "call". The signature for this function is "Object call(RuntimeEnv env, Object... args)". Then finally replace the static variable in "NamespaceGlobal" class.

Refactoring Code

In some cases, you may want to look at manually altering the converted code instead of implementing the function interfaces. This could have the advantage of cleaning up the code, but won't be efficient if a function is used many times. In some cases, this may be your only option, as it will not be worthwhile for you to personally implement the PCRE, CURL, and PDO modules, for example. There is a workaround for using PCRE functions in Java/Android (docs available on request).

Paid Support

We have invested much time and effort in implementing PHP functions in Java, and therefore have expertise in this area. Such expertise is not cheap and should not be compared with the prices on php2java.com.

Updating build.gradle

To use the runtime converter support library with Android, you will need to update the build.gradle file for your project.

First, add the runtime converter maven repository:

repositories {

mavenCentral()

maven {

url "https://s3.amazonaws.com/runtimeconverter-releases/maven/release"

}

}

The line "mavenCentral()" should already be in your Android project, but it may say "google" or "jcenter", etc. Just add the "maven" block.

Then in your dependencies list, add

compile group: 'com.runtimeconverter', name: 'runtimeconverterlib', version: '3.1'

Getting a "RuntimeEnv" instance

The function signatures on your converted project all require an instance of the "RuntimeEnv" interface. There are a few instances available. The recommended is "RuntimeEnvJava". To get an instance, call "com.runtimeconverter.runtime.runtimeEnv.RuntimeEnvJava.getForCli(project)" where project refers to "com.project.convertedCode.main.ConvertedProject.INSTANCE".

The instance of "RuntimeEnvJava" acquires state (static variables, primarily) and it is not thread safe. To share global and static variables between threads, use java.util.ExecutorService.newSingleThreadExecutor() or another single threaded model. You can also create and dispose of "RuntimeEnv" as needed.

Using Includes

It is best to avoid using includes, if possible, but instead to refactor your PHP code to use functions or classes. Includes have no return value and can only output to the console or to the HTTP response in the case of a web server.

Output Buffering

Output buffering (ob_start() functions) are supported by RuntimeEnvJava. You can dynamically include a script inside of a function and then collect the output buffer to return, if that is needed.

Moving code to Android Studio

Not all of the generated code will be useful to Android. You should delete Application.java and servlets directory, as well as all top-level files except "src". It is best not to rename or refactor "com.project.convertedCode" package until you have tested your application.

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page