Creating a Java VM from Native code

On Android init phase we are running init application that uses its init scripts to start all the code system services. We can add a new daemon easily if we are building a ROM by add an empty folder, adding Android.mk file and C/C++ source file.

In this way we can run C/C++ code but what about Java? The problem with Java is the runtime requirement – java code is running inside java virtual machine(JVM) that provides all the functionality and features such as class loading, garbage collector, security manager etc.

So to run Java code we need to create an Android runtime (ART/Dalvik) and thanks to the fact that Android is open source its very easy

If we follow the Android init phase, we find an important process that do the job for us – app_process (called Zygote on android)

The code is located under /frameworks/base/cmds/app_process

We can see from the code that we have an abstract C++ class  – AndroidRuntime , we need to derived from and a simple function start() to run

So to call a main function (static void main) in any Java class we need to write the following code:

We need to build it as a simple c++ application and it will call the static main function in com.android.internal.os.TestJava class (or any other class we specify), we can also send arguments using the second parameter

 

The Java code can be simple

To use Android Activities, Services etc you need to create a context and as you can see from the comments you can use the ActivityManagerNative class for it

To learn more about ActivityManagerNative see the source code for the command “am” here

 

You can find the complete code with Andoid.mk file in our github

 

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *