https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
r

Racka N

07/28/2022, 5:39 AM
Has anyone here got Proguard or R8 working successfully? I got it to shrink my app and run it after using this: https://github.com/JetBrains/compose-jb/issues/1174#issuecomment-1075155835 But it doesn't seem to respect the proguard-rules. Like all my
@Serializable
annotations have been removed despite adding the required rules for
kotlinx.Serialization
and I get a runtime crash when I initiate a network call.
SLF4J
also gets removed.
s

spierce7

07/28/2022, 5:45 AM
I had weird issues, and never quite got it working. The jar would grow on recompiles, so clearly there was a code leak somehow, and when I got it compiling, all of a sudden certain classes couldn’t initialize, even though they were there.
m

mcpiroman

07/28/2022, 8:25 AM
I've been using PG successfully for some time, but I do exclude some dependency jars completely (not even in PG config but in gradle script) and I also don't use kotlinx.serialization.
r

Racka N

07/28/2022, 8:32 AM
Could you share your config? I guess I would have to exclude some jars. I'm also getting
IOException
warnings. Would love to take a look at your build.gradle @mcpiroman
@spierce7 It seems to shrink reliably for me. My installer is now 46MB (instead of 90MB) even after multiple recompiles. The missing serializers and SLF4J are the only major blockers. They just get removed and I don't know how to bring them back.
m

mcpiroman

07/29/2022, 9:18 AM
@Racka N
Copy code
val projectJars = project.rootProject.allprojects.flatMap { proj -> proj.tasks.withType(Jar::class.java).flatMap { it.outputs.files } }
val allJars = tasks.jar.get().outputs.files + sourceSets.main.get().runtimeClasspath.filter { it.path.endsWith(".jar") }
    .filterNot { it.name.startsWith("skiko-awt-") && !it.name.startsWith("skiko-awt-runtime-") } // walkaround <https://github.com/JetBrains/compose-jb/issues/1971>
val (obfuscatedFiles, notObfuscatedFiles) = allJars.partition {
    it in projectJars ||
            ("-" + it.name).contains("-desktop-")
}

fun mapObfuscatedJarFile(file: File) =
    File("${project.buildDir}/tmp/obfuscated/${file.nameWithoutExtension}.min.jar")

val obfuscate by tasks.registering(proguard.gradle.ProGuardTask::class)

compose.desktop {
    application {
		/* ... */
	
        disableDefaultConfiguration()
        fromFiles(obfuscate.get().outputs.files.asFileTree)
        fromFiles(notObfuscatedFiles)
        mainJar.set(tasks.jar.map { RegularFile { mapObfuscatedJarFile(it.archiveFile.get().asFile) } })
    }
}

tasks {
    obfuscate.configure {
        dependsOn(jar.get())

        for (file in obfuscatedFiles) {
            injars(file)
            outjars(mapObfuscatedJarFile(file))
        }

        libraryjars("${compose.desktop.application.javaHome ?: System.getProperty("java.home")}/jmods")
        libraryjars(notObfuscatedFiles)

        configuration("<http://proguard-rules.pro|proguard-rules.pro>")
    }
}
Oh and I have `-dontobfuscate`rule, although it may have worked with it too
r

Racka N

07/29/2022, 9:35 AM
I have a very similar setup except the
notObfuscatedFiles
part. Why are you filtering out jars with "-desktop-"? I actually got it to stop removing my
@Serializable
classes but seems like there's more stuff missing because Ktor doesn't work and now I just get
java.lang.RuntimeException: java.lang.NoSuchFieldException: top
error when I do network calls.
So tried to add
-dontobfuscate
and that seems to have solved my issue with Ktor. Though would really like to have it obfuscate things. That'll do for now. The package is now 52MB instead of the 90MB it was. Have you managed to setup the defined
obfuscate
task to only run when you use the
packageX
tasks or
createDistributable
and not when you just do a simple
:run
task? Because right now I runs the obfuscate task even when you do
gradlew :run
and this can slow down development a bit since the
run
task doesn't even use the obfuscated jar.
m

mcpiroman

07/30/2022, 7:24 AM
Why are you filtering out jars with "-desktop-"?
Actually, I don't remember.
right now I runs the obfuscate task even when you do
gradlew :run
and this can slow down development a bit since the
run
task doesn't even use the obfuscated jar.
For development I always use the IJ's
Kotlin
, not `Gradle`configuration (created e.g. when you click the green triangle next to the main function) and that does not trigger obfuscation. Sometimes I `runDistributable`to check whether the resulting build works. And I don't remember if I use `run`itself, but I also have a feeling that it was not slowed down.
r

Racka N

07/30/2022, 8:13 AM
:run
itself definitely triggers proguard. I totally forgot about running from Main function. I always use
:run
and
:runDistributable
(for testing distributable). Oh well, I already used an environment variable to disable or enable Proguard.
40 Views