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

Proguard rules improvements #1043

Merged
merged 9 commits into from
Jul 8, 2024
Merged

Proguard rules improvements #1043

merged 9 commits into from
Jul 8, 2024

Conversation

priettt
Copy link
Contributor

@priettt priettt commented Jul 3, 2024

Goal

Customers using R8 to obfuscate and shrink their app weren't able to shrink Embrace, because we had proguard rules instructing to keep all classes. This PR changes that.

In android-test-app, turning this on reduced Embrace size by 70%, comparing against master (from 800kb to 250kb, it's not like we were too heavy in the first place 😅)

  • Remove unnecessary rules:

    • java9.**
    • okhttp3.** and okio.**
    • Attributes: Exceptions, InnerClasses, Signature.
  • Removed keep rule for the whole embrace package

    • Anyone using r8 to either obfuscate or shrink their apk can now remove unused Embrace code.
  • Added rules to keep classes used by hosted SDKs, native code, okhttp, swazzled and serialized classes.

Testing

Copy link
Contributor

github-actions bot commented Jul 3, 2024

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

Copy link

codecov bot commented Jul 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.75%. Comparing base (e541f8b) to head (41c2c63).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1043   +/-   ##
=======================================
  Coverage   79.74%   79.75%           
=======================================
  Files         416      416           
  Lines       10775    10775           
  Branches     1763     1763           
=======================================
+ Hits         8593     8594    +1     
  Misses       1399     1399           
+ Partials      783      782    -1     
Files Coverage Δ
...in/java/io/embrace/android/embracesdk/EventType.kt 100.00% <ø> (ø)
...roid/embracesdk/capture/metadata/AppEnvironment.kt 100.00% <ø> (ø)
...id/embracesdk/internal/payload/EnvelopeResource.kt 100.00% <ø> (ø)
...mbrace/android/embracesdk/internal/payload/Span.kt 100.00% <ø> (ø)
...mbrace/android/embracesdk/payload/TapBreadcrumb.kt 18.75% <ø> (ø)

... and 1 file with indirect coverage changes

@priettt priettt force-pushed the proguard-improvements branch from 570195f to ea42a75 Compare July 4, 2024 18:25
@priettt priettt marked this pull request as ready for review July 4, 2024 18:26
@priettt priettt requested a review from a team as a code owner July 4, 2024 18:26
Copy link
Collaborator

@bidetofevil bidetofevil left a comment

Choose a reason for hiding this comment

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

Nice! It'd be cool if we can manage this using annotations rather than a proguard file, but this is great!

Copy link
Contributor

@fractalwrench fractalwrench left a comment

Choose a reason for hiding this comment

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

LGTM pending comments

## Keep classes used from native code
-keep class io.embrace.android.embracesdk.payload.NativeThreadAnrSample { *; }
-keep class io.embrace.android.embracesdk.payload.NativeThreadAnrStackframe { *; }
-keep class io.embrace.android.embracesdk.anr.sigquit.SigquitDataSource { *; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we also need to keep NativeThreadSamplerNdkDelegate, or will that work ok with R8's obfuscation? It's probably worth global-searching for any external fun in the project & checking they work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I verified that they work, but I'm not sure if it's because they're referenced by kept classes or if it's because obfuscation doesn't affect JNI calls. I'll check just in case because I haven't tested every external fun.

The classes referenced from the native code must be kept because they're searched for by name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The classes are always being kept.

Not because we are actually adding any rules for them, but I think it's because they are being initialized in modules that override kotlin.jvm.functions.Function0.invoke(), and those are also called in different places in ComponentActivity, and every activity of any app is added because of some aapt_rules.

It's super weird, and I don't want to lose much time on this, so I'll just add rules for the classes that contain external fun methods just in case 😅

@@ -1,20 +1,33 @@
-keepattributes Exceptions, InnerClasses, Signature, LineNumberTable, SourceFile
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the effect of removing the rule to keep these attributes? Instinctively I'd have thought we would want to retain information about inner classes/exception metadata

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We only need LineNumberTable and SourceFile.

  • Exceptions keeps info on what exceptions a method might throw (the throws part of a function signature). We don't use it.
  • Signature keeps generics type information. If not present, instead of it'll use Object. We don't use it.
  • InnerClasses keeps the relationship between classes. If it's not present, inner classes might be renamed and their relationship with their outer class might be lost. This doesn't affects us per se, but some stacktraces might change. Keeping it wouldn't be a problem, but I don't think it's necessary.

@priettt priettt force-pushed the proguard-improvements branch from ba72d93 to ecb4666 Compare July 5, 2024 18:58
@priettt priettt force-pushed the proguard-improvements branch from ecb4666 to 41c2c63 Compare July 8, 2024 18:56
@priettt priettt merged commit 56bc5d7 into master Jul 8, 2024
5 checks passed
@priettt priettt deleted the proguard-improvements branch July 8, 2024 19:28
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.

3 participants