I am using pdf_viewer_plugin and in Android Studio I have no problem, but when I build APK and try to view the PDF from the APP, the APP crashes?

2,553

Create a file called proguard-rules.pro in your android/app directory.

Add this line of code to it:

 -keep class com.shockwave.** { *; }

Now in your app level build.gradle file, add this to your buildType:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

So that your buildType folder looks something like this:

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }

Then run flutter build apk --buildTypeName

Example:

flutter build apk --release

EXPLANATION: Proguard is probably blocking your app from using the pdf viewer library. That's probably why your app runs in debug mode, but not after building the apk.

According to the plugin source code here, the package name is com.example.pdfviewerplugin, which is why I added com.example to the proguard code.

Try it, and see if it works.

Share:
2,553
Dimitar
Author by

Dimitar

Updated on December 20, 2022

Comments

  • Dimitar
    Dimitar over 1 year

    So I am using Android Studio and Flutter to build an APP in which there is a PDF and I am using pdf_viewer_plugin: ^1.0.0+2 to preview the PDF inside the APP. When I connect my phone using cable and run the APP I have no issues what so ever to open the screen and view the PDF, but when I click on Build->APK and install the APK from the file created and go to the same screen with the PDF viewer the whole APP crashes. I can see the screen opens and even the loading indicator I have put it is spinning, but when it suppose to open the PDF it simply crash the whole APP. Can someone help me?

    This is my flutter docktor:

    [✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Linux, locale bg_BG.UTF-8)
        • Flutter version 1.12.13+hotfix.9 at /home/dimitar/flutter
        • Framework revision f139b11009 (преди 4 седмици), 2020-03-30 13:57:30 -0700
        • Engine revision af51afceb8
        • Dart version 2.7.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
        • Android SDK at /home/dimitar/Android/Sdk
        • Android NDK location not configured (optional; useful for native profiling support)
        • Platform android-29, build-tools 29.0.2
        • Java binary at: /usr/local/android-studio/jre/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
        • All Android licenses accepted.
    
    [✓] Android Studio (version 3.6)
        • Android Studio at /usr/local/android-studio
        • Flutter plugin version 45.1.1
        • Dart plugin version 192.7761
        • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    
    [✓] Connected device (1 available)
        • Mi A2 • 2928015 • android-arm64 • Android 10 (API 29)
    
    • No issues found!
    Process finished with exit code 0
    

    This is some of the code I am using:

    Future<String> get _localPath async {
      final directory = await getApplicationDocumentsDirectory();
    
      return directory.path;
    }
    
    Future<File> get _localFile async {
      final path = await _localPath;
      return File('$path/teste.pdf');
    }
    
    Future<File> writeCounter(Uint8List stream) async {
      final file = await _localFile;
    
      // Write the file
      return file.writeAsBytes(stream);
    }
    
    Future<bool> existsFile() async {
      final file = await _localFile;
      return file.exists();
    }
    
    Future<Uint8List> fetchPost() async {
      final response =
          await http.get('https://expoforest.com.br/wp-content/uploads/2017/05/exemplo.pdf');
      final responseJson = response.bodyBytes;
    
      return responseJson;
    }
    
    void loadPdf() async {
      await writeCounter(await fetchPost());
      await existsFile();
      path = (await _localFile).path;
    
      if (!mounted) {
        return;
      } else {
        setState(() {
          _isLoading = false;
        });
      }
    }
    

    UPDATE: My main question is how can I debug the APK file that I created to find what is going on?

    • Wilson Wilson
      Wilson Wilson about 4 years
      Proguard may be blocking the pdf plugin
  • Wilson Wilson
    Wilson Wilson about 4 years
    What buildType are you using?
  • Dimitar
    Dimitar about 4 years
    release, as you have suggested
  • Dimitar
    Dimitar about 4 years
    W/Gralloc3(12469): allocator 3.x is not supported D/com.shockwave.pdfium.PdfiumCore(12469): Starting PdfiumAndroid 1.9.0 W/lease.myreleas(12469): Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (greylist, reflection, allowed) D/jniPdfium(12469): Init FPDF library is this information helpful by any means?
  • Wilson Wilson
    Wilson Wilson about 4 years
    Probably. add -keep class com.shockwave.** { *; } to your proguard
  • Dimitar
    Dimitar about 4 years
    so add a second line?
  • Dimitar
    Dimitar about 4 years
    it worked! Thank you very much! I have to idea how to know that if you havent talk me. I am new to flutter.
  • Wilson Wilson
    Wilson Wilson about 4 years
    Funny enough, I'm new to flutter too. I just learned about proguard yesterday :)
  • Dimitar
    Dimitar about 4 years
    Good timing. I wanted to ask you where do you learn flutter from? What is your source?
  • Wilson Wilson
    Wilson Wilson about 4 years
  • benten
    benten about 2 years
    best answer ..thanks man