Move to R8 shrinker, tweak compile args

This commit is contained in:
comp500 2022-06-19 18:58:49 +01:00
parent 858fd17f3e
commit d2556c4b4a
2 changed files with 44 additions and 42 deletions

View File

@ -1,14 +1,3 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.guardsquare:proguard-gradle:7.1.0") {
exclude("com.android.tools.build")
}
}
}
plugins { plugins {
java java
application application
@ -26,8 +15,11 @@ java {
repositories { repositories {
mavenCentral() mavenCentral()
google()
} }
val r8 by configurations.creating
dependencies { dependencies {
implementation("commons-cli:commons-cli:1.5.0") implementation("commons-cli:commons-cli:1.5.0")
implementation("com.moandjiezana.toml:toml4j:0.7.2") implementation("com.moandjiezana.toml:toml4j:0.7.2")
@ -36,6 +28,8 @@ dependencies {
implementation(kotlin("stdlib-jdk8")) implementation(kotlin("stdlib-jdk8"))
implementation("com.squareup.okhttp3:okhttp:4.9.3") implementation("com.squareup.okhttp3:okhttp:4.9.3")
implementation("com.michael-bull.kotlin-result:kotlin-result:1.1.14") implementation("com.michael-bull.kotlin-result:kotlin-result:1.1.14")
r8("com.android.tools:r8:3.3.28")
} }
application { application {
@ -60,8 +54,6 @@ licenseReport {
} }
tasks.shadowJar { tasks.shadowJar {
exclude("**/*.kotlin_metadata")
exclude("**/*.kotlin_builtins")
exclude("META-INF/maven/**/*") exclude("META-INF/maven/**/*")
exclude("META-INF/proguard/**/*") exclude("META-INF/proguard/**/*")
@ -73,33 +65,24 @@ tasks.shadowJar {
exclude("META-INF/NOTICE.txt") exclude("META-INF/NOTICE.txt")
} }
tasks.register<proguard.gradle.ProGuardTask>("shrinkJar") { tasks.register<JavaExec>("shrinkJar") {
injars(tasks.shadowJar) val rules = file("src/main/proguard.txt")
outjars("build/libs/" + tasks.shadowJar.get().outputs.files.first().name.removeSuffix(".jar") + "-shrink.jar") val r8File = tasks.shadowJar.get().archiveFile.get().asFile.run { resolveSibling(name.removeSuffix(".jar") + "-shrink.jar") }
if (System.getProperty("java.version").startsWith("1.")) { dependsOn(configurations.named("runtimeClasspath"))
libraryjars("${System.getProperty("java.home")}/lib/rt.jar") inputs.files(tasks.shadowJar, rules)
libraryjars("${System.getProperty("java.home")}/lib/jce.jar") outputs.file(r8File)
} else {
// Use jmods for Java 9+
val mods = listOf("java.base", "java.logging", "java.desktop", "java.sql")
for (mod in mods) {
libraryjars(mapOf(
"jarfilter" to "!**.jar",
"filter" to "!module-info.class"
), "${System.getProperty("java.home")}/jmods/$mod.jmod")
}
}
keep("class link.infra.packwiz.installer.** { *; }") classpath(r8)
dontoptimize() mainClass.set("com.android.tools.r8.R8")
dontobfuscate() args = mutableListOf(
"--release",
// Used by Okio and OkHttp "--classfile",
dontwarn("org.codehaus.mojo.animal_sniffer.*") "--output", r8File.toString(),
dontwarn("okhttp3.internal.platform.**") "--pg-conf", rules.toString(),
dontwarn("org.conscrypt.**") "--lib", System.getProperty("java.home"),
dontwarn("org.bouncycastle.**") "--lib", System.getProperty("java.home") + "/lib/jce.jar", // javax.crypto, necessary on <1.9 for compiling Okio
dontwarn("org.openjsse.**") tasks.shadowJar.get().archiveFile.get().asFile.toString()
)
} }
// Used for vscode launch.json // Used for vscode launch.json
@ -107,9 +90,11 @@ tasks.register<Copy>("copyJar") {
from(tasks.named("shrinkJar")) from(tasks.named("shrinkJar"))
rename("packwiz-installer-(.*)\\.jar", "packwiz-installer.jar") rename("packwiz-installer-(.*)\\.jar", "packwiz-installer.jar")
into("build/libs/") into("build/libs/")
outputs.file("build/libs/packwiz-installer.jar")
} }
tasks.build { tasks.assemble {
dependsOn("shrinkJar")
dependsOn("copyJar") dependsOn("copyJar")
} }
@ -132,13 +117,13 @@ if (project.hasProperty("github.token")) {
tasks.compileKotlin { tasks.compileKotlin {
kotlinOptions { kotlinOptions {
jvmTarget = "1.8" jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjvm-default=enable", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi") freeCompilerArgs = listOf("-Xjvm-default=all", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi", "-Xlambdas=indy")
} }
} }
tasks.compileTestKotlin { tasks.compileTestKotlin {
kotlinOptions { kotlinOptions {
jvmTarget = "1.8" jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjvm-default=enable", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi") freeCompilerArgs = listOf("-Xjvm-default=all", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi", "-Xlambdas=indy")
} }
} }

17
src/main/proguard.txt Normal file
View File

@ -0,0 +1,17 @@
-dontoptimize
-dontobfuscate
# Used by Okio and OkHttp
-dontwarn org.codehaus.mojo.animal_sniffer.*
-dontwarn okhttp3.internal.platform.**
-dontwarn org.conscrypt.**
-dontwarn org.bouncycastle.**
-dontwarn org.openjsse.**
-keep class !link.infra.packwiz.installer.deps.**,link.infra.packwiz.installer.** { *; }
-keep class com.google.gson.reflect.TypeToken { *; }
-keep class * extends com.google.gson.reflect.TypeToken
-renamesourcefileattribute SourceFile
-keepattributes *Annotation*,SourceFile,LineNumberTable,Signature