Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Proguard for mirai-core-all #992

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
64 changes: 63 additions & 1 deletion mirai-core-all/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -26,4 +34,58 @@ dependencies {
api(project(":mirai-core-utils"))
}

configurePublishing("mirai-core-all")
configurePublishing("mirai-core-all")

afterEvaluate {
tasks.register<proguard.gradle.ProGuardTask>("proguard") {
group = "mirai"

verbose()

injars(tasks.getByName<ShadowJar>("shadowJar"))
Him188 marked this conversation as resolved.
Show resolved Hide resolved

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")
}

}
24 changes: 24 additions & 0 deletions mirai-core-all/kotlinx-serialization.pro
Original file line number Diff line number Diff line change
@@ -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 <fields>;
}
-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(...);
}
30 changes: 30 additions & 0 deletions mirai-core-all/mirai.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-dontwarn
#-keep @io.netty.channel.ChannelHandler.Sharable class **

-keepclassmembernames class kotlinx.** {
volatile <fields>;
}

-keepclassmembernames class kotlin.coroutines.SafeContinuation {
volatile <fields>;
}
# Fields used by atomicfu
-keepclassmembers class net.mamoe.mirai.** {
volatile <fields>;
}
-keepclassmembernames class net.mamoe.mirai.** {
volatile <fields>;
}
-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