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?

岁酱吖の 提交于 2020-06-28 05:57:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/61519557/i-am-using-pdf-viewer-plugin-and-in-android-studio-i-have-no-problem-but-when-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!