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

Conversation

AlexeyTsvetkov
Copy link
Collaborator

Resolves #3387

@@ -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.

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.

@@ -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

@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

@mipastgt
Copy link

After switching PG optimization off in the PG config file I am now able to create a release build again. Here are a few numbers which may be of interest here:

101825266 bytes AIP Browser DE-1.0.5-b19-osx-x86_64-pg-disabled.dmg
 56695114 bytes AIP Browser DE-1.0.5-b19-osx-x86_64-dontoptimize.dmg
 55527595 bytes AIP Browser DE-1.0.5-b19-osx-x86_64-optimize.dmg

The numbers show the size in bytes of the installers for my current project compiled with Kotlin 1.9.0 and Compose 1.5.0-dev1114. The size difference between enabled/disabled PG is quite significant but the difference between optimized/non-optimized is basically irrelevant (for me) and I would not trade that against an unreliable build. From the performance side I can't see any difference (tested with the older version where optimized release builds still worked).

It would also be nice to upgrade the version of PG to the latest 7.3.2 which finally supports JDK 20 and contains another bug fix which might be relevant here. (Merge classes only when -optimizeaggressively is set.)
See: https://github.com/Guardsquare/proguard/releases/tag/v7.3.2

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.

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

@AlexeyTsvetkov
Copy link
Collaborator Author

This PR is superseded by #3408

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java.lang.VerifyError when running release build
4 participants