From 177d97d8ea962bdd4dad8fcf0efb04a307f25000 Mon Sep 17 00:00:00 2001 From: Kun Wang Date: Mon, 10 Jul 2023 05:53:21 -0700 Subject: [PATCH] For targeting SDK 34 - Added RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED flag support in DevSupportManagerBase (#38256) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38256 Add RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED flag support to DevSupportManagerBase for Android 14 change. See https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported for details. Without this fix, app crashes during launch because of : ```SecurityException: {package name here}: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts``` Changelog: [Targeting SDK 34] Added RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED flag support in DevSupportManagerBase Reviewed By: javache Differential Revision: D47313501 fbshipit-source-id: 12e8299559d08b4ff87b4bdabb0a29d27763c698 --- .../devsupport/DevSupportManagerBase.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java index 0fefa46c5df55a..d1f2b994f3d8b0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -19,6 +19,7 @@ import android.content.pm.PackageManager; import android.graphics.Typeface; import android.hardware.SensorManager; +import android.os.Build; import android.util.Pair; import android.view.Gravity; import android.view.View; @@ -1022,7 +1023,7 @@ private void reload() { if (!mIsReceiverRegistered) { IntentFilter filter = new IntentFilter(); filter.addAction(getReloadAppAction(mApplicationContext)); - mApplicationContext.registerReceiver(mReloadAppBroadcastReceiver, filter); + compatRegisterReceiver(mApplicationContext, mReloadAppBroadcastReceiver, filter, true); mIsReceiverRegistered = true; } @@ -1120,4 +1121,21 @@ public void setPackagerLocationCustomizer( return mSurfaceDelegateFactory.createSurfaceDelegate(moduleName); } + + /** + * Starting with Android 14, apps and services that target Android 14 and use context-registered + * receivers are required to specify a flag to indicate whether or not the receiver should be + * exported to all other apps on the device: either RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED + * + *

https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported + */ + private void compatRegisterReceiver( + Context context, BroadcastReceiver receiver, IntentFilter filter, boolean exported) { + if (Build.VERSION.SDK_INT >= 34 && context.getApplicationInfo().targetSdkVersion >= 34) { + context.registerReceiver( + receiver, filter, exported ? Context.RECEIVER_EXPORTED : Context.RECEIVER_NOT_EXPORTED); + } else { + context.registerReceiver(receiver, filter); + } + } }