SpringBoot maven with java 11 obfuscation with Proguard

I have used proguard maven plugin of com.github.wvengen, defined rules in proguard conf file. while running the Obfuscated executable jar getting error
“ Error creating bean with name ‘ModelEntityManagerFactory’ defined in class path resource ” .
In the Proguard rules I have excluded all the database classes( entities, jpa repo, db config classes) and all external dependency classes ( like spring boot, hibernate, jpa classes) Still i am facing error .

Error details:
lt pool will be used
2023-12-06 09:49:08.647 INFO 22212 — [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
2023-12-06 09:49:10.602 ERROR 22212 — [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ModelEntityManagerFactory’ defined in class path resource [com/example/database/ModelDatasourceConfiguration.class]: Invocation of init method failed; nested exception is java.util.NoSuchElementException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(Unknown Source) ~[example-1.14.0-SNAPSHOT-obfuscated.jar:1.14.0-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Unknown Source) ~[example-1.14.0-SNAPSHOT-obfuscated.jar:1.14.0-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(Unknown Source) ~[example-1.14.0-SNAPSHOT-obfuscated.jar:1.14.0-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(Unknown Source) ~[example-1.14.0-SNAPSHOT-obfuscated.jar:1.14.0-SNAPSHOT]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Unknown Source) ~[example-1.14.0-SNAPSHOT-obfuscated.jar:1.14.0-SNAPSHOT]

Hello,

Welcome to the Guardsquare community! It seems like the errors you are getting are due to either the obfuscation, shrinking, or optimization features of ProGuard. To narrow down what is causing this crash you should add the following to your configuration:

-dontshrink
-dontoptimize
-dontobfuscate

If adding these options solves the issue, then you know some classes are being removed or renamed and a keep rule is needed. To fix this you will need to adjust your ProGuard rules to keep the necessary classes and methods related to the ModelEntityManagerFactory and its dependencies. ProGuard is either removing or obfuscating these names so they are no longer recognized at runtime and adding keep rules will prevent this.

I would recommend adding broad keep rules like the ones below and then fine-tuning them so they only keep the error-causing classes. While I’m not familiar with the exact structure of your project I’ve written a few keep rules below to give you an idea of the keep rules you need to prevent the crash. You may not necessarily need all of them, but I think a couple will cover the crash cause.

-keep class com.example.database.** {
    <methods>;
    <init>;  
}

-keep class org.springframework.** {
    *;
}

-keep @org.springframework class ** 

-keep class org.hibernate.** { *; }

-keep class io.undertow.** { *; }

You can always use ProGuard Playground to test and see if the keep rules in your configuration correctly keep classes in your project as intended.

Best Regards,

Aissa

1 Like