The DEX translator

dex_translate_class (src/android/dex/translate.h) makes a version 52 class file, with stack map frames, from a DEX class. Dalvik registers have no types, and JVM locals do. The translator gives each register up to five JVM locals of fixed types, one for each way the code reads it: int, float, long, double and java/lang/Object. Each local gets a value at the method's entry and keeps its type, so every branch target has the same frame. A value that the code needs as a narrower class gets a checkcast where it is used.

A dataflow pass over the registers decides what the bytecode alone does not tell:

ART allows some things that the JVM verifier does not. The translator rewrites them:

Some calls of the JDK behave otherwise on Android, and the translator sends them to the framework's own (its remap table): System.loadLibrary to the native bridge, ByteBuffer.allocateDirect to a buffer native code reaches, and what prints a double or a float (Double.toString, Float.toString, String.valueOf, StringBuilder.append and insert, StringBuffer's, PrintStream.print and PrintWriter.print) to snx.dalvik.Digits. ART prints OpenJDK's digits from before JDK 19 (Double.toString(1.0E23) is 9.999999999999999E22), and the JDK the shortest since (1.0E23): Digits calls the JDK's jdk.internal.math.FloatingDecimal, which still has the old ones, and which the driver exports to the framework. A box printed through Object.toString, String.valueOf(Object) or a Formatter's %s has the JDK's digits.

DEX annotations become JVM attributes, so that reflection finds them: the runtime-visible ones become RuntimeVisibleAnnotations and RuntimeVisibleParameterAnnotations, dalvik.annotation.Signature becomes Signature, and AnnotationDefault becomes each element's AnnotationDefault (src/android/dex/annotations.c). The other system annotations (inner classes, enclosing methods, throws) are dropped.

A method that the translator cannot translate gets a body that throws VerifyError with the reason. It does not make the whole class fail.


Made from docs/platforms/android-apps/README.md at 69237645142c. GPL-3.0-or-later.