maui: Adding a Connectivity.ConnectivityChanged handler throw an exception on Android 14
Description
When adding a Connectivity.ConnectivityChanged handler, the following exception is thrown on an Android 14 device, when the targetSdkVersion is 34 (the default with new Maui app) an when run on Android 14:
Exception has occurred: Java.Lang.SecurityException
com.companyname.dotnetmaui: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualObjectMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:20416
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:876
at Android.Content.ContextWrapper.RegisterReceiver(BroadcastReceiver receiver, IntentFilter filter) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Content.ContextWrapper.cs:2605
at Microsoft.Maui.Networking.ConnectivityImplementation.StartListeners() in D:\a\_work\1\s\src\Essentials\src\Connectivity\Connectivity.android.cs:48
at Microsoft.Maui.Networking.ConnectivityImplementation.add_ConnectivityChanged(EventHandler`1 value) in D:\a\_work\1\s\src\Essentials\src\Connectivity\Connectivity.shared.cs:91
at Microsoft.Maui.Networking.Connectivity.add_ConnectivityChanged(EventHandler`1 value) in D:\a\_work\1\s\src\Essentials\src\Connectivity\Connectivity.shared.cs:58
at DotnetMaui.App..ctor() in /Users/daniel/Development/Samples/DotnetMaui/App.xaml.cs:9
Steps to Reproduce
- Create a new MAUI app with botnet new maui
- In the App constructor in App.xaml.cs, add : Connectivity.ConnectivityChanged += (sender, e) => { };
- Start the app on an Android 14 emulator or device.
Link to public reproduction project repository
No response
Version with bug
8.0.0-rc.1.9171
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 14
Did you find any workaround?
Change the target Android version to 13. The code will work on Android 14 then.
Relevant log output
Exception has occurred: Java.Lang.SecurityException
com.companyname.dotnetmaui: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualObjectMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:20416
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:876
at Android.Content.ContextWrapper.RegisterReceiver(BroadcastReceiver receiver, IntentFilter filter) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Content.ContextWrapper.cs:2605
at Microsoft.Maui.Networking.ConnectivityImplementation.StartListeners() in D:\a\_work\1\s\src\Essentials\src\Connectivity\Connectivity.android.cs:48
at Microsoft.Maui.Networking.ConnectivityImplementation.add_ConnectivityChanged(EventHandler`1 value) in D:\a\_work\1\s\src\Essentials\src\Connectivity\Connectivity.shared.cs:91
at Microsoft.Maui.Networking.Connectivity.add_ConnectivityChanged(EventHandler`1 value) in D:\a\_work\1\s\src\Essentials\src\Connectivity\Connectivity.shared.cs:58
at DotnetMaui.App..ctor() in /Users/daniel/Development/Samples/DotnetMaui/App.xaml.cs:9
at at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
at at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
at at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
at at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
at at System.Collections.Concurrent.ConcurrentDictionary`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func`2 valueFactory)
at at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
at at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType) in D:\a\_work\1\s\src\Core\src\MauiContext.cs:68
at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType) in D:\a\_work\1\s\src\Core\src\MauiContext.cs:68
at at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[IApplication](IServiceProvider provider)
at Microsoft.Maui.MauiApplication.OnCreate() in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiApplication.cs:46
at Android.App.Application.n_OnCreate(IntPtr jnienv, IntPtr native__this) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Application.cs:1086
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:26
at java.lang.SecurityException: com.companyname.dotnetmaui: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
at at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
at at android.os.Parcel.createException(Parcel.java:3041)
at at android.os.Parcel.readException(Parcel.java:3024)
at at android.os.Parcel.readException(Parcel.java:2966)
at at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5684)
at at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1852)
at at android.app.ContextImpl.registerReceiver(ContextImpl.java:1792)
at at android.app.ContextImpl.registerReceiver(ContextImpl.java:1780)
at at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
at at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
at at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
at at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
at at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6998)
at at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236)
at at android.os.Handler.dispatchMessage(Handler.java:106)
at at android.os.Looper.loopOnce(Looper.java:205)
at at android.os.Looper.loop(Looper.java:294)
at at android.app.ActivityThread.main(ActivityThread.java:8177)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
at Caused by: android.os.RemoteException: Remote stack trace:
at at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13908)
at at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
at at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2720)
at at android.os.Binder.execTransactInternal(Binder.java:1339)
at at android.os.Binder.execTransact(Binder.java:1275)
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Reactions: 3
- Comments: 29 (6 by maintainers)
I’m getting this on a project after .net 8 upgrade, specifically during this call:
Connectivity.ConnectivityChanged += OnConnectivityChanged;This issue has been resolved and a fix is merged but is yet to be released. I expect it to be part of the upcoming service release.
I don’t think this has been released yet. If you want to be sure, check the nightly feed.
Still get the exception on Connectivity.ConnectivityChanged in the newest pre-release from 3days ago, not sure if it’s my fault or if the pre-release didn’t include the fix
Explicitly in the AndroidManifest under the Platform folder
Are you able to provide an update on the timing of when this fix will get released?
@dylix if maui team counts that it is fixed then it is fixed, don’t you understand this?
@albertruff You can temporary avoid this exception by setting the Android targetSdkVersion to 33 instead of 34.
We also face this issue. Is there any chance to temporary avoid this exception?
verified the issue exists on Xamarin forms as well