ingageco / capacitor-music-controls-plugin

An update to Cordova Music Controls plugin to support Capacitor
15 stars 3 forks source link

API34+ : java.lang.SecurityException: xx.xxxxxxx.xx: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts #22

Open NorthFred opened 1 month ago

NorthFred commented 1 month ago

App updates must target targetSdk 34 by August 31st 2024. When targetting the app for API level 34, following error occurs:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xx.xxxxxxx.xx, PID: 4456
    java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xxxxxxx.xx/xx.xxxxxxx.xx.MainActivity}: java.lang.SecurityException: xx.xxxxxxx.xx: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:205)
        at android.os.Looper.loop(Looper.java:294)
        at android.app.ActivityThread.main(ActivityThread.java:8176)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
     Caused by: java.lang.SecurityException: xx.xxxxxxx.xx: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
        at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
        at android.os.Parcel.createException(Parcel.java:3041)
        at android.os.Parcel.readException(Parcel.java:3024)
        at android.os.Parcel.readException(Parcel.java:2966)
        at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5668)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1852)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1792)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1780)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
        at com.capacitorjs.plugins.share.SharePlugin.load(SharePlugin.java:44)
        at com.getcapacitor.PluginHandle.loadInstance(PluginHandle.java:115)
        at com.getcapacitor.PluginHandle.load(PluginHandle.java:105)
        at com.getcapacitor.PluginHandle.<init>(PluginHandle.java:65)
        at com.getcapacitor.Bridge.registerPlugin(Bridge.java:664)
        at com.getcapacitor.Bridge.registerAllPlugins(Bridge.java:620)
        at com.getcapacitor.Bridge.<init>(Bridge.java:218)
        at com.getcapacitor.Bridge.<init>(Unknown Source:0)
        at com.getcapacitor.Bridge$Builder.create(Bridge.java:1539)
        at com.getcapacitor.BridgeActivity.load(BridgeActivity.java:42)
        at com.getcapacitor.BridgeActivity.onCreate(BridgeActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:8595)
        at android.app.Activity.performCreate(Activity.java:8573)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:205) 
        at android.os.Looper.loop(Looper.java:294) 
        at android.app.ActivityThread.main(ActivityThread.java:8176) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13895)
        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2563)
        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2708)
        at android.os.Binder.execTransactInternal(Binder.java:1339)
        at android.os.Binder.execTransact(Binder.java:1275)

The app keeps closing when trying to run it.

ingageco commented 1 month ago

@NorthFred I've tested Android 34 in our apps with the latest version of this plugin + Capacitor 5 and I dont get this error.

Are you running the latest version + Capacitor 5?

Also - I don't see anything in this trace that references the plugin.

ingageco commented 1 month ago

I only test Android 34 at this point - so I'm sure this supports it.

heffthedev commented 1 month ago

I have the same issue trying to use the plugin on an API 34 emulator (no matching API 34 device available sadly). https://github.com/ingageco/capacitor-music-controls-plugin/blob/main/android/src/main/java/com/ingageco/capacitormusiccontrols/CapacitorMusicControls.java I was adjusting the same file for the orignal cordova plugin to fit the requirement and I figure one will need the same adjustments in this version?

I used the androidx compat library to not cause issues with older versions, and also changed all mentions of FLAG_MUTABLE to FLAG_IMMUTABLE, because this seems to cause issues too.

Here's the things I changed that make it work on my end: https://github.com/ingageco/capacitor-music-controls-plugin/compare/main...heffthedev:capacitor-music-controls-plugin:main

ingageco commented 1 month ago

@heffthedev Thank you! i'll take a look at your changes and probably merge them once i confirm everything is working.

NorthFred commented 3 weeks ago

@ingageco When could we get this merged? I'm eager to start testing! :)

NorthFred commented 1 week ago

@ingageco @heffthedev Any updates, please? Can I get the commit with changes merged to this master branch, or a separate branch that can be used for testing?

ingageco commented 1 week ago

My time is limited right now- I'm happy to review a PR

NorthFred commented 1 week ago

@ingageco I am not able to create a PR from the changes done by @heffthedev since I am not a collaborator in the project. Could you merge the changes, they look good to me.

ingageco commented 1 week ago

@NorthFred You can try the review branch where i have merged these changes - but I may not keep the changes to method names updateElapsed and music-controls-skip-to. I'll let you know once you review and prepare to merge into main.

NorthFred commented 1 week ago

@ingageco Thanks for the help! I'm however running now into following error for RECEIVER_NOT_EXPORTED:

error: cannot find symbol
        ContextCompat.registerReceiver(context, (BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-previous"), ContextCompat.RECEIVER_NOT_EXPORTED);
                                                                                                                                               ^
  symbol:   variable RECEIVER_NOT_EXPORTED
  location: class ContextCompat
ingageco commented 1 week ago

@NorthFred I'm sorry but at this moment I'm too tied up in paid client work to dig into this.

NorthFred commented 1 week ago

@ingageco @heffthedev I realized my variables.gradle file was outdated. It needs following settings for Capacitor 5+:

ext {
    minSdkVersion = 22
    compileSdkVersion = 34
    targetSdkVersion = 34
    androidxActivityVersion = '1.7.0'
    androidxAppCompatVersion = '1.6.1'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxCoreVersion = '1.10.0'
    androidxFragmentVersion = '1.5.6'
    coreSplashScreenVersion = '1.0.0'
    androidxWebkitVersion = '1.6.1'
    junitVersion = '4.13.2'
    androidxJunitVersion = '1.1.5'
    androidxEspressoCoreVersion = '3.5.1'
    cordovaAndroidVersion = '10.1.1'
}

Then the RECEIVER_NOT_EXPORTED issue no longer appears.

I will continue testing tomorrow...

NorthFred commented 1 week ago

@ingageco This can now be merged.

Please exclude the commits with changes to updateElapsed and music-controls-skip-to.

Worth noting also is that the manifest file AndroidManifest.xml requires this permission for Android 14: <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

NorthFred commented 1 week ago

@ingageco The review branch can be merged to the main branch.

(The updateElapsed and music-controls-skip-to changes affect only to the README.md.)

I have received positive feedback from Android 14 users, so all is good!