From b7743307a78ca8c584beb119c7b2df1b47967e25 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 25 Dec 2020 20:53:42 +0800 Subject: [PATCH 1/9] Configure Proguard for mirai-core-all --- buildSrc/src/main/kotlin/Versions.kt | 1 + mirai-core-all/build.gradle.kts | 70 +++++++++++++++++++++++- mirai-core-all/kotlinx-serialization.pro | 24 ++++++++ mirai-core-all/mirai.pro | 22 ++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 mirai-core-all/kotlinx-serialization.pro create mode 100644 mirai-core-all/mirai.pro diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 8203dbef28..0030a90f4a 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -52,6 +52,7 @@ object Versions { const val kotlinIntellijPlugin = "203-1.4.30-release-IJ7148.5" // keep to newest as kotlinCompiler const val intellij = "2020.3.2" // don't update easily unless you want your disk space -= 500MB + const val proguard = "7.0.1" } @Suppress("unused") diff --git a/mirai-core-all/build.gradle.kts b/mirai-core-all/build.gradle.kts index 28bcd31b52..566bfded2b 100644 --- a/mirai-core-all/build.gradle.kts +++ b/mirai-core-all/build.gradle.kts @@ -9,6 +9,12 @@ @file:Suppress("UnusedImport") +buildscript { + dependencies { + classpath("com.guardsquare:proguard-gradle:${Versions.proguard}") + } +} + plugins { kotlin("jvm") kotlin("plugin.serialization") @@ -26,4 +32,66 @@ dependencies { api(project(":mirai-core-utils")) } -configurePublishing("mirai-core-all") \ No newline at end of file +configurePublishing("mirai-core-all") + +afterEvaluate { + tasks.register("proguard") { + group = "mirai" + dependsOn("jar", "shadowJar") + + verbose() + + injars("build/libs/${project.name}-${project.version}-all.jar") + + kotlin.runCatching { + file("build/libs/${project.name}-${project.version}-all-min.jar").delete() + }.exceptionOrNull()?.printStackTrace() + outjars("build/libs/${project.name}-${project.version}-all-min.jar") + + val kotlinLibraries = kotlin.target.compilations["main"].compileDependencyFiles + // .plus(kotlin.target.compilations["main"].runtimeDependencyFiles) + + kotlinLibraries.distinctBy { it.normalize().name }.forEach { file -> + if (file.name.contains("-common")) return@forEach + if (file.name.contains("-metadata")) return@forEach + if (file.extension == "jar") libraryjars(file) + } + + val javaHome = System.getProperty("java.home") + // 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("$javaHome/lib/rt.jar") + } else { + File(javaHome, "jmods").listFiles().orEmpty().forEach { file -> + libraryjars( + mapOf( + "jarfilter" to "!**.jar", + "filter" to "!module-info.class" + ), file + ) + } +// // As of Java 9, the runtime classes are packaged in modular jmod files. +// libraryjars( +// // filters must be specified first, as a map +// mapOf("jarfilter" to "!**.jar", +// "filter" to "!module-info.class"), +// "$javaHome/jmods/java.base.jmod" +// ) + } + + configuration("mirai.pro") + configuration("kotlinx-serialization.pro") + + dontobfuscate() + keep("""class net.mamoe.mirai.message.**""") + keep("""class net.mamoe.mirai.network.**""") + keep("""class net.mamoe.mirai.utils.**""") + keep("""class net.mamoe.mirai.contact.**""") + keep("""class net.mamoe.mirai.internal.message.**""") + keep("""class net.mamoe.mirai.internal.utils.**""") + keep("""class net.mamoe.mirai.internal.contact.**""") + // keepattributes("*Annotation*,synthetic") + } + +} diff --git a/mirai-core-all/kotlinx-serialization.pro b/mirai-core-all/kotlinx-serialization.pro new file mode 100644 index 0000000000..cf2d7aa7a9 --- /dev/null +++ b/mirai-core-all/kotlinx-serialization.pro @@ -0,0 +1,24 @@ +-keepattributes RuntimeVisibleAnnotations, InnerClasses, RuntimeVisibleTypeAnnotations, AnnotationDefault, LocalVariableTypeTable, Signature +-dontnote kotlinx.serialization.AnnotationsKt # core serialization annotations + +# kotlinx-serialization-json specific. Add this if you have java.lang.NoClassDefFoundError kotlinx.serialization.json.JsonObjectSerializer +-keepclassmembers class kotlinx.serialization.json.** { + *** Companion; +} +-keepclasseswithmembers class kotlinx.serialization.json.** { + kotlinx.serialization.KSerializer serializer(...); +} +-keepclassmembers class ** extends java.lang.Enum { + static ; +} +-keepnames class ** extends java.lang.Enum + +# Change here com.yourcompany.yourpackage +-keep,includedescriptorclasses class net.mamoe.mirai.**$$serializer { *; } # <-- change package name to your app's +-keepclassmembernames class net.mamoe.mirai.** { # <-- change package name to your app's + *** Companion; +} +-keepclassmembernames class net.mamoe.mirai.** { # <-- change package name to your app's + kotlinx.serialization.KSerializer serializer(...); + static kotlinx.serialization.KSerializer serializer(...); +} \ No newline at end of file diff --git a/mirai-core-all/mirai.pro b/mirai-core-all/mirai.pro new file mode 100644 index 0000000000..c9277c7a28 --- /dev/null +++ b/mirai-core-all/mirai.pro @@ -0,0 +1,22 @@ +-dontwarn +#-keep @io.netty.channel.ChannelHandler.Sharable class ** + +-keepclassmembernames class kotlinx.** { + volatile ; +} + +-keepclassmembernames class kotlin.coroutines.SafeContinuation { + volatile ; +} + +-keepclassmembernames class ** extends kotlinx.serialization.internal.GeneratedSerializer + +-keep enum ** { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +#-keep class ** extends kotlin.internal.** + +-keepnames class net.mamoe.mirai.Mirai +-keepnames class net.mamoe.mirai.MiraiImpl \ No newline at end of file From 7715352ac4c472ce39d41459d399853e849ff6e8 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 12:53:39 +0800 Subject: [PATCH 2/9] =?UTF-8?q?injars=20=E4=BE=9D=E8=B5=96=20shadowJar=20?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: AdoptOSS --- mirai-core-all/build.gradle.kts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mirai-core-all/build.gradle.kts b/mirai-core-all/build.gradle.kts index 566bfded2b..1cb83025fc 100644 --- a/mirai-core-all/build.gradle.kts +++ b/mirai-core-all/build.gradle.kts @@ -37,11 +37,10 @@ configurePublishing("mirai-core-all") afterEvaluate { tasks.register("proguard") { group = "mirai" - dependsOn("jar", "shadowJar") verbose() - injars("build/libs/${project.name}-${project.version}-all.jar") + injars(tasks.getByName("shadowJar")) kotlin.runCatching { file("build/libs/${project.name}-${project.version}-all-min.jar").delete() From 129742d6da39d0bce6ca87fd996f38775c2dfeb4 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 12:54:24 +0800 Subject: [PATCH 3/9] Update mirai-core-all/kotlinx-serialization.pro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改注释 Co-authored-by: AdoptOSS --- mirai-core-all/kotlinx-serialization.pro | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mirai-core-all/kotlinx-serialization.pro b/mirai-core-all/kotlinx-serialization.pro index cf2d7aa7a9..602333150b 100644 --- a/mirai-core-all/kotlinx-serialization.pro +++ b/mirai-core-all/kotlinx-serialization.pro @@ -13,7 +13,7 @@ } -keepnames class ** extends java.lang.Enum -# Change here com.yourcompany.yourpackage +# Serializer on Mirai -keep,includedescriptorclasses class net.mamoe.mirai.**$$serializer { *; } # <-- change package name to your app's -keepclassmembernames class net.mamoe.mirai.** { # <-- change package name to your app's *** Companion; @@ -21,4 +21,4 @@ -keepclassmembernames class net.mamoe.mirai.** { # <-- change package name to your app's kotlinx.serialization.KSerializer serializer(...); static kotlinx.serialization.KSerializer serializer(...); -} \ No newline at end of file +} From 2a7cf95ac69f88d5296e6fc69575e86d0a9628be Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 12:55:19 +0800 Subject: [PATCH 4/9] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改注释 Co-authored-by: AdoptOSS --- mirai-core-all/kotlinx-serialization.pro | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mirai-core-all/kotlinx-serialization.pro b/mirai-core-all/kotlinx-serialization.pro index 602333150b..1241e38637 100644 --- a/mirai-core-all/kotlinx-serialization.pro +++ b/mirai-core-all/kotlinx-serialization.pro @@ -15,10 +15,10 @@ # Serializer on Mirai -keep,includedescriptorclasses class net.mamoe.mirai.**$$serializer { *; } # <-- change package name to your app's --keepclassmembernames class net.mamoe.mirai.** { # <-- change package name to your app's +-keepclassmembernames class net.mamoe.mirai.** { *** Companion; } --keepclassmembernames class net.mamoe.mirai.** { # <-- change package name to your app's +-keepclassmembernames class net.mamoe.mirai.** { kotlinx.serialization.KSerializer serializer(...); static kotlinx.serialization.KSerializer serializer(...); } From 058ebd024d3a04d753f44fee1481310252f4f2a4 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 12:55:55 +0800 Subject: [PATCH 5/9] Update mirai-core-all/mirai.pro Keep Bouncy Castle Co-authored-by: AdoptOSS --- mirai-core-all/mirai.pro | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mirai-core-all/mirai.pro b/mirai-core-all/mirai.pro index c9277c7a28..7f66e91958 100644 --- a/mirai-core-all/mirai.pro +++ b/mirai-core-all/mirai.pro @@ -17,6 +17,8 @@ } #-keep class ** extends kotlin.internal.** - +# Bouncy Castle +-keep class org.bouncycastle.jcajce.provider.** { *; } +-keep class org.bouncycastle.jce.provider.** { *; } -keepnames class net.mamoe.mirai.Mirai --keepnames class net.mamoe.mirai.MiraiImpl \ No newline at end of file +-keepnames class net.mamoe.mirai.MiraiImpl From 1b2bfa4de504712357162a9d22e8808de5489d29 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 12:56:35 +0800 Subject: [PATCH 6/9] Update mirai-core-all/mirai.pro Add rules for atomicfu Co-authored-by: AdoptOSS --- mirai-core-all/mirai.pro | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mirai-core-all/mirai.pro b/mirai-core-all/mirai.pro index 7f66e91958..2039eccab5 100644 --- a/mirai-core-all/mirai.pro +++ b/mirai-core-all/mirai.pro @@ -8,7 +8,13 @@ -keepclassmembernames class kotlin.coroutines.SafeContinuation { volatile ; } - +# Fields used by atomicfu +-keepclassmembers class net.mamoe.mirai.** { + volatile ; +} +-keepclassmembernames class net.mamoe.mirai.** { + volatile ; +} -keepclassmembernames class ** extends kotlinx.serialization.internal.GeneratedSerializer -keep enum ** { From 3997095b885d8c11de0e6b243eb406efa5cc76e5 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 13:03:43 +0800 Subject: [PATCH 7/9] Add import for ShadowJar --- mirai-core-all/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mirai-core-all/build.gradle.kts b/mirai-core-all/build.gradle.kts index 1cb83025fc..3503a684d1 100644 --- a/mirai-core-all/build.gradle.kts +++ b/mirai-core-all/build.gradle.kts @@ -9,6 +9,8 @@ @file:Suppress("UnusedImport") +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar + buildscript { dependencies { classpath("com.guardsquare:proguard-gradle:${Versions.proguard}") From f0373a3377e353f573ee576215ab6bdf3ef5cf83 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 13:15:46 +0800 Subject: [PATCH 8/9] Update mirai-core-all/mirai.pro Co-authored-by: AdoptOSS --- mirai-core-all/mirai.pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mirai-core-all/mirai.pro b/mirai-core-all/mirai.pro index 2039eccab5..0392e6c67f 100644 --- a/mirai-core-all/mirai.pro +++ b/mirai-core-all/mirai.pro @@ -17,7 +17,7 @@ } -keepclassmembernames class ** extends kotlinx.serialization.internal.GeneratedSerializer --keep enum ** { +-keep enum net.mamoe.mirai.** { public static **[] values(); public static ** valueOf(java.lang.String); } From 5eb2ee8711fd22e2d38d4f41af7bcc00351c7640 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Mar 2021 13:17:19 +0800 Subject: [PATCH 9/9] Update mirai-core-all/build.gradle.kts Co-authored-by: AdoptOSS --- mirai-core-all/build.gradle.kts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/mirai-core-all/build.gradle.kts b/mirai-core-all/build.gradle.kts index 3503a684d1..bb11157b32 100644 --- a/mirai-core-all/build.gradle.kts +++ b/mirai-core-all/build.gradle.kts @@ -85,13 +85,6 @@ afterEvaluate { configuration("kotlinx-serialization.pro") dontobfuscate() - keep("""class net.mamoe.mirai.message.**""") - keep("""class net.mamoe.mirai.network.**""") - keep("""class net.mamoe.mirai.utils.**""") - keep("""class net.mamoe.mirai.contact.**""") - keep("""class net.mamoe.mirai.internal.message.**""") - keep("""class net.mamoe.mirai.internal.utils.**""") - keep("""class net.mamoe.mirai.internal.contact.**""") // keepattributes("*Annotation*,synthetic") }