Capacitor Hard ware back button closing the app in release build

Issue

I am handling the back button by adding a listener in the _app.tsx of my react project like so

useEffect(() => {
        CapacitorApp.addListener("backButton", () => {
            if (router.pathname === "/") {
                CapacitorApp.exitApp();
            } else {
                router.back();
            }
        });

        return () => {
            CapacitorApp.removeAllListeners();
        };
    }, [router]);

and it is working as expected in the dev build without any issues.

But in the release build the back button is closing the app

My capacitor package versions

"@capacitor/android": "^3.5.1",
"@capacitor/app": "^1.1.1",
"@capacitor/cli": "^3.5.1",
"@capacitor/core": "^3.5.1",

I have tried removing the whole android folder and readding it

and also issue only happening with aab(android app bundle) if I build a normal apk the back button logic is working perfectly and not exiting the app.

Solution

It is a known issue you just need to copy the following proguard rules into your app’s proguard rules

# Rules for Capacitor v3 plugins and annotations
 -keep @com.getcapacitor.annotation.CapacitorPlugin public class * {
     @com.getcapacitor.annotation.PermissionCallback <methods>;
     @com.getcapacitor.annotation.ActivityCallback <methods>;
     @com.getcapacitor.annotation.Permission <methods>;
     @com.getcapacitor.PluginMethod public <methods>;
 }

# Rules for Capacitor v2 plugins and annotations
# These are deprecated but can still be used with Capacitor for now
-keep @com.getcapacitor.NativePlugin public class * {
  @com.getcapacitor.PluginMethod public <methods>;
}

# Rules for Cordova plugins
-keep public class * extends org.apache.cordova.* {
  public <methods>;
  public <fields>;
}

Answered By – Nikhil Reddy Avuthu

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.