Avoid ClassVerificationErrors in OnBackPressedDispatcher
Although we won't actually call any of the T specific OnBackInvoked APIs
on Android versions prior to T, the class loader does not know that. So
it attempts to load all of the classes and since theose APIs are not
present we get a ClassVerificationError.
We should move any initialization of these classes to static functions
that are specific to API 33.
RelNote: "Initializing a `OnBackPressedDispatcher` will not longer cause
`ClassVerificationError`s when using SDK versions prior to 33."
Test: verified with integration app and running test on 32- devices
Bug: 241739353
Change-Id: Ic32e1fb18a4f17692d4b62ddf9d37bb5e67c2771
diff --git a/activity/activity/src/main/java/androidx/activity/OnBackPressedDispatcher.java b/activity/activity/src/main/java/androidx/activity/OnBackPressedDispatcher.java
index e306a9b..4645d84 100644
--- a/activity/activity/src/main/java/androidx/activity/OnBackPressedDispatcher.java
+++ b/activity/activity/src/main/java/androidx/activity/OnBackPressedDispatcher.java
@@ -127,12 +127,7 @@
updateBackInvokedCallbackState();
}
};
- mOnBackInvokedCallback = new OnBackInvokedCallback() {
- @Override
- public void onBackInvoked() {
- onBackPressed();
- }
- };
+ mOnBackInvokedCallback = Api33Impl.createOnBackInvokedCallback(this::onBackPressed);
}
}
@@ -331,18 +326,22 @@
@DoNotInline
static void registerOnBackInvokedCallback(
- OnBackInvokedDispatcher onBackInvokedDispatcher, int priority,
- OnBackInvokedCallback onBackInvokedCallback
+ Object dispatcher, int priority, Object callback
) {
+ OnBackInvokedDispatcher onBackInvokedDispatcher = (OnBackInvokedDispatcher) dispatcher;
+ OnBackInvokedCallback onBackInvokedCallback = (OnBackInvokedCallback) callback;
onBackInvokedDispatcher.registerOnBackInvokedCallback(priority, onBackInvokedCallback);
}
@DoNotInline
- static void unregisterOnBackInvokedCallback(
- OnBackInvokedDispatcher onBackInvokedDispatcher,
- OnBackInvokedCallback onBackInvokedCallback
- ) {
+ static void unregisterOnBackInvokedCallback(Object dispatcher, Object callback) {
+ OnBackInvokedDispatcher onBackInvokedDispatcher = (OnBackInvokedDispatcher) dispatcher;
+ OnBackInvokedCallback onBackInvokedCallback = (OnBackInvokedCallback) callback;
onBackInvokedDispatcher.unregisterOnBackInvokedCallback(onBackInvokedCallback);
}
+ @DoNotInline
+ static OnBackInvokedCallback createOnBackInvokedCallback(Runnable runnable) {
+ return runnable::run;
+ }
}
}