darryncampbell / darryncampbell-cordova-plugin-intent

General purpose intent shim layer for cordova appliations on Android. Handles various techniques for sending and receiving intents.
MIT License
86 stars 136 forks source link

Fix NullPointerException caused by wrong authority for FileProvider #58

Closed Gachapen closed 6 years ago

Gachapen commented 6 years ago

I got several crash reports in Google Play that had this cryptic stack trace:

java.lang.RuntimeException: 
  at android.app.ActivityThread.installProvider (ActivityThread.java:7110)
  at android.app.ActivityThread.installContentProviders (ActivityThread.java:6594)
  at android.app.ActivityThread.handleInstallProvider (ActivityThread.java:3559)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2141)
  at android.os.Handler.dispatchMessage (Handler.java:108)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:7425)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
Caused by: java.lang.NullPointerException: 
  at android.support.v4.content.FileProvider.parsePathStrategy (FileProvider.java:604)
  at android.support.v4.content.FileProvider.getPathStrategy (FileProvider.java:578)
  at android.support.v4.content.FileProvider.attachInfo (FileProvider.java:391)
  at android.app.ActivityThread.installProvider (ActivityThread.java:7107)

After some googling, I discovered that the authority for the FileProvider provider in AndroidManifest.xml should be ${applicationId}.fileprovider instead of ${applicationId}.provider, which is also what the official documentation specifies (https://developer.android.com/reference/android/support/v4/content/FileProvider).

Updating the app with this change seems to have silenced these crash reports. I'm not an expert in Android development, so I'd be glad if anyone could confirm that this change is correct.

darryncampbell commented 6 years ago

Thanks for the pull request. I have just returned from a week off work so I'll take a look at it when I get a chance.

darryncampbell commented 6 years ago

I took a look but I got an error - I think you also need to update https://github.com/darryncampbell/darryncampbell-cordova-plugin-intent/blob/master/src/android/IntentShim.java#L319, can you please check?

Gachapen commented 6 years ago

You're right, I'm getting this error as well. I will fix it.

Gachapen commented 6 years ago

I was a bit quick to reply. I actually don't get the error, but I see why, and I also think I understand why I, and some others, get the NullPointerException.

In my project, I actually have two providers, one from this plugin, providing FileProvider, and one from https://github.com/katzer/cordova-plugin-local-notifications, providing a class inherited from FileProvider. Both of these specifies ${applicationId}.provider as authorities, which I think is the cause of the exception (because of a conflict). Changing this plugin to use ${applicationId}.fileprovider probably has resolved this conflict, and thus also the exception, but it now uses the other derived FileProvider. Therefore, I don't think this is an issue specific for this plugin, but rather an issue when combining multiple plugins.

I'm not sure how this could be solved in a good way. Maybe change the authorities to something more unique, like ${applicationId}.darryncampbell.cordova.plugin.intent.fileprovider?

darryncampbell commented 6 years ago

Thanks, I think the suggestion to use unique names makes sense. I went ahead and implemented the fix in version 1.1.1. If that works for you then I will close this pull request.

Gachapen commented 6 years ago

Great, thanks. I won't be publishing a new version of our app yet, so I can't say for sure that it will work, but it should, as my current workaround (with a different name) works. So I'll close the PR and rather create a new if necessary.