Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Generating Dalvik Bytecode into a running Dalvik/Android application

Tags:

This question has been asked(and answered) many times about dynamically generating and loading java bytecodes at runtime into a running Dalvik VM, but is there any way to load dex files/bytecodes into an app at runtime?

Thanks

like image 327
Michael Kohout Avatar asked Apr 09 '10 21:04

Michael Kohout


People also ask

Are Android applications compiled to Dalvik byte code?

Dalvik programs are written in Java using the Android application programming interface (API), compiled to Java bytecode, and converted to Dalvik instructions as necessary. A tool called dx is used to convert Java .

Why does the Android architecture use the Dalvik virtual machine to run applications?

Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable ( . dex ) format which is optimised for minimal memory footprint.

What is Dalvik bytecode?

The DVM is a virtual machine to run Android applications. The DVM executes Dalvik bytecode, which is compiled from programs written in the Java language. Note that the DVM is not a JVM. One of the key design principles of the DVM is that it should run on low memory mobile devices and loads quicker compared to any JVM.

What is DVM and ART Android?

ART vs DVM in AndroidDVM was explicitly designed for mobile devices and was used as a virtual machine to run android apps up until Android 4.4 Kitkat. Starting from this version, ART was introduced as a runtime environment, and in Android 5.0 (Lollipop), ART completely replaced Dalvik.


1 Answers

The Dalvik team would like to build a first-class runtime code generation library. We're tracking the feature request as Android bug 6322. Unfortunately, we have a very long list of performance and correctness issues, so I can't give you a timeline for when we'll spend time on this issue.

There are some alternatives, but they will all take some work:

  • Run your application on a standard JVM and exercise all runtime code generation there. Dump the .class files from memory to files, and then run dx on those files. If you're quite sophisticated, you could integrate all of this work into your build.

  • Include the open source dx tool as a project library, and execute it programatically from within your application, possibly in your application's classloader. This will bloat your application's binary.

like image 60
Jesse Wilson Avatar answered Nov 02 '22 04:11

Jesse Wilson