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:
if-eqz and if-eq compare numbers or references,aget or aput reads (int[] or float[], long[] or double[]),new-instance before its <init>. The translator puts the JVM's new and
dup at the <init> call, so no uninitialized object goes through a branch,this is initialized.ART allows some things that the JVM verifier does not. The translator rewrites them:
<init> that is not its direct
superclass's, and a new-instance can be followed by an ancestor's <init>. dex_program_open reads every
method first. Each class from the instance's class up to that ancestor then gets a synthetic constructor that
calls its superclass's. Its descriptor has two more parameters (Lsnx/dalvik/Init; and the ancestor's type), so
it is never one of the class's own constructors.ViewGroup that
calls a View method).invoke-super of a method of an interface that is not a direct superinterface becomes an invokespecial of the
superclass's method. JVM resolution from the superclass finds the default method that it inherits.fill-array-data becomes a call to snx.dalvik.Runtime.fillArrayData with the payload in string constants.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.