The Riffbeam desktop app is written in Compose Multiplatform and runs on the JVM, so it ships with its own Java runtime. The MSI installer weighed 125 MB, which is a lot for a songbook. At the end of October we looked at where the size comes from and how much of it we could drop.

What takes up the space

The app folder from createDistributable held 120.6 MB of code and libraries plus a 65 MB Java runtime, already trimmed by jlink to the modules we need. Zipped, it came to 125.2 MB. The biggest items were:

  • material-icons-extended: 36 MB, and we use a fraction of those icons
  • Skiko: 13.5 MB
  • ICU: 10 MB
  • BouncyCastle: 9.8 MB

The icon pack alone is almost a third of the code, so the easiest win is to strip its unused classes. That is what ProGuard does, and the Compose Gradle plugin already has it built in.

Four attempts

Turning ProGuard on is one line in Gradle, but with the default settings the app didn’t survive startup.

  1. With optimisation, the default: 73.4 MB, but startup ended in a VerifyError in okio.
  2. With optimize = false: 76.8 MB, this time a VerifyError in kotlinx.coroutines, because ProGuard removed an interface that JobSupport relies on.
  3. Keeping all of kotlin.** and kotlinx.**: 78.0 MB. The app drew its first screen and then crashed on libraries loaded through ServiceLoader (cryptography-kotlin and the TwelveMonkeys WebP decoder). ProGuard can’t tell that these classes are loaded by name from META-INF/services, so it strips them.
  4. Keeping those libraries too: 79.4 MB, and the app starts and runs without exceptions.

The package ended up 37% smaller, and nearly all of the gain came from the stripped icons. We measured a zip of the app folder, not the finished installer.

Obfuscation in releases

Last, we turned on obfuscation, but only for our own code. Library names stay, because many libraries look up classes by name (ServiceLoader, JNA, PDFBox, Ktor engines), and renaming them hides nothing anyway. So that stack traces in bug reports stay readable, ProGuard keeps line numbers and writes a mapping file. Every release has its own, and we use it to recover the real class and method names.

Since 29 October all Riffbeam releases for Windows (MSI and the portable version) and macOS are built with ProGuard. The Android version has gone through R8, which does the same for the APK, for a long time.

Settings

In the desktop module’s build.gradle.kts:

compose.desktop {
    application {
        buildTypes.release.proguard {
            isEnabled.set(true)
            obfuscate.set(true)
            optimize.set(false)
            configurationFiles.from(project.file("proguard-rules.pro"))
        }
    }
}

A shortened rules file. Replace com.example with your own package:

# line numbers for reading stack traces with retrace
-keepattributes SourceFile,LineNumberTable
-renamesourcefileattribute SourceFile
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod
-printmapping build/proguard/mapping.txt

-keep class com.example.MainKt { public static void main(java.lang.String[]); }
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keepclassmembers @kotlinx.serialization.Serializable class ** {
    *** Companion;
    kotlinx.serialization.KSerializer serializer(...);
}
-keep,includedescriptorclasses class **$$serializer { *; }

# libraries stay whole, ProGuard only cuts Compose and our own code
-keep class !androidx.compose.**,!com.example.**,** { *; }

-dontwarn **
-ignorewarnings

Only tasks with Release in the name, such as createReleaseDistributable or packageReleaseMsi, build the ProGuard package. Plain packageMsi still builds the full one.

Watch out for

  • ProGuard’s optimisation broke library bytecode, so we turned it off. Obfuscation works without it.
  • Keep the mapping file for every release. Without it, a stack trace from a bug report is a string of a.b.c.
  • -dontwarn ** and -ignorewarnings silence warnings, so a missing class only shows up when the feature that needs it runs. After a change like this you have to click through the whole app; just launching it is not enough.
  • The rule that keeps every library is broad. We tried narrowing it, but startup broke on classes removed from libraries, and the gain was small, since Compose frees most of the space.