ClassNotFoundException for a ContentProvider
Asked Answered
T

6

13

I have a ContentProvider class and is declared in AndroidMenifest.xml like this:

<provider android:name=".MediaSearchProvider"
    android:authorities="org.iii.romulus.meridian.mediasearch">
    <path-permission android:path="/search_suggest_query"
        android:readPermission="android.permission.GLOBAL_SEARCH" />
</provider>

It works well on most devices, but the Market tells me some users are suffering error with it. The stack trace is:

java.lang.RuntimeException: Unable to get provider org.iii.romulus.meridian.MediaSearchProvider: java.lang.ClassNotFoundException: org.iii.romulus.meridian.MediaSearchProvider in loader dalvik.system.PathClassLoader[/mnt/asec/org.iii.romulus.meridian-1/pkg.apk]
at android.app.ActivityThread.installProvider(ActivityThread.java:4509)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4281)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4237)
at android.app.ActivityThread.access$3000(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: org.iii.romulus.meridian.MediaSearchProvider in loader dalvik.system.PathClassLoader[/mnt/asec/org.iii.romulus.meridian-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.ActivityThread.installProvider(ActivityThread.java:4494)
... 12 more

I have totally no idea about what's up and I can't reproduce it on any of my phones. I also tried clean and build, but the report still comes up. Anyone can help? Thanks!

Tawnyatawsha answered 10/6, 2011 at 7:31 Comment(3)
Are you running proguard obfuscation when you make a release build? That can sometimes cause this kind of problem because it changes class names which then do not get resolved at runtime. Anything that gets declared in the manifest, or is referenced using reflection must be excluded from the proguard obfuscation.Wynne
Yes, I'm running Proguard. I'll check it, thank you very much.Gehlenite
The SD card is removed or mounted to a PC #4821054 #5484447Sunderance
V
5

The answers regarding proguard are incorrect. This would cause an easily reproducible error on every phone, every time, because the ContentProvider class would be completely missing. The developer clearly states that they cannot reproduce the error, meaning that the ContentProvider class is present but for some reason is not being found on one of their user's phones.

I have the same crash reported in the market for my app. The stack traces look identical, and the error is occurring at installProvider. I have about 15 test phones in my office and none of them can reproduce this problem. Any other ideas would be appreciated.

Fully qualified names in the manifest are only necessary if your java package names are not the same as your android package name. If a fully qualified name is not specified, the OS will automatically prepend the android package name to the class name specified in the manifest.

Villegas answered 29/8, 2011 at 18:19 Comment(5)
I have come across an interesting article. This bug is discussed right at the bottom of the page: ContentProvider Class Not FoundVillegas
I've also seen this reported on the market, and agreed it's not related to Proguard. If it was it would always be reproducible. It usually comes in spurts around upgrade time.Cockoftherock
it can be easily checked if it is a proguard problem - works in debug mode / doesnt work in release mode = its proguard problemStrangulate
I fixed it just now in my own app: 1) Properties -> Java Build Path -> remove all jars listed (except "Android dependencies" and "Android Private Libraries"). 2) renamed the "lib" folder in my projects to "libs" 3) clean and rebuild all. Deployed to my debug device and it worked great ! (before I did the 3 steps above I repeatedly saw the ClassNotFoundException).Mufinella
I have fully qualified package path for name attribute. But still, I encounter the error on android KitKat 4.4.4. Here are error stack git.io/fNXhm. Please review it.Kinkajou
P
4

Ensure twice that you have correct qualified class name specified in AndroidManifest.xml, it must read something like this:

<provider
    android:authorities="org.iii.romulus.meridian.mediasearch"
    android:name="org.iii.romulus.meridian.MediaSearchProvider">
</provider>

Notice that @name is fully qualified.

Proliferation answered 16/6, 2011 at 15:21 Comment(3)
Why is it needed to be fully qualified? Some devices have a bug to fail to recognize not fully qualified name?Gehlenite
This was my problem. I thought I had everything specified, but the "name" attribute was ".BlahMyProvider" and it didn't happen to be in the top level package of the app, yielding a ClassNotFoundDivided
I have fully qualified package path for name attribute. But still, I encounter the error on android KitKat 4.4.4. Here are error stack git.io/fNXhm. Please review it.Kinkajou
C
2

This sounds similar to an issue I had that was caused by an issue with the ClassLoader, see here: Bizarre behaviour when using Apache Commons lib in Android

This bug discusses an error relating to the class loader failing sometimes. The fix for me was to add this line:

Thread.currentThread().setContextClassLoader(this.getClassLoader());

in the constructor of the class that was calling the code that was failing.

Counterrevolution answered 8/11, 2011 at 6:59 Comment(0)
W
0

Proguard excludes all inherited content providers by default with this line (make sure it's in your cfg):

-keep public class * extends android.content.ContentProvider

If you have any additional inheritance you should exclude it as well or exclude your specific Content Provider class, for example:

-keep public class org.iii.romulus.meridian.MediaSearchProvider
Witchhunt answered 4/8, 2011 at 20:18 Comment(0)
E
0

This is an old thread, and the OP didn't have the same ContentProvider declaration as me, but I had the same exact error, so I want to share my findings, in case it helps anyone.

For me, what caused the problem was that the ContentProvider declaration in the AndroidManifest.xml had an exported attribute set to true:

android:exported="true"

Removing it fixed the problem for me. (I didn't really need it)

Excipient answered 20/5, 2017 at 23:13 Comment(0)
P
0

If your app has grown large enough to require Multidexing and your app is not set up correctly as a Multidex app you will get this error. To set up your app as a multidex app, follow these directions:

Setup multidex app

Phionna answered 13/3, 2020 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.