A GraalVM-compiled native image of kotlin-compiler-embeddable that runs as a standalone executable. Behaves like the regular kotlinc but with some limitations — mainly, a limited list of supported compiler plugins (see Plugins). Produces a native image distribution which is similar in its structure to the standard kotlinc distribution.
The distribution is produced in prepare/compiler-native-image/build/dist directory and has the following structure:
bin/kotlinc-native-image[.exe] — native binary;bin/kotlinc-native-image.[sh|bat] — launcher wrappers;lib/ — runtime classpath jars;license/ — license information.Produce a native image binary:
./gradlew :kotlin-compiler-native-image:kotlincNativeImage
The task requires a GraalVM JDK version 25.1.3+ toolchain, which is automatically resolved via Gradle.
Produce a native image distribution:
./gradlew :kotlin-compiler-native-image:kotlincNativeImageDist
You can also produce native image distribution release artifacts in prepare/compiler-native-image/build/artifacts via:
./gradlew :kotlin-compiler-native-image:kotlincNativeImageArtifacts
Additional build properties that require Oracle GraalVM:
-Pkotlin.build.native-image.use-g1=true enables the G1 garbage collector for the compiler native image. It provides significant performance benefits.-Pkotlin.build.native-image.dynamic-plugins=true enables dynamic plugin loading support, see Plugins for more details.-Pkotlin.build.native-image.pgo-profile=file.iprof enable profile-guided optimizations using file.iprof as an input profile. See Profile-Guided Optimizations for more details.Similar to JVM kotlinc, the native image binary requires some configuration. To simplify the user experience, the distribution also provides kotlinc-native-image.[sh|bat] wrapper scripts that do all the necessary configuration of the binary. Namely, they configure:
-Djava.home — resolved to the JAVA_HOME environment variable;-Dkotlin.home — resolved to the location of the distribution root.These wrapper scripts may be used as a drop-in replacement for the JVM kotlinc:
dist/kotlinc-native-image/bin/kotlinc-native-image.sh path/to/Foo.kt -d out/
For a quick sanity check, use:
./gradlew :kotlin-compiler-native-image:nativeImageSmokeTest
It compiles a “Hello World” with the native image compiler and verifies that the compilation succeeds.
For the full suite, run:
./gradlew :kotlin-compiler-native-image:nativeImageBoxTest
The test suite mirrors the compiler's JVM box-codegen tests: it is generated from the same test data under compiler/testData/codegen/box and behaves similarly to the JVM tests. The goal is to ensure that the behavior of the native image compiler is the same as its JVM counterpart.
The current setup is quite ad-hoc:
compilerTest aggregate and run only via the dedicated nativeImageBoxTest task; the main goal is to not intervene with pre-existing workflows while still ensuring that the native image works correctly;JVM_IR is treated as the target backend, so a test is skipped if it contains // IGNORE_BACKEND: JVM_IR directive (or its equivalent);helpers.*) are skipped.GraalVM reachability metadata is stored in prepare/compiler-native-image/resources/META-INF/native-image.
Initial metadata was collected by running the Kotlin Compiler Embeddable JAR with the GraalVM Tracing Agent on the same set of compiler box tests compiler/testData/codegen/box.
Metadata can be regenerated when necessary. For a quick update, use:
./gradlew :kotlin-compiler-native-image:generateReachabilityMetadataSmoke
It will run the reachability metadata collection on the sample “Hello World” program.
For a full regeneration over all the box test data, use (takes ~3–4 hours):
./gradlew :kotlin-compiler-native-image:generateReachabilityMetadataBox
IDE run configurations are available both for running native image tests and collecting reachability metadata. You can find them in the “Compiler Native Image” run configuration folder.
Kotlin Compiler Native Image by default only supports a limited list of built-in compiler plugins:
Attempting to use other plugins will result in a runtime error on the compiler startup.
It is possible, however, to enable (experimental) support for dynamic compiler plugin loading thanks to GraalVM's Project Crema. To enable it, set kotlin.build.native-image.dynamic-plugins Gradle property to true in CLI when building the executable or in gradle.properties file:
./gradlew -Pkotlin.build.native-image.dynamic-plugins=true :kotlin-compiler-native-image:kotlincNativeImageDist
GraalVM Native Image supports Profile-Guided Optimizations. PGO provides native image a way to compete with JVM's JIT compiler: a way to optimize the performance and size of the native image based on the execution profile of the application. However, while JIT can do that at the runtime using the actual runtime behaviour, native image requires the profile to be collected ahead of time.
Currently, we do not provide a ready-to-use profile, however our experiments show that even basic up-to-date profile can provide some performance benefits. We are planning to collect and provide a comprehensive profile for the Kotlin compiler in the future, for now one can collect a custom profile if necessary.
To collect a custom profile you need to rebuild the compiler native image with --pgo-instrument option. You may use -Pkotlin.build.native-image.pgo-instrument=true option to do so:
./gradlew -Pkotlin.build.native-image.pgo-instrument=true :kotlin-compiler-native-image:kotlincNativeImageArtifacts
After that, you can use the newly built image to collect a profile on a given workload. You can also specify where to store the collected profile using -XX:ProfilesDumpFile=file.iprof option, be default it will produce a default.iprof file.
./prepare/compiler-native-image/build/dist/bin/kotlinc-native-image.sh -XX:ProfilesDumpFile=prepare/compiler-native-image/kotlin-compiler-embeddable.iprof HelloWorld.kt
You can also collect multiple profiles and merge them into a single file using native-image-utils, see GraalVM documentation for more details.
We also provide an option to include the Kotlin Compiler Daemon native image in the distribution, -Pkotlin.build.native-image.kotlin-daemon=true. Daemon native image is not yet production ready and is not integrated with the Build Tools API. The main purpose of it is to ensure that the daemon native image can be compiled and correctly responds to compilation requests.
You can run the native image tests using the native image daemon (instead of CLI native image):
./gradlew -Pkotlin.build.native-image.kotlin-daemon=true :kotlin-compiler-native-image:nativeImageDaemonBoxTest
And update reachability metadata for the daemon:
./gradlew -Pkotlin.build.native-image.kotlin-daemon=true :kotlin-compiler-native-image:generateReachabilityMetadataDaemonBox
Currently, we're using the same reachability-metadata.json for both daemon and CLI native image. Ideally, they would be separate and each one would include only the classes and methods that are used by the respective native image. However, using the single merged metadata JSON does not significantly affect the size of the native image (<1MB change), so it is not worth the effort for now. This is a place to look into when optimizing the native image size in the future.