Run Proguard on a Spring project

I tried downloading ProGuard and running the examples folder. I do not want to run the android sample project. I want to use ProGuard just to remove dead code from my Spring Application. But I do not understand what needs to be mentioned in the build.gradle file instead of the android block and what other files should be a part of my Spring application and is the ANDROID_SDK needed even if it is a spring application. Are there any resources which can help get started with ProGuard to remove dead code from Spring application ? If not, can some HelloWorld application or some readme with all required details be provided to run ProGuard on a spring project preferably using maven ?

1 Like

For Java projects that are not Android related, you can integrate ProGuard e.g. as a gradle task. This is explained in more detail here: ProGuard Manual: Android Gradle | Guardsquare.

If you only want ProGuard to shrink and optimize your Spring application (thus not obfuscate it), you might be interested in setting -dontobfuscate in your ProGuard configuration file.

1 Like

In addition to @jonas.gijbelsā€™s answer, this Spring Boot example may be useful for you to check out: proguard/examples/spring-boot at master Ā· Guardsquare/proguard Ā· GitHub

I created a spring project, deleted the lib directory from the jar file,
added -ignorewarnings and run Proguard.
In the new jar added files from the lib folder.
After launch java - jar proj.jar I received
Exception in thread ā€œmainā€ Failed to get nested archieve for entry BOOT-INF/lib/activation.jar.
Whats wrong ?

Hi @konusua ,

Can you post a full stack trace please?

Kind regards,

Jonas

Hi Jonas,
Thank you a lot for answer.
I created a SpringBoot project.
and my next actions were:

Extract the unobfuscated fat jar from Spring Boot

jar xvf input.jar

Execute ProGuard, as input for ProGuard use

-injars BOOT-INF/classes/

java -jar proguard.jar @ proguard.conf

ProGuard, version 6.2.2
Reading program directory [F:\projfiles\Proguard\BOOT-INF\classes] (filtered)
Reading library jar [C:\Java\jdk11\lib\jrt-fs.jar] (filtered)
Note: the configuration refers to the unknown class ā€˜proguard.ProGuardā€™
Note: the configuration refers to the unknown class ā€˜java.lang.Stringā€™
Warning: com.konus.office.MonitorApplication: canā€™t find superclass or interface java.lang.Object
Warning: com.konus.office.controller.EventsController: canā€™t find superclass or interface java.lang.Object
Warning: com.konus.office.controller.LoginController: canā€™t find superclass,

Warning: com.konus.suprema.model.Userlogin: canā€™t find referenced class java.lang.String
Note: there were 2 references to unknown classes.
You should check your configuration for typos.
ā€¦
Warning: there were 906 unresolved references to classes or interfaces.
You may need to add missing library jars or update their versions.
If your code works fine without the missing classes, you can suppress
the warnings with ā€˜-dontwarnā€™ options.

Warning: there were 1 unresolved references to program class members.
Your input classes appear to be inconsistent.
You may need to recompile the code.

Error: Please correct the above warnings first.

I am reading the documentation.
I would really appreciate some tips on how to get started.

regards
Andrey

Jonas,
Could you please help me.
I found such a plugin for eclipse and proguard.

When I launch the project, everything works.
When I try to make a jar file, I get an error.
[INFO] Downloading from : https://dl.bintray.com/guardsquare/proguard/com/guardsquare/proguard-base/7.0.0/proguard-base-7.0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.138 s
[INFO] Finished at: 2021-05-18T20:27:53+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal ā€¦
Execution default of ā€¦
Plugin com. ā€¦ or one of its dependencies could not be resolved:
Failed to collect dependencies at ā€¦ guardsquare:proguard-base:jar:7.0.0:
Failed to read artifact descriptor for ā€¦
Could not transfer artifact ā€¦
Access denied to link Error code 403, Forbidden ā†’ [Help 1] [ERROR]

Is there a working version of this plugin?

regards
Andrey

Hi @konusua,

Regarding the Eclipse plugin;
For a Spring Boot application, this maven plugin below was recommended before in the community: GitHub - wvengen/proguard-maven-plugin: ProGuard Maven plugin that supports modularised ProGuard packages

Regarding your previous issue;
Your compiled class files are most likely inconsistent. Possibly, some class file didnā€™t get recompiled properly, or some class file was left behind after its source file was removed. Try removing all compiled class files and rebuilding your project.

Kind regards,
Jonas

Hi Jonas,
Proguard Maven Plugin works !
Thank you for advice

Andrey

Hi @konusua,

In addition to my previous reply I would like to mention the following regarding the issue: Error: Please correct the above warnings first.

Since the java.lang.String class is missing from the input (amongst others), you will need to add the Java 11 base mod to the input using -libraryjars ;
-libraryjars C:\Java\jdk11\jmods\java.base.jmod(!module-info.class)

Kind regards,

Jonas

Hi Jonas !,
Could you help me, please

I wrote a simple springboot application, obfuscated it and application started.
After added one rest controller. After obfuscation at the startup stage, I got an error

ConflictingBeanDefinitionException: Annotation-specified bean name ā€˜aā€™ for bean class [com.proguard.example.c.a] conflicts with existing, non-compatible bean definition of same name and class [com.proguard.example.a.a]

I forgot to set some option ?

regards

Hi @konusua ,

Can you please share the full stack trace and ProGuard version with us?

Best regards,

Jonas

Hi Jonas,
I am using version 7.1.3 beta.
The options:
-dontshrink
-dontoptimize
Error
2022-02-14 13:05:44,735 [WARN ] [main ] [o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.proguard.spring.boot.Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name ā€˜aā€™ for bean class [com.proguard.spring.boot.c.a] conflicts with existing, non-compatible bean definition of same name and class [com.proguard.spring.boot.a.a]

 I add next option   <option>-flattenpackagehierarchy</option> 
     <option>-dontshrink</option>  
     <option>-dontoptimize</option> 
     <option>-flattenpackagehierarchy</option> 

[o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ā€˜applicationā€™: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ā€˜c.aā€™ available: expected at least 1 bean which qualifies as autowire candidate.

I try work with 7.2 version, but got error
com.guardsquare:proguard-base:pom:7.2 from/to bintray-guardsquare-proguard (https://dl.bintray.com/guardsquare/proguard): Failed to transfer https://dl.bintray.com/guardsquare/proguard/com/guardsquare/proguard-base/7.2/proguard-base-7.2.pom. Error code 502, Bad Gateway ā†’ [Help 1]

thank you a lot,
regards

Full stack:
2022-02-14 14:56:38,491 [INFO ] [main ] [c.p.s.b.Application ] - Starting Application v2.2.0 using Java 11.0.11 on WORKSTATION with PID 4656 (Z:\tmp\spring.boot.jar started by Andrey in Z:\tmp)
2022-02-14 14:56:38,493 [INFO ] [main ] [c.p.s.b.Application ] - No active profile set, falling back to default profiles: default
2022-02-14 14:56:39,247 [INFO ] [main ] [o.s.b.w.e.t.TomcatWebServer ] - Tomcat initialized with port(s): 6060 (http)
2022-02-14 14:56:39,258 [INFO ] [main ] [o.a.c.c.StandardService ] - Starting service [Tomcat]
2022-02-14 14:56:39,259 [INFO ] [main ] [o.a.c.c.StandardEngine ] - Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-14 14:56:39,304 [INFO ] [main ] [o.a.c.c.C.[.[.[/] ] - Initializing Spring embedded WebApplicationContext
2022-02-14 14:56:39,306 [INFO ] [main ] [o.s.b.w.s.c.ServletWebServerApplicationContext] - Root WebApplicationContext: initialization completed in 765 ms
2022-02-14 14:56:39,346 [WARN ] [main ] [o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ā€˜applicationā€™: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ā€˜c.aā€™ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2022-02-14 14:56:39,349 [INFO ] [main ] [o.a.c.c.StandardService ] - Stopping service [Tomcat]
2022-02-14 14:56:39,359 [INFO ] [main ] [o.s.b.a.l.ConditionEvaluationReportLoggingListener] -

Error starting ApplicationContext. To display the conditions report re-run your application with ā€˜debugā€™ enabled.
2022-02-14 14:56:39,377 [ERROR] [main ] [o.s.b.d.LoggingFailureAnalysisReporter] -


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.proguard.spring.boot.Application required a bean of type ā€˜c.aā€™ that could not be found.

Action:

Consider defining a bean of type ā€˜c.aā€™ in your configuration.

Hi @konusua ,

We no longer distribute ProGuard via Jcenter, please use maven central instead e.g. Maven Central Repository Search

I would recommend you to upgrade to a later (stable) version of ProGuard. The issue is, however, linked to name obfuscation. In the error you can see itā€™s looking for a bean named application , which cannot be found. Adding a -keep option for this class should resolve the issue.

Best regards,

Jonas