Minify the shadowed JAR with R8 - #669
Conversation
Goooler
commented
Aug 12, 2026
|
Just a quick note: we don't need to merge this for now. The main purpose here is to show the difference between the before and after. #584 has been merged for minification and to provide a binary JAR. It should be a better replacement for this version. |
|
BTW, I fed the output of the shadowed JAR into the native image input and the final binary size was reduced slightly. |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
This is really nice improvement, thanks for submitting the PR. I noticed you did not update the |
|
I sent this to demonstrate a potential use case, but I don't have a strong opinion on whether it should be merged. Because there are so many cases that can't be covered by the current tests. I would prefer closing this or holding it. |
|
I think its worthwhile to do (I contributed this feature to shadow!) and it's doable to make a custom Test task that tests with this minified jar. I do think it shouldn't be obfuscated though, as that'll make bugs nigh-impossible to report for users. I don't know how this would interact with native image though, it may require a bit more work |
|
|
||
| dependencies { add(ktfmtCliDependencies.name, project(":ktfmt")) } | ||
| dependencies { | ||
| ktfmtCliDependencies(project(path = ":ktfmt", configuration = "shadow")) |
There was a problem hiding this comment.
This is too annoying for developments. Reverted.
| // Run unit tests against the R8-processed shadow JAR to ensure Maven API compatibility | ||
| val testShadow = | ||
| register("testShadow", Test::class) { | ||
| group = "verification" | ||
| description = "Runs unit tests against the R8-processed shadow JAR." | ||
| dependsOn(shadowJar) | ||
| classpath = | ||
| (sourceSets["test"].runtimeClasspath - sourceSets["main"].output) + | ||
| files(shadowJar.flatMap { it.archiveFile }) | ||
| testClassesDirs = sourceSets["test"].output.classesDirs | ||
| } | ||
|
|
||
| check { | ||
| dependsOn(testShadow) |
There was a problem hiding this comment.
This should cover the test cases in current behaviors. Refined a bit.
| classpath.setFrom( | ||
| nativeImageJar.flatMap { it.archiveFile }, | ||
| project.tasks.named("shadowJar", Jar::class).flatMap { it.archiveFile }, | ||
| project.configurations.getByName("nativeImageClasspath"), |
There was a problem hiding this comment.
We have no much necessary to feed the shadowJar to the native image building. Reverted.
b8ea180 to
7f1c55b
Compare
|
```
OLD: old.jar
NEW: new.jar
│ compressed │ uncompressed
├────────────┬───────────┬─────────────┼────────────┬───────────┬─────────────
JAR │ old │ new │ diff │ old │ new │ diff
───────┼────────────┼───────────┼─────────────┼────────────┼───────────┼─────────────
class │ 66.09 MiB │ 14.12 MiB │ -51.97 MiB │ 168 MiB │ 28 MiB │ -140.01 MiB
other │ 997.03 KiB │ 826.3 KiB │ -170.72 KiB │ 2.03 MiB │ 1.94 MiB │ -87.19 KiB
───────┼────────────┼───────────┼─────────────┼────────────┼───────────┼─────────────
total │ 67.07 MiB │ 14.93 MiB │ -52.14 MiB │ 170.03 MiB │ 29.94 MiB │ -140.09 MiB
CLASSES │ old │ new │ diff
─────────┼────────┼───────┼───────────────────────
classes │ 32913 │ 10230 │ -22683 (+1 -22684)
methods │ 309579 │ 70944 │ -238635 (+72 -238707)
fields │ 78687 │ 22714 │ -55973 (+12 -55985)
```
7f1c55b to
411a290
Compare