Obfuscating jar that uses log4j

All around beginner, bare with me. I’m attempting to obfuscate a jar that uses log4j to write to the console as well as to a .log file. I’ve specified the locations of where to write the logs through in a log4j.properties file in my resources file prior to “jarring” up the project. Prior to obfuscation, the executable runs perfectly. After obfuscation, running the executable results in this error:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console…

I am using the proguardgui opensource executable and in the obfuscation section I’ve attempted to “Keep additional class names and class member names” for the following:
Class org.apache.logging.log4j.java
Class org.apache.logging.log4j.*
Class com.foo.bar.Main.java
Class com.foo.bar.CommandLineInterface.java
Class org.apache.logging.log4j.core.java

Not sure if I have to include the .java extension or not (I’ve tried both ways).

I also tried selecting the option to “Adapt resource file names” for **.properties

Any idea on how I can remediate this issue or the whereabouts of any further documentation?

1 Like

Hi @macpugliese ,

Based on your error, ProGuard likely removed some of these classes as part of the shrinking step, or obfuscated their names while they’re accessed via reflection. Typically you will see exceptions in your logs such as ClassNotFoundException, NoSuchMethodException, NoSuchFieldException. You will need to add a -keep rule to ProGuard’s configuration to instruct ProGuard not to remove/obfuscate these parts. Please note this will be an iterative approach, it’s very likely a similar exception for a different class, method, or field is thrown.

Resolving things like this is documented in our troubleshooting manual. I’d also recommend you to have a look at the -addconfigurationdebugging option as this option might help you set up these -keep rules via suggestions in your logs rather than configuring ProGuard iteratively.

Best regards,

Jonas