README.md
1## ART Service
2
3Warning: The contents in this doc can become stale while the code evolves.
4
5ART Service manages dexopt artifacts of apps. With ART Service, you can dexopt
6apps, query their dexopt status (the compiler filter, the compilation reason,
7whether the dexopt artifacts are up-to-date, etc.), and delete dexopt artifacts.
8
9Note: ART Service is introduced since Android U. Prior to ART Service, dexopt
10artifacts were managed by Package Manager with a legacy implementation. ART
11Service is the default on Android U, while partners are allowed to opt-out and
12use the legacy implementation instead. The legacy implementation will be removed
13since Android V. This doc only describes ART Service, not the legacy
14implementation.
15
16### Primary dex vs. secondary dex
17
18ART Service dexopts both primary dex files and secondary dex files of an app.
19
20A primary dex file refers to the base APK or a split APK of an app. It's
21installed by Package Manager or shipped as a part of the system image, and it's
22loaded by Framework on app startup.
23
24A secondary dex file refers to an APK or JAR file that an app adds to its own
25data directory and loads dynamically.
26
27Note: Strictly speaking, an APK/JAR file is not a DEX file. It is a ZIP file
28that contain one or more DEX files. However, it is called a *dex file*
29conventionally.
30
31### Compiler filters
32
33See
34[Compilation options](https://source.android.com/docs/core/runtime/configure#compilation_options).
35
36### Dexopt scenarios
37
38At a high level, ART Service dexopts apps in the following senarios:
39
40- the device is on the very first boot
41- the device is on the first boot after an OTA update
42- the device is on the first boot after a mainline update
43- an app is being installed
44- the device is idle and charging
45
46Tip: The compilation reason, stored in the header of the OAT file, shows the
47senario, in which the app is dexopted.
48
49The sections below describe the default behavior in each senario. Note that the
50list of apps to dexopt and the compiler filter, as well as other options, can be
51customized by partners through a callback.
52
53#### On the very first boot / the first boot after an OTA update
54
55On the very first boot / the first boot after an OTA update, ART Service only
56dexopts primary dex files of all apps with the "verify" compiler filter.
57
58Note: It doesn't dexopt secondary dex files or use the "speed-profile" filter
59because doing so may block the boot for too long.
60
61Note: In practice, ART Service does nothing for most of the apps because the
62apps on the system partitions have dexopt artifacts generated by dexpreopt, and
63the apps on the data partition still have VDEX files usable even though their
64dependencies (bootclasspath and class loader context) have probably changed. In
65this senario, ART Service mostly dexopt apps in APEXes because they are not
66supported by dexpreopt.
67
68#### On the first boot after a mainline update
69
70On the first boot after a mainline update, ART Service dexopts the primary dex
71files of the system UI and the launcher. It uses the "speed" compiler filter for
72the system UI, and uses the "speed-profile" compiler filter for the launcher.
73
74Note: It only dexopts those two apps because they are important to user
75experience.
76
77Note: ART Service cannot use the "speed-profile" compiler filter for the system
78UI because the system UI is dexpreopted using the "speed" compiler filter and
79therefore it's never JITed and as a result there is no profile collected on the
80device to use. This may change in the future.
81
82#### During app installation
83
84During app installation, ART Service dexopts the primary dex files of the app.
85If the app is installed along with a DM file that contains a profile (known as a
86*cloud profile*), it uses the "speed-profile" compiler filter. Otherwise, it
87uses the "verify" compiler filter.
88
89Note: If the APK is uncompressed and aligned, and it is installed along with a
90DM file that only contains a VDEX file (but not a profile), no dexopt will be
91performed because the compiler filter will be "verify" and the VDEX file is
92satisfactory.
93
94Note: There is no secondary dex file present during installation.
95
96#### When the device is idle and charging
97
98ART Service has a job called *background dexopt job* managed by Job Scheduler.
99It is triggered when the device is idle and charging. During the job execution,
100it dexopts primary dex files and secondary dex files of all apps with the
101"speed-profile" compiler filter.
102
103The job is cancellable. When the device is no longer idle or charging, Job
104Scheduler cancels the job.
105