Failed to get provider: ClassNotFoundException

I have a MyContentProvider in my application that works great when I develop and run in debug mode.

 <provider android:name=".MyContentProvider" android:authorities="com.contactcities" android:exported="false"> </provider> 

But when I export the application, download it and run it, it will instantly work:

 10-10 18:24:37.682 E/AndroidRuntime(10428): FATAL EXCEPTION: main 10-10 18:24:37.682 E/AndroidRuntime(10428): java.lang.RuntimeException: Unable to get provider com.contactcities.MyContentProvider: java.lang.ClassNotFoundException: com.contactcities.MyContentProvider in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.contactcities-1.apk] 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.installProvider(ActivityThread.java:4509) 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.installContentProviders(ActivityThread.java:4281) 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4237) 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.access$3000(ActivityThread.java:125) 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071) 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.os.Handler.dispatchMessage(Handler.java:99) 10-10 18:24:37.682 E/AndroidRuntime(10428): at android.os.Looper.loop(Looper.java:123) 

I can play it on all my devices, 2.2, 4.0, 4.1

Today I read a lot of threads. Some of them blame ProGuard for this. I tried to add

 -keep public class com.contactcities.MyContentProvider 

but no luck.

When I disable proguard without putting proguard.config=proguard.cfg in my project.properties . It still gives the same error in the release version. Debugging is fine again. Maybe it is not enough to disable such a program?

Maybe a hint that it refers to maps.jar in this crash. I'm not sure why he does it.

 in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar 

Any hints would be much appreciated

+4
source share
4 answers

Arg, I thought I tried it, but it looks like I didn’t.

Projectclean did the job

front palm

+3
source

I was getting the same error, it turned out that I had the wrong value for the android:name attribute for my provider tag in AndroidManifest.xml:

 <provider android:authorities="com.example.ProviderClass" android:name=".ProviderClass" android:exported="false" > </provider> 

So double check this value as above .ProviderClass or whatever your class name (with a dot prefix). Then go to Project> Clean and it will work.

+1
source

The authority attribute must match the content authority, while the name attribute must match the class name of your custom provider class.

For instance,

  <provider android:authorities="com.richa.myapp" android:name=".data.CustomProvider"/> 
+1
source

Maybe my inexperience will help someone else with this mistake. It was completely mysterious to me, but, like everything in computers, it is obvious when you know.

I followed this tutorial, which is generally very good: http://www.vogella.com/tutorials/AndroidSQLite/article.html

At the beginning, he says to create a package called "contentprovider", and then the class inside it "MyTodoContentProvider". Now anyone who is familiar with Java-style naming conventions would see that this should apply to the main package. But I was new to this, so I created it on the side somewhere.

Everywhere, building the code, everything worked fine. Eclipse found and included this package and found no errors. Then there was a failure and the critical message "Unable to get provider." The path in the error message that I now understood was the nonexistent path in which the code looked for the class was so long and contained so many iterations of the “contentprovider” that it was difficult to understand what this was about or what was wrong.

I suspected that the problem was in the manifest file, but I was sure that it was in “android: authority”. It was a little unintuitive. The line in this element in the manifest corresponds to the public member that you expose in your provider class. This member is called "CONTENT_URI". But these two do not exactly match. For instance:

 private static final String AUTHORITY = "com.vegnab.provider"; private static final String BASE_PATH = "todos"; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH); 

It ends with:

 CONTENT_URI = "content://com.vegnab.provider/todos" 

(Please note that AUTHORITY is itself a private member, and not accessible externally.)

The corresponding manifest line will look like this:

 android:authorities="com.vegnab.provider" 

This is the middle part between the "content: //" schema and the base path.

The problem was in the manifest line:

 android:name=".contentprovider.MyTodoContentProvider" 

I had an error-free class named "MyTodoContentProvider" in a package called "contentprovider", so what's the problem? The problem was that the package was in the wrong place. It was assumed that it will relate to the main packaging. What the dot means is first in the title. It should follow the same syntax as the android: name lines in its sections of the manifest.

If you find this helpful, please be one. Once you solve this problem, it's easy to forget how mysterious it was.

0
source

Source: https://habr.com/ru/post/1438977/


All Articles