dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

Adding a Connectivity.ConnectivityChanged handler throw an exception on Android 14 #17861

Closed daniel-c closed 1 year ago

daniel-c commented 1 year ago

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

  1. Create a new MAUI app with botnet new maui
  2. In the App constructor in App.xaml.cs, add : Connectivity.ConnectivityChanged += (sender, e) => { };
  3. 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)
573F4N077 commented 9 months ago

I am using version 8.0.7 but the event ConnectivityChanged still doesn't fire if I turn off Wifi or turn on Wife on my Android phone. At least it doesn't crash the whole application anymore. Do you have the same issue or does it work for you now?

plppp2001 commented 8 months ago

I am using version 8.0.7 but the event ConnectivityChanged still doesn't fire if I turn off Wifi or turn on Wife on my Android phone. At least it doesn't crash the whole application anymore. Do you have the same issue or does it work for you now?

Same here it's not working for me either. Please FIX MS.

shingming commented 8 months ago

Test on

The app will crash on the same exception in the initial stage

One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts ---> Android.OS.RemoteException

ilmian commented 8 months ago

Not working on Android 14.

dartasen commented 8 months ago

@ilmian Maybe you wanna open a new issue, since this one is closed