1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# Building the Kotlin compiler native image for OFA
The stub jar runs a GraalVM native image of the Kotlin compiler
(`kotlin.proxy.nativeImage`). The JetBrains release of that image only bundles
some compiler plugins, and loads no others. OFA uses parcelize and
kotlin-empty-to-string, so it needs its own image. This document records how the
`kotlin-2.4.21-native-image-x86_64-ofa-1` image was built.
Files in this directory:
| file | what it is |
|---|---|
| `kotlin-2.4.21-native-image.patch` | changes to `prepare/compiler-native-image` in the Kotlin repository at tag `v2.4.21` |
| `build.sh` | runs the native image build with the options below |
Tools used from `../tools`:
| tool | what it is |
|---|---|
| `collect_metadata.py` | replays a compile from a Kotlin daemon log with the GraalVM tracing agent, to collect reachability metadata |
| `ic_check.sh` | checks that incremental compilation gives correct output |
## Requirements
- Oracle GraalVM for JDK 25, in `/usr/lib/jvm/java-25-graalvm` (or set
`GRAALVM_HOME`). GraalVM Community Edition can build the image, but it has
no G1 GC and no PGO, which give most of the speed.
- The Kotlin repository: `git clone https://github.com/JetBrains/kotlin`,
then `git checkout v2.4.21`.
- An OFA checkout with the `kotlinProxyStubJar` / `kotlinProxyNativeImage`
support in `KotlinConventionPlugin`, and a Gradle cache that holds
`kotlin-empty-to-string-compiler` (any OFA Kotlin build downloads it).
The first build of the Kotlin repository takes a long time.
## Why the image needs changes
In Kotlin 2.4.21, `PluginCliParser` reads `META-INF/services` from each
`-Xplugin` jar, then creates the registrar and the command line processor with
`Class.forName(name).newInstance()`. In the native image, `Class.forName`
finds only classes that are compiled into the image. So each plugin needs two
things:
1. Its classes on the native image classpath.
2. Reflection metadata for the no-arg constructors of its registrar and
command line processor.
The file `META-INF/org/jetbrains/kotlin/bundled-compiler-plugins.txt` in the
image is not read at runtime in 2.4.21. Plugin jars are matched by their
service files, not by name.
## The patch
`kotlin-2.4.21-native-image.patch` changes
`prepare/compiler-native-image/build.gradle.kts`:
- Bundles parcelize (`:kotlin-parcelize-compiler`).
- `kotlin.build.native-image.extra-plugins` bundles plugin jars from outside
the Kotlin repository, for kotlin-empty-to-string. Format:
`pluginId;registrarFqName;commandLineProcessorFqName;jarPrefixes;jarPath`,
entries separated by `|`. The jar must be compiled against
kotlin-compiler-embeddable.
- `generateBundledPluginsMetadata` writes the constructor metadata for all
bundled plugins onto the image classpath.
- `kotlin.build.native-image.use-g1`, `pgo-instrument`, `pgo-profile` and
`extra-args` add `--gc=G1`, `--pgo-instrument`, `--pgo=<file>` and other
native-image options. The names match the Kotlin master branch.
- `kotlin.build.native-image.graalvm-home` selects GraalVM directly. Oracle
GraalVM reports `Oracle Corporation` as vendor, so the `GRAAL_VM` toolchain
request in 2.4.21 does not find it.
The patch also changes the reachability metadata in
`resources/META-INF/native-image/org/jetbrains/kotlin/kotlin-compiler-embeddable/reachability-metadata.json`.
The JetBrains metadata was collected from Kotlin-only tests. OFA compiles Java
and Kotlin sources together, which uses IntelliJ PSI code that those tests do
not reach. The added metadata came from these steps:
1. `collect_metadata.py` runs on modules that use different features:
| module | why |
|---|---|
| `coreutils` | Java and Kotlin sources, parcelize |
| `live_score` | sources generated by KSP, parcelize, serialization |
| `ndc` | serialization, protobuf sources |
| `rewards` | compose, parcelize, serialization |
| `app` | the largest module |
2. Six IntelliJ resource bundles are added by hand. The tracing agent did not
record them, and `:app` failed with `MissingResourceException: Can't find
bundle for base name messages.JavaPsiBundle`. The bundles are
`messages.CoreBundle`, `messages.CoreDeprecatedMessagesBundle`,
`messages.JavaErrorBundle`, `messages.JavaOptionBundle`,
`messages.JavaPsiBundle` and `messages.UtilBundle`. They are all the
`messages/*.properties` files in `kotlin-compiler-embeddable.jar`.
## Procedure
### 1. Apply the patch
```bash
cd ~/source/kotlin
git checkout v2.4.21
git apply ~/source/kotlin_proxy/native-image/kotlin-2.4.21-native-image.patch
```
The build with `-PdeployVersion` also changes
`libraries/stdlib/src/kotlin/util/KotlinVersion.kt`. That change is
generated, it is not part of the patch.
### 2. Build a plain image
```bash
~/source/kotlin_proxy/native-image/build.sh plain
```
The image is in `prepare/compiler-native-image/build/dist`. `build.sh` uses
`-march=x86-64-v3`, so the image runs on the IQ builders. Do not use
`-march=native` for an image that leaves the build machine.
### 3. Build OFA with the image, and collect missing metadata
Build the stub jars (`make` in this repository), then set in OFA
`mobile/mobile/gradle-local.properties`:
```properties
kotlinProxyStubJar=/home/<user>/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar
kotlinProxyNativeImage=/home/<user>/source/kotlin/prepare/compiler-native-image/build/dist
```
Stop old Kotlin daemons, then build all Kotlin modules:
```bash
pkill -f 'KotlinCompile[D]aemon'
./gradlew compileDebugKotlin --continue
```
A module that fails with one of these errors needs more reachability metadata:
- `PicoInitializationException: Either do the specified parameters not match
any of the following constructors:[]`
- `ClassNotFoundException` or `NoSuchMethodException` from compiler code.
The failed compile is in `/tmp/kotlin-daemon.*.log`, with its arguments. Run:
```bash
~/source/kotlin_proxy/tools/collect_metadata.py <module> [<module> ...]
```
`<module>` is the `-module-name` of the compile, for example `coreutils`.
The script merges the metadata into the Kotlin checkout. Then repeat steps 2
and 3 until all modules build.
A `MissingResourceException` for a resource bundle needs a hand-made entry in
the `resources` list of `reachability-metadata.json`:
`{"bundle": "messages.<Name>"}`.
### 4. Check incremental compilation
```bash
~/source/kotlin_proxy/tools/ic_check.sh jvm # must pass, else the test is wrong
~/source/kotlin_proxy/tools/ic_check.sh proxy
```
The native image compiler runs in a subprocess. The Kotlin daemon gets no
output or lookup data from it, so incremental builds leave stale class files.
`KotlinConventionPlugin` disables incremental compilation when the proxy is
used. All four checks must pass in proxy mode.
### 5. Collect PGO profiles
```bash
~/source/kotlin_proxy/native-image/build.sh instrument
```
Add a profile directory to `gradle-local.properties`. Each compile writes one
`.iprof` file there.
```properties
kotlinProxyPgoProfileDir=/home/<user>/kotlin-pgo
```
Stop old Kotlin daemons, then compile all modules:
```bash
pkill -f 'KotlinCompile[D]aemon'
./gradlew compileDebugKotlin --rerun-tasks
```
Merge the profiles:
```bash
/usr/lib/jvm/java-25-graalvm/lib/svm/bin/native-image-utils merge-pgo-profiles \
--input-dir=/home/<user>/kotlin-pgo --output-file=/home/<user>/kotlin.iprof
```
Then remove `kotlinProxyPgoProfileDir` from `gradle-local.properties`.
### 6. Build the final image
```bash
~/source/kotlin_proxy/native-image/build.sh pgo /home/<user>/kotlin.iprof
```
### 7. Package and upload
`bin/` must be at the root of the archive. OFA passes the extracted directory
as `kotlinProxyNativeImage`.
```bash
cd ~/source/kotlin/prepare/compiler-native-image/build/dist
tar -cJf ~/kotlin-2.4.21-native-image-x86_64-ofa-1.tar.xz .
sha256sum ~/kotlin-2.4.21-native-image-x86_64-ofa-1.tar.xz
```
Upload the archive and `dist/kotlin-proxy-compiler-stubs-nolog.jar` (as
`kotlin-proxy-compiler-stubs-<version>.jar`) to
`https://build.android.service.osa/storage/`. Update the file names and the
SHA-256 checksums in OFA `mobile/mobile/build/iq/kotlin_proxy.py`. Use a new
`ofa-N` suffix for a new image of the same Kotlin version.
## Results
`./gradlew compileDebugKotlin --rerun-tasks`, OFA, 2026-11-23:
| compiler | build time | sum of Kotlin compile tasks | `:app` compile |
|---|---|---|---|
| JVM daemon | 2m00s | 151.4s | 50.8s |
| native image, G1, no PGO | about 2m00s | | |
| native image, G1, PGO | 1m48s | 131.6s | 39.2s |
Each compile starts a new process with cold caches. Modules that compile in
less than 3s are not faster than with the JVM daemon. The large modules give
the gain. Without incremental compilation, the image is not useful for
developer builds, only for clean builds on IQ.
## When Kotlin is updated
The patch, the metadata and the checksums are for Kotlin 2.4.21. For a new
Kotlin version:
1. Check out the new tag, and apply the patch. Fix conflicts in
`build.gradle.kts`.
2. Set `KOTLIN_VERSION` in `build.sh`, and the kotlin-empty-to-string version
for the new Kotlin version.
3. Do steps 2 to 7. Keep the hand-made bundle entries in the metadata.
4. Regenerate the stub jars for the new Kotlin version (see `../README.md`).
|