Skip to content

Instantly share code, notes, and snippets.

@eginez
Created May 8, 2018 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eginez/0d4f2264acccbadfdb8f4144e8a7815b to your computer and use it in GitHub Desktop.
Save eginez/0d4f2264acccbadfdb8f4144e8a7815b to your computer and use it in GitHub Desktop.
Compile kotlin code to a native executable

You need to first install GraalVM, and then set your PATH so that you get the GraalVM binaries

Then you need the kotlin compiler toolchain. Easiest with sdkman Create a Kotlin file and add a simple program

$> cat src/main.kt
fun main() {
    println("hello")
}

Emmit java byte code with kotlin. Do not forget to include the runtime but not the reflection library

kotlinc -include-runtime  src/main.kt -d main.jar

Now run graal's aot compiler on the jvm byte code

$> native-image -cp main.jar MainKt
Build on Server(pid: 30544, port: 26682)
   classlist:     318.36 ms
       (cap):   1,866.47 ms
       setup:   2,153.75 ms
  (typeflow):   2,790.60 ms
   (objects):     931.18 ms
  (features):      26.37 ms
    analysis:   3,813.06 ms
    universe:     337.25 ms
     (parse):     529.82 ms
    (inline):     537.83 ms
   (compile):   5,301.12 ms
     compile:   6,606.64 ms
       image:     509.38 ms
       write:     558.19 ms
     [total]:  14,337.96 ms

Finally run your native executable

$> ./mainKt
hello

You just compiled kotlin code to native.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment