Flutter

Payhere Flutter Android Release Build Issue fixed – 2021-December

Problem Statement Flutter release apk can’t proceed payment in PayHere


Customers are unable to continue with payment after clicking a payment method. A message is
returned to the Flutter code stating the following. The issue only affects Release Android builds.

 PHResponse{status=-1, message=’Error Occured’, data=null}

Problem Environment and Parameters
Platform: Android
Build Mode: Release
Flutter SDK Versions: Unknown, reported once for “1.22.1”
PayHere Merchant Account type: Live
Registered App package name: Yes
PayHere Flutter SDK Version: 1.0.1
PayHere Android SDK Version: ‘lk.payhere.sdk:android-payment-sdk:2.0.30’
PayHere iOS SDK Version: 2.1.1
Valid Payment Object: Yes (confirmed)

Root Cause
The issue is caused by Flutter’s R8 code shrinking which is turned on by default for Release
builds as stated here. Since Android Gradle version 3.4.0, R8 does not use Proguard under the
h

The issue is caused by Flutter’s R8 code shrinking which is turned on by default for Release builds as stated here. Since Android Gradle version 3.4.0, R8 does not use Proguard under the hood as mentioned here. However, R8 still uses the proguard rule files in the exact format, to exclude classes from being obfuscated/removed only if explicitly provided. Otherwise, unused code will be stripped out of the final product.

It is understood that the MethodChannels used internally by Flutter and Dart to link between the PayHere Android SDK and PayHere Flutter Dart components are affected by this code obfuscation process.

Solution
There are two solutions to resolving the issue. The first offers more flexibility down the line, should
developers extend the existing codebase. The second solution is a quick “hotfix”, which disables code
obfuscation in favor of ease of applicability.


NOTE: Please maintain a backup of your existing codebase before carrying out solutions
mentioned hereafter. This step is recommended even if you have version control enabled, since
.gitignore definitions might not track changes to files carrying sensitive information.
Please turn overleaf for the proposed solutions.

This solution is preferred if you are comfortable creating Pro-guard rule files manually or you have
already configured Pro-guard rules. The steps required are mentioned below.

  1. Open your Flutter Project in your preferred IDE.
  2. Create a new file named, “proguard-rules.pro” in the “/android/app” folder.
Payhere Flutter Android Release Build Issue
Payhere Flutter Android Release Build Issue

Add the following lines of code in the “proguard-rules.pro” file created in Step 2 above.

#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
-keep class lk.payhere.** { *; }

The above code excludes all default Flutter libraries and the PayHere Android SDK classes. Make sure
you do not have overlapping rules. If you already had an existing “proguard-rules.pro” file, only add the
last line (the line stated in a blue font).

  1. Open the “build.gradle” file located in the “/android/app” folder.
  2. Find the “buildTypes” block inside the top-level “android” block. Note that in the context of this issue,
    developers are building the Flutter App for release mode and should already have the “buildTypes” block
    written.
  1. 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.
    Note that if you have already set up Proguard previously, you might be able to skip this step.
android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // Android configurations are usually here
    }

    // Signing Configurations are usually here

    buildTypes {
        release {

        signingConfig signingConfigs.debug
        
        // Added to fix PayHere Flutter SDK Issue PHMSF001
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        // END

        }
    }
}

7. Perform a Flutter clean task and build your Release APK. Your issue should be resolved now.

2. Solution by disabling modification

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.

  1. Open up your Flutter Project in your preferred IDE
  2. Open the “build.gradle” file in the “/android/app” folder.
  3. Find the “android” block, within it locate the “buildTypes” and “release” blocks.
  4. Add the following mentioned code to disable code modification.
android {
    compileSdkVersion 29
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // Android configurations are usually here
    }
    // Signing Configurations are usually here
    buildTypes {
        release {
            signingConfig signingConfigs.debug

            // add these code lines
            minifyEnabled false
            shrinkResources false
        } 
    }
}

5. Perform a Flutter clean task and build your Release APK. Your issue should be resolved now.

Back to top button