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..bb11157b32 100644 --- a/mirai-core-all/build.gradle.kts +++ b/mirai-core-all/build.gradle.kts @@ -9,6 +9,14 @@ @file:Suppress("UnusedImport") +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar + +buildscript { + dependencies { + classpath("com.guardsquare:proguard-gradle:${Versions.proguard}") + } +} + plugins { kotlin("jvm") kotlin("plugin.serialization") @@ -26,4 +34,58 @@ 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" + + verbose() + + injars(tasks.getByName("shadowJar")) + + 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() + // 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..1241e38637 --- /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 + +# Serializer on Mirai +-keep,includedescriptorclasses class net.mamoe.mirai.**$$serializer { *; } # <-- change package name to your app's +-keepclassmembernames class net.mamoe.mirai.** { + *** Companion; +} +-keepclassmembernames class net.mamoe.mirai.** { + kotlinx.serialization.KSerializer serializer(...); + static kotlinx.serialization.KSerializer serializer(...); +} diff --git a/mirai-core-all/mirai.pro b/mirai-core-all/mirai.pro new file mode 100644 index 0000000000..0392e6c67f --- /dev/null +++ b/mirai-core-all/mirai.pro @@ -0,0 +1,30 @@ +-dontwarn +#-keep @io.netty.channel.ChannelHandler.Sharable class ** + +-keepclassmembernames class kotlinx.** { + volatile ; +} + +-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 net.mamoe.mirai.** { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +#-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