endigo / flutter_pdfview

PDF view for Flutter
https://pub.dev/packages/flutter_pdfview
MIT License
264 stars 219 forks source link

Android R8 issue #132

Closed wcewong closed 2 months ago

wcewong commented 3 years ago

This package works flawlessly on iOS! On Android, R8 (which is enabled by default in --release mode) is messing up this package.

Output of "flutter doctor"

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.2, on macOS 11.2.2 20D80 darwin-x64, locale en-MY)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.54.1)
[✓] Connected device (3 available)

• No issues found!

Excerpt from my app level build.gradle file:

buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }

In proguard-rules.pro placed in the same directory as build.gradle:

-keep class com.shockwave.**

WORKAROUND (PDF loads with no issues on Android):

Change the build.gradle file to reflect the following:

buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
            minifyEnabled false
            shrinkResources false
        }
    }

Effectively, I am turning off R8 to get this package to work.

Is it possible to get this package to work in conjunction with R8 turned on?

endigo commented 3 years ago

Hello, thanks for using my package.

I ported this package from AndroidPdfViewer, so you should look through native android library.

wcewong commented 3 years ago

Hi. I should be thanking you instead for saving me hours of development time :-)

I've looked at AndroidPdfViewer - it appears to be based on the PDFium engine. I couldn't find anything useful on their github page but I've made a bit of progress so I'd like to update this thread.

My build.gradle:

buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }

So R8 is now enabled!

In proguard-rules.pro:

-dontobfuscate
-keep class com.shockwave.**
-printconfiguration proguard.debug.txt

-dontobfuscate I'm turning off obfuscation for now. This seems to be the key to solving my issue with R8

-keep class com.shockwave.** This is recommended by the PDFium library. Somehow, something must've changed in the latest Flutter update as this no longer solves the issue

-printconfiguration proguard.debug.txt__ This is unnecessary, I am logging my R8 flags to a file purely for debugging purposes.

Summary: This library works with R8 (with obfuscation turned off). I'd be interested if anyone knows how to get this to work with obfuscation turned on. Hope this is helpful to someone facing the same issue I am. Cheers!

myselfuser1 commented 1 year ago

This will help https://www.youtube.com/watch?v=gAUVz0U7eyA