Skip to content

Commit

Permalink
Add r8Jar task to minify fatJar using R8
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Nov 9, 2023
1 parent 9644235 commit f7667f7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ junit5 = "org.junit.jupiter:junit-jupiter:5.10.1"
assertj = "org.assertj:assertj-core:3.24.2"
sarif4k = "io.github.detekt.sarif4k:sarif4k:0.5.0"
jimfs = "com.google.jimfs:jimfs:1.3.0"
r8 = "com.android.tools:r8:8.1.56"
60 changes: 46 additions & 14 deletions ktlint-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ plugins {
signing
}

val r8: Configuration by configurations.creating

tasks.jar {
manifest {
attributes("Main-Class" to "com.pinterest.ktlint.Main")
Expand Down Expand Up @@ -37,29 +39,59 @@ dependencies {
runtimeOnly(projects.ktlintCliReporterSarif)

testImplementation(projects.ktlintTest)

r8(libs.r8)
}

val r8File =
layout
.buildDirectory
.file("libs/ktlint-cli-$version-r8.jar")
.get()
.asFile
val rulesFile = file("src/main/proguard.pro")
val fatJarFile = tasks.shadowJar.get().archiveFile
val r8Jar by tasks.registering(JavaExec::class) {
description = "Creates a jar minified from the fat jar using R8"
group = "Distribution"
dependsOn(tasks.shadowJar)

// Find the "ktlint-cli-<version>-all.jar" file
val fatJar =
tasks
.shadowJar
.get()
.outputs
.files
.singleFile
inputs.file(fatJar)
inputs.file(rulesFile)
outputs.file(r8File)

classpath = r8
mainClass = "com.android.tools.r8.R8"
args =
listOf(
"--release",
"--classfile",
"--output", r8File.toString(),
"--pg-conf", rulesFile.path,
"--lib", System.getProperty("java.home").toString(),
fatJarFile.get().toString(),
)
}

// Implements https://github.com/brianm/really-executable-jars-maven-plugin maven plugin behaviour.
// To check details how it works, see https://skife.org/java/unix/2011/06/20/really_executable_jars.html.
val shadowJarExecutable by tasks.registering(DefaultTask::class) {
description = "Creates self-executable file, that runs generated shadow jar"
group = "Distribution"

dependsOn(tasks.shadowJar)
dependsOn(r8Jar)

val isReleaseBuild = !version.toString().endsWith("SNAPSHOT")

// Find the "ktlint-cli-<version>-all.jar" file
val ktlintCliAllJarFile =
tasks
.shadowJar
.orNull
?.outputs
?.files
?.singleFile
?: throw GradleException("Can not locate the jar file for building the self-executable ktlint-cli")
logger.lifecycle("ktlint-cli: Base jar to build self-executable file: ${ktlintCliAllJarFile.absolutePath}")
inputs.files(ktlintCliAllJarFile)
logger.lifecycle("ktlint-cli: Base jar to build self-executable file: ${r8File.absolutePath}")
inputs.files(r8File)

val windowsBatchFileInputPath = "$projectDir/src/main/scripts/ktlint.bat"
inputs.files(windowsBatchFileInputPath)
Expand Down Expand Up @@ -100,7 +132,7 @@ val shadowJarExecutable by tasks.registering(DefaultTask::class) {
""".trimIndent(),
)
// Add the jar
appendBytes(ktlintCliAllJarFile.readBytes())
appendBytes(r8File.readBytes())

setExecutable(true, false)

Expand Down
17 changes: 17 additions & 0 deletions ktlint-cli/src/main/proguard.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-dontobfuscate
-keepattributes SourceFile, LineNumberTable

-allowaccessmodification

-keep class com.pinterest.ktlint.Main {
public static void main(java.lang.String[]);
}

-dontwarn com.sun.jna.Native.**
-dontwarn javax.servlet.ServletContainerInitializer
-dontwarn kotlin.annotations.jvm.**
-dontwarn kotlinx.coroutines.**
-dontwarn org.checkerframework.checker.nullness.qual.**
-dontwarn org.codehaus.janino.ClassBodyEvaluator
-dontwarn org.jetbrains.annotations.**
-dontwarn org.jetbrains.kotlin.com.google.**
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencyResolutionManagement {
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
repositories {
mavenCentral()
google()
}
}

Expand Down

0 comments on commit f7667f7

Please sign in to comment.