Hi, I have a two custom libraries lib1 and lib2, which are added into my project as a dependency.
My requirement is to obfuscate my project along with lib1 and lib2.
What I tried is I obfuscated lib1 and lib2 and saved it’s -printMapping into a file.
While obfuscating the project I am applying this mapping, but when I run the jar I keep getting errors for obfuscated classes “Error creating Bean : Lookup method resolution failed.”
When I unzipped my project’s obfuscated jar, BOOT-INF/lib had unobfuscated jars of lib1 and lib2.
How can I obfuscate my libraries (added as dependencies) while obfuscating main project.
My project is built using spring boot (3.2.2) and proguard (7.4.2) for obfuscation.
Please check below gradle proguardTask and rules.pro file.
proguardTask
task proguard(type: ProGuardTask) {
dependsOn tasks.extractJar
verbose
injars "${buildDir}/extracted/BOOT-INF/classes"
outjars "${buildDir}/obfuscatedClasses.jar"
configuration proguardConfig
// Automatically handle the Java version of this build.
if (System.getProperty('java.version').startsWith('1.')) {
// Before Java 9, the runtime classes were packaged in a single jar file.
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
} else {
// As of Java 9, the runtime classes are packaged in modular jmod files.
libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
//libraryjars "${System.getProperty('java.home')}/jmods/....."
}
// This will contain the Spring dependencies.
libraryjars sourceSets.main.compileClasspath
// Keep the main class entry point.
keep 'public class com.example.Application { \
public static void main(java.lang.String[]); \
}'
keep 'public class org.springframework.boot.loader.JarLauncher'
keepattributes '*Annotation*'
// This simple example requires classes with @Component annotation classes
// to be kept, since otherwise components could end up with clashing names,
// if they do not set the name explicitly.
keep 'public @org.springframework.stereotype.Component class *'
keep 'public @org.springframework.context.annotation.Configuration class *'
keep 'public @org.springframework.boot.context.properties.ConfigurationProperties class *'
keep 'public @org.springframework.boot.autoconfigure.SpringBootApplication class *'
keep 'public @org.springframework.stereotype.Service class *'
keep 'public @org.springframework.ws.server.endpoint.annotation.Endpoint class *'
// You may need to keep classes or members based on other annotations such as:
keepclassmembers 'public class * { \
@org.springframework.beans.factory.annotation.Autowired *; \
@org.springframework.beans.factory.annotation.Value *; \
@org.springframework.beans.factory.annotation.Qualifier *; \
@org.springframework.beans.factory.annotation.Required *; \
@org.springframework.context.annotation.Bean *; \
@org.springframework.context.annotation.Primary *; \
@org.springframework.boot.context.properties.ConfigurationProperties *; \
@org.springframework.boot.context.properties.EnableConfigurationProperties *; \
@javax.inject.Inject *; \
@javax.annotation.PostConstruct *; \
@javax.annotation.PreDestroy *; \
}'
// After ProGuard has executed, repackage the app.
finalizedBy tasks.repackage
}
rules.pro
-keep class javax.xml.** { ; }
-keepclassmembernames class * {
java.lang.Class class$(java.lang.String);
java.lang.Class class$(java.lang.String, boolean);
}
-dontoptimize
-dontshrink
-adaptclassstrings
-keepattributes Exceptions, InnerClasses, Signature, Deprecated, SourceFile, LineNumberTable, Annotation, EnclosingMethod, RuntimeVisibleTypeAnnotations, RuntimeInvisibleTypeAnnotations
-keepnames interface **
-keepparameternames
-keep interface * extends * { ; }
-keep class com.fasterxml.jackson. { ; }
-dontwarn javax.xml.*
-keepdirectories com.example.**
-applymapping proguard.map
-keepattributes SourceFile, LineNumberTable
-keepattributes LocalVariableTable, LocalVariableTypeTable
-addconfigurationdebugging
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers