Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable ProGuard's optimizations by default #3392

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ abstract class ProguardSettings @Inject constructor(
val configurationFiles: ConfigurableFileCollection = objects.fileCollection()
val isEnabled: Property<Boolean> = objects.notNullProperty(false)
val obfuscate: Property<Boolean> = objects.notNullProperty(false)
val optimize: Property<Boolean> = objects.notNullProperty(false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

release builds usually should be optimized.

Can we:

  • exclude only problematic functions/classes
  • write test that checks all classes, to find problematic (maybe not now, but in the future)

I wouldn't make this change only to speed up release and simplify the release process.

As an example, Android builds enable optimizations in the release build, and there is the rule that each third-party library should contain a valid ProGuard rules.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there is no such rule for desktop. With lots of external libraries it is often a PITA to get all the ProGuard rules right just in order to be able to do a release build. Maybe one should not carry over all Android habbits to the rest of the world.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there is no such rule for desktop

It is useful rule, otherwise we have a situation as you described. We have this situation though, becase indeed - there is no rule for JVM libraries.

But then, it is not only about optimization, ProGuard itself becomes useless. Every library can have some classes that require specific ProGuard rules (for example, if it uses reflection). We either should disable ProGuard completely, or provide a configuration that works for templates and popular libraries.

Not sure that disabling is a good idea, because there won't be any point in Release builds (ProGuard is the only difference at the moment)

Regarding the androidx/compose/animation/core/AnimationSpecKt.estimateAnimationDurationMillis issue - it is Compose class, so it should be fixed in Compose ProGuard rules

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a dillemma. I have to admit that I do not have an Android background but I have been using JavaFX with GraalVM/native-image which conceptually has a similar problem and so I am wondering whether you ever considered something like the native-image agent (https://www.graalvm.org/22.0/reference-manual/native-image/Agent/) which I found to be very helpful and easier to use than guessing ProGuard rules for libraries which are not your own. Would some kind of a bridge between the agents output and ProGuard be possible? Just thinking.

Copy link
Collaborator

@igordmn igordmn Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

native-image/Agent looks interesting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we:

  • exclude only problematic functions/classes
  • write test that checks all classes, to find problematic (maybe not now, but in the future)

In both cases most likely no.
I don't see any way to exclude just one class from optimization.
Also, simply forcing classloading does not trigger the verification error, which is described in the issue.
Also, I'm really not sure, if PG optimizations are actually beneficial with Hotspot. The desktop JVM is much better at optimizing code than mobile. Our primary motivation for supporting PG was to enable minification, not making bytecode faster

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After having a closer look at https://www.guardsquare.com/manual/configuration/optimizations I am a bit shocked to realize what PG is actually trying to do with my code by default. This really looks like fishing for trouble to me where all I really want is a reliable build combined with some reasonable code reduction. Packaging your app with the most recent JVM is probably more beneficial and safer than applying all this bit fiddling.

}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ private fun JvmApplicationContext.configureProguardTask(
// That's why a task property is follows ProGuard design,
// when our DSL does the opposite.
dontobfuscate.set(settings.obfuscate.map { !it })
dontoptimize.set(settings.optimize.map { !it })

dependsOn(unpackDefaultResources)
defaultComposeRulesFile.set(unpackDefaultResources.flatMap { it.resources.defaultComposeProguardRules })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ abstract class AbstractProguardTask : AbstractComposeDesktopTask() {
@get:Input
val dontobfuscate: Property<Boolean?> = objects.nullableProperty()

@get:Optional
@get:Input
val dontoptimize: Property<Boolean?> = objects.nullableProperty()

// todo: DSL for excluding default rules
// also consider pulling coroutines rules from coroutines artifact
// https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro
Expand Down Expand Up @@ -109,6 +113,10 @@ abstract class AbstractProguardTask : AbstractComposeDesktopTask() {
writer.writeLn("-dontobfuscate")
}

if (dontoptimize.orNull == true) {
writer.writeLn("-dontoptimize")
}

writer.writeLn("""
-keep public class ${mainClass.get()} {
public static void main(java.lang.String[]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
Expand Down Expand Up @@ -25,7 +26,9 @@ object Main {
}

val image = renderComposeScene(height = 10, width = 10) {
mainShape()
AnimatedVisibility(true) {
mainShape()
}
}
val encodedImage = image.encodeToData(EncodedImageFormat.PNG) ?: error("Could not encode image as png")
workingDir.resolve("main-image.actual.png").writeBytes(encodedImage.bytes)
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugins/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.parallel=true
kotlin.code.style=official

# Default version of Compose Libraries used by Gradle plugin
compose.version=1.4.1
compose.version=1.5.0-dev1114
# The latest version of Compose Compiler used by Gradle plugin. Used only in tests/CI.
compose.tests.compiler.version=1.5.0
# The latest version of Kotlin compatible with compose.tests.compiler.version. Used only in tests/CI.
Expand Down
11 changes: 11 additions & 0 deletions tutorials/Native_distributions_and_local_execution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,14 @@ compose.desktop {
}
}
```

ProGuard's optimization is disabled by default since Compose 1.5. To enable it, set the following property via Gradle DSL:
```
compose.desktop {
application {
buildTypes.release.proguard {
optimize.set(true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will users encounter androidx/compose/animation/core/AnimationSpecKt.estimateAnimationDurationMillis(FFFFF)J when they enable it?

If yes, we should add a rule to the default rules

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igordmn It is not possible to "turn it off" optimizations for a single class. The problem is not that the class is removed or renamed, but that it is rewritten in a way, which violates the JVM bytecode verification.
Also, there is no "estimateAnimationDurationMillis" in AnimationSpecKt. According to GitHub Search the function is only defined in SpringEstimation.kt. So PG is clearly doing something non-trivial with the bytecode, and it is doing it incorrectly.
The only option we have is to turn the optimizations off by default, report the bug to PG and wait for resolution.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Compose 1.4.1, Windows

benchmark

./gradlew run, OpenJDK 20

AnimatedVisibility: 26ms
LazyGrid: 101ms
VisualEffects: 33ms

./gradlew run, OpenJDK 18

AnimatedVisibility: 27ms
LazyGrid: 104ms
VisualEffects: 33ms

./gradlew runDistributable, OpenJDK 18:

AnimatedVisibility: 28ms
LazyGrid: 102ms
VisualEffects: 33ms

./gradlew runReleaseDistributable, OpenJDK 18:

AnimatedVisibility: 27ms
LazyGrid: 98ms
VisualEffects: 33ms

benchmark, startup + single frame, LazyGrid, median, 10-15 tries:
./gradlew run, OpenJDK 20

311ms

./gradlew run, OpenJDK 18

304ms

./gradlew runDistributable, OpenJDK 18:

306ms

./gradlew runReleaseDistributable, OpenJDK 18:

261ms

When we apply optimizations - no significant improvements when we render frames, but there are some improvement in the startup time.

So, it is preferable to keep optimizations working - this PR fixes this particular issue. Also I created a task for the future - run all Compose tests after ProGuard optimizations.

As for disabling optimizations by default - it is debatable, let's just keep them enabled until there will be a strong argument against it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's enable optmizations again but keep it in Gradle DSL

}
}
}
```