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

Minimize the bundle size using Proguard #16

Draft
wants to merge 6 commits into
base: 0.9.16
Choose a base branch
from

Conversation

EchoEllet
Copy link

Minimize the bundle size by using Proguard

Before: 12.5 MB
With Proguard: 4.83 MB (reduced by 59.30%)
With Proguard (obfuscation enabled): 2.86 MB (reduced by 75.91%)

The proguard task is configured to use the rules that are included in the JAR file in META-INF/proguard which will exist even if not using Proguard, as the required rules won't be included by default as there might be some downsides, one of them if a library update the rules we will have to update the library to get the updated rules, another way to address this is by fetching the rules from the repository and include it in a task, this also have downsides, some new rules for the updated library might not work with the current version

We also could manually update the rules and this will require checking if the rules have been updated when updating the libraries that have Proguard support, this solution is commonly used by most projects

I have not encountered any issues with the current solution and it's similar to R8

I have updated OkHttp from 4.10.0 to 4.12.0 to fix some warnings as the latest stable has default rules (in META-INF/proguard/okhttp3.pro) that will fix the encountered warnings, this change could be reverted and fixed either by including the latest rules manually (could be outdated after a while) or only the changes, or ignore the warnings.

The proguard task will read the rules from META-INF/proguard

The libraries in the project that support Proguard are:

  • OkHttp and Okio (used by OkHttp)
  • Kotlinx Coroutines
  • Kotlinx Serialization

There might be other dependencies that support Proguard such as Google Guava (https://github.com/google/guava/wiki/UsingProGuardWithGuava) though the rules are in a custom location (https://github.com/google/guava/tree/master/proguard) instead of resources/META-INF/proguard as a result the task won't be able to include it and will have to include it manually

It's not recommended to use -ignorewarnings, see this comment for more details
If the warning has an import that's from the JDK, try to include the required modules from the JDK, I included only the required modules so in the future if you use a new module, you will have to include it manually and will get warning that cause build failure, we could also include all modules

Proguard will be able to detect the most common usages of reflections and will cause a build failure with warnings that need to be solved (https://www.guardsquare.com/manual/home) to avoid runtime exceptions, however, some warnings should be suppressed as it will not cause any issues

I disabled obfuscation to make debugging easier, although if you want to save more, we might want to consider enabling it.

I haven't fully tested the CLI yet.

The GitHub workflow could upload the minimized JAR file in Github releases (with a note indicating it's not stable yet), or release this change as experimental to avoid disrupting the developers

This change might introduce additional maintenance needs, we could also drop the change.

@EchoEllet EchoEllet marked this pull request as draft June 17, 2024 19:53
@EchoEllet EchoEllet marked this pull request as draft June 17, 2024 19:53
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.

1 participant