Slackbot
05/21/2024, 3:24 PMAhmed Riyadh
05/21/2024, 3:25 PMtasks.register<proguard.gradle.ProGuardTask>("proguard") {
configuration(file("<http://proguard.pro|proguard.pro>"))
injars(tasks.shadowJar.flatMap { it.archiveFile })
outjars(
layout.buildDirectory.file("libs/${tasks.shadowJar.get().archiveFile.get().asFile.nameWithoutExtension}-minified.jar"),
)
// 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 include the Kotlin library jars
libraryjars(sourceSets.main.get().compileClasspath)
verbose()
}
Ahmed Riyadh
05/21/2024, 5:12 PMAhmed Riyadh
05/21/2024, 5:15 PMAhmed Riyadh
05/21/2024, 5:16 PMsuspend
keyword, so it's not related or specific to my applicationAhmed Riyadh
05/21/2024, 5:17 PMAhmed Riyadh
05/21/2024, 5:19 PMRandom
from kotlin
you will get an error. using java imports doesn't have such issues because it's already in the JREAhmed Riyadh
05/21/2024, 5:21 PMapplication-kotlin
from proguard example is a little bit outdated.Ahmed Riyadh
05/21/2024, 5:24 PMAhmed Riyadh
05/21/2024, 5:27 PMendeavors
mean, Good luck to you too.Ahmed Riyadh
05/24/2024, 4:32 AMval javaHome = System.getProperty("java.home")
if (System.getProperty("java.version").startsWith("1.")) {
// Before Java 9, runtime classes are packaged in a single JAR file.
libraryjars("$javaHome/lib/rt.jar")
} else {
// Starting from Java 9, runtime classes are packaged in modular JMOD files.
libraryjars(
mapOf("jarfilter" to "!**.jar", "filter" to "!module-info.class"),
"$javaHome/jmods/java.base.jmod",
)
}
printmapping(destinationDirectory.file("$originalJarNameWithoutExtension.map"))
// TODO: Workaround to fix `Please correct the above warnings first`
ignorewarnings()
dontobfuscate()
dontoptimize()
dontwarn()
configuration(file("<http://proguard.pro|proguard.pro>"))
The rules depends on your application and mainly libraries, for my use-case:
# Proguard Kotlin Example <https://github.com/Guardsquare/proguard/blob/master/examples/application-kotlin/proguard.pro>
-keepattributes *Annotation*
-keep class kotlin.Metadata { *; }
# Entry point to the app.
-keep class MainKt { *; }
# Kotlinx Serialization <https://github.com/Kotlin/kotlinx.serialization/blob/master/rules/common.pro>
# Keep `Companion` object fields of serializable classes.
# This avoids serializer lookup through `getDeclaredClasses` as done for named companion objects.
-if @kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
static <1>$Companion Companion;
}
# Keep `serializer()` on companion objects (both default and named) of serializable classes.
-if @kotlinx.serialization.Serializable class ** {
static **$* *;
}
-keepclassmembers class <2>$<3> {
kotlinx.serialization.KSerializer serializer(...);
}
# Keep `INSTANCE.serializer()` of serializable objects.
-if @kotlinx.serialization.Serializable class ** {
public static ** INSTANCE;
}
-keepclassmembers class <1> {
public static <1> INSTANCE;
kotlinx.serialization.KSerializer serializer(...);
}
# @Serializable and @Polymorphic are used at runtime for polymorphic serialization.
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
# Don't print notes about potential mistakes or omissions in the configuration for kotlinx-serialization classes
# See also <https://github.com/Kotlin/kotlinx.serialization/issues/1900>
-dontnote kotlinx.serialization.**
# Serialization core uses `java.lang.ClassValue` for caching inside these specified classes.
# If there is no `java.lang.ClassValue` (for example, in Android), then R8/ProGuard will print a warning.
# However, since in this case they will not be used, we can disable these warnings
-dontwarn kotlinx.serialization.internal.ClassValueReferences
# Kotlinx Coroutines <https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro>
# ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
# Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembers class kotlinx.coroutines.** {
volatile <fields>;
}
# Same story for the standard library's SafeContinuation that also uses AtomicReferenceFieldUpdater
-keepclassmembers class kotlin.coroutines.SafeContinuation {
volatile <fields>;
}
# These classes are only required by kotlinx.coroutines.debug.AgentPremain, which is only loaded when
# kotlinx-coroutines-core is used as a Java agent, so these are not needed in contexts where ProGuard is used.
-dontwarn java.lang.instrument.ClassFileTransformer
-dontwarn sun.misc.SignalHandler
-dontwarn java.lang.instrument.Instrumentation
-dontwarn sun.misc.Signal
# Only used in `kotlinx.coroutines.internal.ExceptionsConstructor`.
# The case when it is not available is hidden in a `try`-`catch`, as well as a check for Android.
-dontwarn java.lang.ClassValue
# An annotation used for build tooling, won't be directly accessed.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# OkHttp <https://square.github.io/okhttp/features/r8_proguard/>
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keeppackagenames okhttp3.internal.publicsuffix.*
-adaptresourcefilenames okhttp3/internal/publicsuffix/PublicSuffixDatabase.gz
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# OkHttp platform used only on JVM and when Conscrypt and other security providers are available.
-dontwarn okhttp3.internal.platform.**
-dontwarn org.conscrypt.**
-dontwarn org.bouncycastle.**
-dontwarn org.openjsse.**
# Okio (missing)
# FlatLaf
# Exclude the entire FlatLaf dependency from minimization to fix `no ComponentUI class for: javax.swing.<component>`
# Due to reflections, more details: <https://github.com/JFormDesigner/FlatLaf/issues/648#issuecomment-1441547550>
-keep class com.formdev.flatlaf.** { *; }