razorpay / razorpay-flutter

Razorpay Flutter Plugin
MIT License
106 stars 147 forks source link

Issue with Missing Classes During R8 Minification (ProGuard Annotations) #android build #415

Open shamnad-sherief opened 4 days ago

shamnad-sherief commented 4 days ago

After switching to flutter 3.24, I am experiencing an issue with the Razorpay SDK while building my project with R8 minification enabled. The build fails with the following error related to missing ProGuard annotations:

ERROR: Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /<project_location>/build/app/outputs/mapping/release/missing_rules.txt.
ERROR: R8: Missing class proguard.annotation.Keep (referenced from: com.razorpay.AnalyticsEvent and 8 other contexts)
Missing class proguard.annotation.KeepClassMembers (referenced from: com.razorpay.AnalyticsEvent and 8 other contexts)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:minifyReleaseWithR8'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.R8Task$R8Runnable
   > Compilation failed to complete

It seems that the com.razorpay.AnalyticsEvent and other classes are referencing proguard.annotation.Keep and proguard.annotation.KeepClassMembers, but these classes are not available during the R8 minification process. I have already tried adding ProGuard rules to retain these classes, but the issue persists.

Steps to Reproduce:

  1. Integrate Razorpay SDK into an Android project.
  2. Enable R8 minification in the release build configuration.
  3. Attempt to build the project.

Environment Details:

Additional Information:

Workaround:

To work around the issue, I added additional keep rules that were generated in the following file: //build/app/outputs/mapping/release/missing_rules.txt

Here are the rules I added to proguard-rules.pro:


-dontwarn androidx.window.extensions.WindowExtensions
-dontwarn androidx.window.extensions.WindowExtensionsProvider
-dontwarn androidx.window.extensions.area.ExtensionWindowAreaPresentation
-dontwarn androidx.window.extensions.layout.DisplayFeature
-dontwarn androidx.window.extensions.layout.FoldingFeature
-dontwarn androidx.window.extensions.layout.WindowLayoutComponent
-dontwarn androidx.window.extensions.layout.WindowLayoutInfo
-dontwarn androidx.window.sidecar.SidecarDeviceState
-dontwarn androidx.window.sidecar.SidecarDisplayFeature
-dontwarn androidx.window.sidecar.SidecarInterface$SidecarCallback
-dontwarn androidx.window.sidecar.SidecarInterface
-dontwarn androidx.window.sidecar.SidecarProvider
-dontwarn androidx.window.sidecar.SidecarWindowLayoutInfo
-dontwarn proguard.annotation.Keep
-dontwarn proguard.annotation.KeepClassMembers

Result:

While this workaround allows me to build the project without minification, the app size has significantly increased, which is not ideal for production.

vivekshindhe commented 3 days ago

@shamnad-sherief Have you added the proguard rules for the Razorpay SDK? You can find these here.

shamnad-sherief commented 3 days ago

@shamnad-sherief Have you added the proguard rules for the Razorpay SDK? You can find these here.

I followed the ProGuard rules provided in the Razorpay documentation, but unfortunately, I didn't see any reduction in the app size. The APK size remains at 82MB, whereas it was only 30MB approx before. It seems like the rules suggested by Razorpay (like -dontwarn com.razorpay. and -keep class com.razorpay. {*;}) are functionally similar to the ones I was already using, which came from the missing_rules.txt file generated by R8.

The provided rules ensure that the necessary Razorpay classes and methods are retained, and suppress any warnings, but they don't seem to address the increase in size. Could it be that something else in the SDK or project is contributing to the large APK size? Any further insights or recommendations would be greatly appreciated.

sgjesse commented 2 days ago

Using a rule like -keep class com.razorpay.** {*;} is not a good solution but more a workaround. The Razorpay team should spend some time to find more precise rules instead of just keeping everything.

The -dontwarn rules are not relevant to size but only to suppress warnings for using APIs which are not present in any dependencies.

gujjar1988 commented 1 day ago

ERROR: Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /development/project/build/app/outputs/mapping/release/missing_rules.txt. ERROR: R8: Missing class proguard.annotation.Keep (referenced from: com.razorpay.AnalyticsEvent and 8 other contexts) Missing class proguard.annotation.KeepClassMembers (referenced from: com.razorpay.AnalyticsEvent and 8 other contexts)

FAILURE: Build failed with an exception.

BUILD FAILED in 9s Running Gradle task 'assembleRelease'... 10.4s Gradle task assembleRelease failed with exit code 1

I also have same issue

gujjar1988 commented 17 hours ago

Create a file in app folder proguard-rules.pro in this file write some code below-

Flutter wrapper

-keep class io.flutter.app. { *; } -keep class io.flutter.plugin.* { ; } -keep class io.flutter.util. { *; } -keep class io.flutter.view. { *; } -keep class io.flutter.* { ; } -keep class io.flutter.plugins. { *; } -keep class com.google.firebase.* { ; } -dontwarn io.flutter.embedding.** -ignorewarnings

it's working for me. Thanks