I have an app that is using ActiveAndroid and it's been working fine. However; now when I try to save a model to the database I'm getting a SecurityException.
The stack is:
Error saving model java.lang.SecurityException: Failed to find provider null for user 0; expected to find a valid ContentProvider for this authority
at android.os.Parcel.readException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1888)
at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:801)
at android.content.ContentResolver.notifyChange(ContentResolver.java:2046)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1997)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1967)
at com.activeandroid.Model.save(Model.java:162)
[.... local stack removed]
Has anyone else experienced this? Do we need to specify the Content Provider in the AndroidManifest.xml?
Sorry but I do not have an isolated example of this yet. I will work to put something together.
Thanks in advance
Android O apparently requires the use of a custom ContentProvider for database usage, even if you do not intend to share your data with other applications.
https://developer.android.com/about/versions/oreo/android-8.0-changes.html
Your custom class must be defined in a provider tag within your application tag in AndroidManifest.xml. If applicable, set exported=false to protect your data from external usage.
For me the problem arose because I was calling notifyChange from the contentResolver. I ended up not needing this, so I was able to avoid implementation a ContentProvider.
For hot fix, set your
and you will ignore android O features. It will save your day!
important for hot fix:
But this solution will be invalid.
August 2018: New apps required to target API level 26 (Android 8.0) or higher. November 2018: Updates to existing apps required to target API level 26 or higher. 2019 onwards: Each year the targetSdkVersion requirement will advance. Within one year following each Android dessert release, new apps and app updates will need to target the corresponding API level or higher.
Another ways, you can fix with ActiveAndroid but It is deprecated now. You can try or you can try ReActiveAndroid
with ActiveAndroid, visit https://github.com/pardom/ActiveAndroid/issues/536#issuecomment-344470558
When project have defined model classes in java source code through addModelClasses configuration method application will still be crashing cause model classes wont be loaded through that configuration. In that situation You need to move model definition to the AndroidManifest.xml file.
AndroidManifest.xml
DatabaseContentProvider.java
If you are doing something with FileProvider, don't miss to change package name
Edit Manifest and add this provider tag node in side application node.
I use this and it fixed my error. Click here for demo.
There are two simple steps.
You need to have the provider specified in your
AndroidManifest.xml
under yourapplication
tag like the following.And you need to have the java class which specifies the provider stated in the manifest. In my case, I had the provider file in the
util
package.That's it. You are good to go.
Here is a complete working code for SQLite read/write/update operation in Github. Hope that helps!
As pointed out by @GeigerGeek security changes on Android 26 and above require you to specify the content provider in your manifest.
For ActiveAndroid you can add the below to your Manifest and change your package name.
If using flavours on your build process you can use below instead:
Using
${applicationId}
will help in flavor based project structure where application package may be different for each flavors.For more solutions or info this was taken from here.
Maybe because of your uri is null when you use notifyChange, https://developer.android.com/about/versions/oreo/android-8.0-changes.html#ccn
Android O will add check for provider: