Trying to convert existing Spring Boot micro-service to run on GraalVM native image came across an issue with protobuf generated classed with protobuf-java.
Generated classes use reflection and need to add ALL classes to reflect-config.json, also many classes has nested Builder classes so need to add of these as well, very dirty work for existing projects having hundreds of such protos.
Reading protobuf documentation it is mentioned either use protobuf-javalite or run GraalVM tracing agent to generate reflect-config.json automatically.
Moving to protobuf-javalite sounds risky, it was designed for Android and advertised as not stable library.
Running trace agent after every modification of proto add major overhead to development process.
So the question if anyone came across same issue and was able to solve that in more elegant manner and if there are any better plans of protobuf-java to support native image.
Using protobuf-java in GraalVM native image
224 Views Asked by Pavel At
2
There are 2 best solutions below
5
On
I found the most convenient way in this case is to create custom feature (described here) which scans classpath before analysis and register the classes for runtime reflection.
Created simple utility which do that for you. You just need to add it as dependency into your project
Above solution is great for applications not using Spring Boot. Following similar logic, I created similar solution for Spring Boot context.
Dependency in addition to spring boot 3.1.3+ depedencies:
Class:
Add to Spring @Configuration class:
Resource file (
META-INF/native-image/protobuf-packages.properties):Test: