Flutter发布的apk无法在PayHere中进行支付

Raj*_*nga 5 payment-gateway flutter

我的 flutter 应用程序无法在发布 apk 中的 PayHere 支付网关中继续付款。在调试模式下并在发布模式下运行应用程序工作正常。但只有在构建和运行release apk时才会出现错误。

Raj*_*nga 2

根本原因

\n

该问题是由 Flutter\xe2\x80\x99s R8 代码收缩引起的,在发布版本中默认打开该代码收缩,如此处所述。从 Android Gradle 版本 3.4.0 开始,R8 不再使用 Proguard,如此处所述。然而,R8 仍然使用确切格式的混淆器规则文件,以排除仅在明确提供的情况下被混淆/删除的类。否则,未使用的代码将从最终产品中删除。

\n

据了解,Flutter 和 Dart 内部用于链接 PayHere Android SDK 和 PayHere Flutter Dart 组件的 MethodChannel 受到了此代码混淆过程的影响。

\n

解决方案

\n

有两种解决方案可以解决该问题。如果开发人员扩展现有的代码库,第一个可以提供更大的灵活性。第二种解决方案是快速 \xe2\x80\x9chotfix\xe2\x80\x9d,它禁用代码混淆以方便应用。

\n

注意:在执行下文提到的解决方案之前,请保留现有代码库的备份。即使您启用了版本控制,也建议执行此步骤,因为 .gitignore 定义可能不会跟踪携带敏感信息的文件的更改。

\n

1.使用Android Proguard规则的解决方案

\n

如果您愿意手动创建 Pro-guard 规则文件或者您已经配置了 Pro-guard 规则,则首选此解决方案。下面提到了所需的步骤。

\n
    \n
  1. 在您喜欢的 IDE 中打开您的 Flutter 项目。

    \n
  2. \n
  3. 在 \xe2\x80\x9c/android/app\xe2\x80\x9d 文件夹中创建一个名为 \xe2\x80\x9cproguard-rules.pro\xe2\x80\x9d 的新文件。

    \n

    截屏

    \n
  4. \n
  5. 在上面步骤 2 中创建的 \xe2\x80\x9cproguard-rules.pro\xe2\x80\x9d 文件中添加以下代码行。

    \n
    #Flutter Wrapper\n-keep class io.flutter.app.** { *; } \n-keep class io.flutter.plugin.** { *; } \n-keep class io.flutter.util.** { *; } \n-keep class io.flutter.view.** { *; } \n-keep class io.flutter.** { *; }\n-keep class io.flutter.plugins.** { *; } \n\n#the required rule\n-keep class lk.payhere.** { *; }\n
    Run Code Online (Sandbox Code Playgroud)\n
  6. \n
\n

上面的代码排除了所有默认的 Flutter 库和 PayHere Android SDK 类。确保没有重叠的规则。如果您已有 \xe2\x80\x9cproguard-rules.pro\xe2\x80\x9d 文件,则只需添加最后一行。

\n
    \n
  1. 打开位于 \xe2\x80\x9c/android/app\xe2\x80\x9d 文件夹中的 \xe2\x80\x9cbuild.gradle\xe2\x80\x9d 文件。
  2. \n
  3. Find the \xe2\x80\x9cbuildTypes\xe2\x80\x9d block inside the top-level \xe2\x80\x9candroid\xe2\x80\x9d block. Note that in the context of this issue, developers are building the Flutter App for release mode and should already have the \xe2\x80\x9cbuildTypes\xe2\x80\x9d block written.
  4. \n
  5. Instruct the Android Build system to keep code minification/obfuscation turned on, and to use your Proguard rules defined in Step 3 using the following highlighted code.
  6. \n
\n

Note that if you have already set up Proguard previously, you might be able to skip this step.

\n
android {\n  compileSdkVersion 29\n   sourceSets {\n     main.java.srcDirs += \'src/main/kotlin\'\n  }\n   lintOptions {\n     disable \'InvalidPackage\'\n  }\n   defaultConfig {\n     // Android configurations are usually here\n  }\n   // Signing Configurations are usually here\n   buildTypes {\n     release {\n       signingConfig signingConfigs.debug\n\n       // add these code lines\n       minifyEnabled true\n       useProguard true\n       proguardFiles getDefaultProguardFile(\'proguard-android.txt\'), \'proguard-rules.pro\'\n    } \n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. Perform a Flutter clean task and build your Release APK. Your issue should be resolved now.
  2. \n
\n

2. Solution by disabling modification

\n

If you do not require code obfuscation or minification in your Release APK, you can simply disable this feature altogether for the Android code. The required steps are mentioned below.

\n
    \n
  1. Open up your Flutter Project in your preferred IDE

    \n
  2. \n
  3. Open the \xe2\x80\x9cbuild.gradle\xe2\x80\x9d file in the \xe2\x80\x9c/android/app\xe2\x80\x9d folder.

    \n
  4. \n
  5. Find the \xe2\x80\x9candroid\xe2\x80\x9d block, within it locate the \xe2\x80\x9cbuildTypes\xe2\x80\x9d and \xe2\x80\x9crelease\xe2\x80\x9d blocks.

    \n
  6. \n
  7. Add the following mentioned code to disable code modification.

    \n
    android {\n    compileSdkVersion 29\n    sourceSets {\n        main.java.srcDirs += \'src/main/kotlin\'\n    }\n\n    lintOptions {\n        disable \'InvalidPackage\'\n    }\n\n    defaultConfig {\n        // Android configurations are usually here\n    }\n    // Signing Configurations are usually here\n    buildTypes {\n        release {\n            signingConfig signingConfigs.debug\n\n            // add these code lines\n            minifyEnabled false\n            shrinkResources false\n        } \n    }\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  8. \n
  9. Perform a Flutter clean task and build your Release APK. Your issue should be resolved now.

    \n
  10. \n
\n

Happy coding :D

\n