我正在使用 pdf_viewer_plugin 并且在 Android Studio 中我没有问题,但是当我构建 APK 并尝试从 APP 查看 PDF 时,APP 崩溃了?

Dim*_*tar 1 pdf android flutter

所以我使用 Android Studio 和 Flutter 来构建一个 APP,其中有一个 PDF,我pdf_viewer_plugin: ^1.0.0+2用来在 APP 中预览 PDF。当我使用电缆连接手机并运行 APP 时,打开屏幕并查看 PDF 没有任何问题,但是当我单击 Build->APK 并从创建的文件安装 APK 并转到同一屏幕时使用 PDF 查看器,整个应用程序崩溃。我可以看到屏幕打开了,甚至我放置的加载指示器也在旋转,但是当它想打开 PDF 时,它只会使整个 APP 崩溃。有人能帮我吗?

这是我的颤振码头:

[?] 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
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的一些代码:

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;
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

更新:我的主要问题是如何调试我创建的 APK 文件以查找发生了什么?

Wil*_*son 5

在您的android/app目录中创建一个名为 proguard-rules.pro 的文件。

将这行代码添加到其中:

 -keep class com.shockwave.** { *; }
Run Code Online (Sandbox Code Playgroud)

现在在您的应用程序级 build.gradle 文件中,将其添加到您的 buildType 中:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
Run Code Online (Sandbox Code Playgroud)

所以你的 buildType 文件夹看起来像这样:

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

然后运行 ​​flutter build apk -- buildTypeName

例子:

flutter build apk --release
Run Code Online (Sandbox Code Playgroud)

说明:Proguard 可能阻止您的应用程序使用 pdf 查看器库。这可能就是为什么您的应用程序在调试模式下运行,而不是在构建 apk 后运行的原因。

根据这里的插件源代码,包名是com.example.pdfviewerplugin,这就是我在proguard代码中添加com.example的原因。

尝试一下,看看它是否有效。