Search code examples
javaandroidgluon-mobilegluonfx

gluon app does not find method in jdk, how is that possible?


I made an android gluon app, testing the validity of the model before going "big boy". I had some struggles, mainly due to how different a gluon app on desktop and mobile (android) react.

I was able to overcome all of them, except one.

When my app starts, i open some websockets connections and get this exception

[Thu Aug 11 08:39:13 CEST 2022][INFO] [SUB] D/GraalCompiled(22536): Caused by: java.lang.NoSuchMethodException: java.lang.Byte.valueOf(java.lang.String)
[Thu Aug 11 08:39:13 CEST 2022][INFO] [SUB] D/GraalCompiled(22536):     at java.lang.Class.getMethod(DynamicHub.java:1114)
[Thu Aug 11 08:39:13 CEST 2022][INFO] [SUB] D/GraalCompiled(22536):     at org.eclipse.jetty.util.TypeUtil.<clinit>(TypeUtil.java:147)
[Thu Aug 11 08:39:13 CEST 2022][INFO] [SUB] D/GraalCompiled(22536):     ... 35 more

I've had lots of reflection problems, but this one i don't know how to tackle. Here are the various parameters:

  • app compiled on linux (ubuntu)
  • netbeans 13
  • jdk 17.0.4
  • gluon plugin 2.8.4
  • graalvm-svm-java17-linux-gluon-22.0.0.3-Final
  • javaStaticSdkVersion='18-ea+prep18-8
  • galaxy tab A8, android 11

Don't know what else to say, i don't have a clue as to where to start, there are too many paths i can go and search.

Thanks for any input.


Solution

  • You're seeing this error because Jetty's TypeUtil class uses reflection for some String-to-basic type conversions.

    Access via reflection is difficult to resolve upon compilation time, which is probably why your GraalVM native-image build fails: native-image only includes classes that are deemed necessary, and in this case it just missed to include it.

    A proper fix would be to change TypeUtil to use method references instead of reflection (I ran into the same problem, and I've just filed Jetty pull request #9115).

    However, you will most likely want to run your code now, and can't wait for this to be merged. Additionally, you may probably encounter more of these reflection issues even if this particular one is fixed.

    In order for GraalVM native-image to know which classes and methods are accessed via reflection, you can specify them via configuration files in the resource classpath, under META-INF/native-image/com.yourdomain/your-project/reflect-config.json. Upon assembly, native-image will pick up this configuration from the classpath.

    You can also use the GraalVM native-image Tracing Agent to create this file automatically.

    To do this, invoke your app with the GraalVM JRE (i.e., using Hotspot, not native-image), and specify the following VM argument:

    -agentlib:native-image-agent=config-output-dir=/tmp/native-image.{pid}
    

    Interact with your app like you normally would (cover as much as possible), then terminate the VM.

    You will see a bunch of JSON files created under /tmp/native-image.(PID). Copy all of them under the META-INF path described above and rebuild your native image.

    For details see the GraalVM documentation, e.g., Reflection Use in Native Images and Native Image Tracing Agent.