Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use kotlin.random.Random inside GraalVM native image

I tried to build a simple App with Kotlin that uses kotlin.random.Random class on a GraalVM native image. But this fails at runtime. Stacktrace see below.

As a workaround you can use the java standard java.util.Random class.

Can someone tell me how to use the Kotlin type?

App.kt

import kotlin.random.Random

fun main(args: Array<String>) {
   println(Random.nextInt())
}

Dockerfile

############################################################################
# Graal Native Image Early-Access Build
#
# Make sure you configured Docker to use at least 6gb ram!
# build from project root dir with: docker build -t example-kotlin-random-graalvm-native:1.0.0-SNAPSHOT .
# run with: docker run -d example-kotlin-random-graalvm-native:1.0.0-SNAPSHOT
############################################################################
#####
# The builder image to build the native app
#####
FROM findepi/graalvm:native as builder
LABEL stage=builder

WORKDIR /builder
COPY ./build/libs/app-all.jar ./app.jar
RUN native-image \
    --no-fallback \
    --static \
    -jar app.jar

#####
# The actual image to run
#####
FROM alpine:3.9
RUN apk --no-cache add ca-certificates

# App
WORKDIR /app
COPY --from=builder /builder/app .
EXPOSE $PORT
ENTRYPOINT ["./app"]

Runtime Error

Exception in thread "main" java.lang.ExceptionInInitializerError

at com.oracle.svm.core.hub.ClassInitializationInfo.initialize(ClassInitializationInfo.java:290)

at java.lang.Class.ensureInitialized(DynamicHub.java:475)

at kotlin.random.Random.<clinit>(Random.kt:242)

at com.oracle.svm.core.hub.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:350)

at com.oracle.svm.core.hub.ClassInitializationInfo.initialize(ClassInitializationInfo.java:270)

at com.example.AppKt.main(App.kt:8)

Caused by: java.lang.InstantiationException: Type `kotlin.internal.jdk8.JDK8PlatformImplementations` can not be instantiated reflectively as it does not have a no-parameter constructor or the no-parameter constructor has not been added explicitly to the native image.

at java.lang.Class.newInstance(DynamicHub.java:793)

at kotlin.internal.PlatformImplementationsKt.<clinit>(PlatformImplementations.kt:41)

... 6 more

Minimal Working example Project here

like image 871
Oliver Avatar asked Apr 28 '20 18:04

Oliver


1 Answers

You have to modify the reflections rules
add the following to the native-image arguments:

-H:ReflectionConfigurationFiles=/path/to/reflectconfig

and put the rules for the affected class into the reflectionconfig file:

[{
    "name" : "kotlin.internal.jdk8.JDK8PlatformImplementations",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true
}]

alternatively you can also specify the init method with the same file, read this

like image 143
Francesco Avatar answered Nov 10 '22 20:11

Francesco