You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ma...@apache.org on 2026/03/13 15:54:08 UTC

(cordova-android) 01/01: fix(android): avoid OnBackInvokedCallback class-load crashes on older runtimes in CoreAndroid

This is an automated email from the ASF dual-hosted git repository.

manuelbeck pushed a commit to branch pr-fix-OnBackInvokedCallback-class-load-crashes-older-android-versions
in repository https://gitbox.apache.org/repos/asf/cordova-android.git

commit 93d10a9149c59a32a0ac4d0f8608f4c37c493ee1
Author: Manuel Beck <ma...@outlook.de>
AuthorDate: Fri Mar 13 16:53:47 2026 +0100

    fix(android): avoid OnBackInvokedCallback class-load crashes on older runtimes in CoreAndroid
    
    - Fixes error message: `java.lang.Class<org.apache.cordova.CoreAndroid$$ExternalSyntheticLambda0>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/window/OnBackInvokedCallback;` on older Android versions pre API Level 33 (Android 13)
    - The use of `OnBackInvokedCallback` and `OnBackInvokedDispatcher` was introduced with PR https://github.com/apache/cordova-android/pull/1831/changes
    - This change fixes startup log spam and plugin instantiation failures on older Android runtimes where `android.window.OnBackInvokedCallback` is unavailable, because it is introduced since API Level 33 (Android 13) and not available before.
    - `CoreAndroid.java` referenced `OnBackInvokedCallback` directly (including lambda generation), which can trigger class resolution on runtimes that do not contain that API.
    - Fix: replaced direct API typing with reflection + dynamic proxy registration/unregistration of predictive-back callback.
    - Behavior: predictive-back callback is still registered when supported; on unsupported runtimes, registration is skipped safely and app startup continues without `NoClassDefFoundError` noise.
    - Generated-By: GPT-5.3-Codex, GitHub Copilot Chat
---
 framework/src/org/apache/cordova/CoreAndroid.java | 62 ++++++++++++++++++++---
 1 file changed, 55 insertions(+), 7 deletions(-)

diff --git a/framework/src/org/apache/cordova/CoreAndroid.java b/framework/src/org/apache/cordova/CoreAndroid.java
index 2550b9b2..61af2017 100755
--- a/framework/src/org/apache/cordova/CoreAndroid.java
+++ b/framework/src/org/apache/cordova/CoreAndroid.java
@@ -32,9 +32,11 @@ import android.os.Build;
 import android.content.IntentFilter;
 import android.telephony.TelephonyManager;
 import android.view.KeyEvent;
-import android.window.OnBackInvokedCallback;
-import android.window.OnBackInvokedDispatcher;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.util.HashMap;
 
 /**
@@ -48,7 +50,7 @@ public class CoreAndroid extends CordovaPlugin {
     private CallbackContext messageChannel;
     private PluginResult pendingResume;
     private PluginResult pendingPause;
-    private OnBackInvokedCallback backCallback;
+    private Object backCallback;
     private final Object messageChannelLock = new Object();
     private final Object backButtonHandlerLock = new Object();
 
@@ -262,15 +264,13 @@ public class CoreAndroid extends CordovaPlugin {
                         // it's own logic, UNLESS if there is an OnBackInvokedCallback registered on the dispatcher.
                         // The key dispatch events will still fire, which still handles propagating back button events to the webview.
                         // See https://developer.android.com/about/versions/16/behavior-changes-16#predictive-back for more info on the caveat.
-                        backCallback = () -> {};
-                        this.cordova.getActivity().getOnBackInvokedDispatcher().registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT, backCallback);
+                        registerOnBackInvokedCallback();
                     }
                 }
             } else {
                 synchronized (backButtonHandlerLock) {
                     if (backCallback != null) {
-                        this.cordova.getActivity().getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(backCallback);
-                        backCallback = null;
+                        unregisterOnBackInvokedCallback();
                     }
                 }
             }
@@ -279,6 +279,54 @@ public class CoreAndroid extends CordovaPlugin {
         webView.setButtonPlumbedToJs(KeyEvent.KEYCODE_BACK, override);
     }
 
+    private void registerOnBackInvokedCallback() {
+        try {
+            Class<?> callbackClass = Class.forName("android.window.OnBackInvokedCallback");
+            Class<?> dispatcherClass = Class.forName("android.window.OnBackInvokedDispatcher");
+
+            Method getDispatcherMethod = this.cordova.getActivity().getClass().getMethod("getOnBackInvokedDispatcher");
+            Object dispatcher = getDispatcherMethod.invoke(this.cordova.getActivity());
+
+            Field priorityField = dispatcherClass.getField("PRIORITY_DEFAULT");
+            int priority = priorityField.getInt(null);
+
+            InvocationHandler noOpHandler = new InvocationHandler() {
+                @Override
+                public Object invoke(Object proxy, Method method, Object[] args) {
+                    return null;
+                }
+            };
+
+            Object callbackProxy = Proxy.newProxyInstance(
+                    callbackClass.getClassLoader(),
+                    new Class<?>[] { callbackClass },
+                    noOpHandler
+            );
+
+            Method registerMethod = dispatcherClass.getMethod("registerOnBackInvokedCallback", int.class, callbackClass);
+            registerMethod.invoke(dispatcher, priority, callbackProxy);
+            backCallback = callbackProxy;
+        } catch (ReflectiveOperationException | RuntimeException ex) {
+            LOG.d(TAG, "Unable to register OnBackInvokedCallback", ex);
+        }
+    }
+
+    private void unregisterOnBackInvokedCallback() {
+        try {
+            Class<?> callbackClass = Class.forName("android.window.OnBackInvokedCallback");
+            Class<?> dispatcherClass = Class.forName("android.window.OnBackInvokedDispatcher");
+
+            Method getDispatcherMethod = this.cordova.getActivity().getClass().getMethod("getOnBackInvokedDispatcher");
+            Object dispatcher = getDispatcherMethod.invoke(this.cordova.getActivity());
+
+            Method unregisterMethod = dispatcherClass.getMethod("unregisterOnBackInvokedCallback", callbackClass);
+            unregisterMethod.invoke(dispatcher, backCallback);
+            backCallback = null;
+        } catch (ReflectiveOperationException | RuntimeException ex) {
+            LOG.d(TAG, "Unable to unregister OnBackInvokedCallback", ex);
+        }
+    }
+
     /**
      * Override the default behavior of the Android volume buttons.
      * If overridden, when the volume button is pressed, the "volume[up|down]button" JavaScript event will be fired.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org