spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.25k stars 37.98k forks source link

Malformed url "nested" after compile using proguard #32030

Closed dimaslanjaka closed 8 months ago

dimaslanjaka commented 8 months ago

Affects: \


Need help proguard configuration to success run the spring web server.

proguard.pro

-verbose
-ignorewarnings
#-dontoptimize
#-dontshrink
#-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
#-dontpreverify
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-renamesourcefileattribute SourceFile
#-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

##---------------End: proguard configuration for Gson  ----------

# Application classes that will be serialized/deserialized over Gson
-keep class im3.model.** { *; }
-keep class web.models.** { *; }

# Keep - Applications. Keep all application classes, along with their 'main'
# methods.
-keepclasseswithmembers public class org.eclipse.jdt.internal.jarinjarloader.*, org.apache.* {
    public static void main(java.lang.String[]);
}
-keepclasseswithmembers public class web.MainApplication {
    public static void main(java.lang.String[]);
}
-keepclasseswithmembers class org.springframework.** {
    public static void main(java.lang.String[]);
}
-keepparameternames

-keepclasseswithmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keep class org.apache.catalina.webresources.**
-keepclassmembers @javax.** class * { *; }
-keepclassmembers @jakarta.** class * { *; }

# Keep - Database drivers. Keep all implementations of java.sql.Driver.
-keep class * extends java.sql.Driver
# Keeps the directory structure intact.
# Spring's @ComponentScan needs the specified directory structure at runtime as well.
-keepdirectories web.**
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
-keep class kotlin.** { *; }
-keepdirectories org.springframework.boot.autoconfigure
## Do not change names of the getters and setter, if you remove this
##thymeleaf unable to find the getter and setter i.e:
##${greetingDTO.message}
-keepclassmembers class * {
    *** get*();
    void set*(***);
}
-printmapping build/libs/out.txt
-printconfiguration build/libs/conf.txt

proguard gradle task

tasks.register('proguard', ProGuardTask) {
    // run bootJar first
    dependsOn("bootJar")

    // proguard config
    configuration file('proguard.pro')

    // input jar from task bootJar output
    injars(tasks.named('bootJar', Jar).flatMap { it.archiveFile })
    // java home
    String javaHome = System.getProperty('java.home');

    // Automatically handle the Java version of this build.
    // JDK 8 and below use jars on the classpath
    if (JavaVersion.current().java8Compatible &&    !JavaVersion.current().java9Compatible) {
        println "Obfuscation inputs based on JDK 8 layout."
        libraryjars "$javaHome/lib/rt.jar"
        libraryjars "$javaHome/lib/jce.jar"
        libraryjars "$javaHome/lib/ext/jfxrt.jar"
    } else {
        // JDK 9 and above use modules on the module-path
        println "Obfuscation inputs based on JDK 9+ module layout."
        def jdkModuleList = [
                'java.base', 'java.datatransfer', 'java.desktop',
                'java.instrument', 'java.logging',
                'java.management', 'java.prefs', 'java.rmi',
                'java.scripting', 'java.xml',
                'jdk.attach'
        ]
        jdkModuleList.forEach {
            libraryjars "$javaHome/jmods/${it}.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
        }
//        target '10' // JDK 9 is obsolete, would target 11, but Proguard can't deal with 11's class files yet
    }
    // dependencies
    configurations.runtimeClasspath.files.each {
        libraryjars it, filter: '!META-INF/versions/**'
    }

    printseeds "$buildDir/libs/proguard_seeds.txt"
    printmapping "$buildDir/libs/proguard_map.txt"

    // add all dependencies
//    libraryjars "${dependsDir}"

    verbose

    // output jar
    outjars(layout.buildDirectory.file("libs/proguard-server.jar"))
}

dependencies

dependencies {
    implementation project(":jvm")
    implementation platform('org.springframework.boot:spring-boot-dependencies:2.7.8')
    implementation("org.springframework.boot:spring-boot-autoconfigure")
    implementation("org.springframework.boot:spring-boot-starter-web")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    implementation 'io.projectreactor:reactor-core:3.6.1'

    // spring session
    implementation("org.springframework.session:spring-session-core")
    implementation "org.xerial:sqlite-jdbc:3.+"
    implementation "org.hibernate.orm:hibernate-community-dialects:+"
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation("org.springframework.session:spring-session-jdbc")
//    implementation "org.springframework.boot:spring-boot-starter-security"
    // spring logger
    implementation 'org.apache.logging.log4j:log4j-bom:3.0.0-beta1'
    implementation 'org.apache.logging.log4j:log4j-api'
    implementation 'org.apache.logging.log4j:log4j-core'
    testImplementation 'org.apache.logging.log4j:log4j-slf4j2-impl'
    implementation 'org.slf4j:slf4j-jdk14:1.7.25'
    testImplementation 'ch.qos.logback:logback-classic:1.4.14'
    implementation 'org.slf4j:slf4j-nop:2.0.7'

    // JSON
    implementation group: 'org.json', name: 'json', version: '20231013'
    implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    implementation "com.google.code.gson:gson:2.10.1"

    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version"))
    // Use the Kotlin JDK 8 standard library.
    // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib")

    // spring theme engine
//    implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

    // IDE
    implementation "org.jetbrains:annotations:24.1.0"

    // Okhttp
    implementation(platform("com.squareup.okhttp3:okhttp-bom:4.12.0"))
    implementation("com.squareup.okhttp3:okhttp")
    implementation("com.squareup.okhttp3:okhttp-urlconnection")
    implementation("com.squareup.okhttp3:logging-interceptor")

    // apache commons
    implementation 'org.apache.commons:commons-lang3:3.14.0'
    implementation 'org.apache.commons:commons-io:1.3.2'
    implementation "commons-codec:commons-codec:1.16.0"
    implementation 'org.apache.httpcomponents.client5:httpclient5:5.4-alpha1'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
bclozel commented 8 months ago

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.