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!

Mark Allison
  • 21,839
  • 8
  • 47
  • 46
Romulus Urakagi Ts'ai
  • 3,699
  • 10
  • 42
  • 68
  • 2
    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. – Mark Allison Jun 10 '11 at 07:36
  • Yes, I'm running Proguard. I'll check it, thank you very much. – Romulus Urakagi Ts'ai Jun 13 '11 at 10:04
  • The SD card is removed or mounted to a PC http://stackoverflow.com/questions/4820554/android-unable-to-instantiate-activity-classnotfoundexception http://stackoverflow.com/questions/5483947/android-intermittent-class-not-found-run-time-exception – Dante Jul 22 '11 at 08:58

6 Answers6

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.

OldSchool4664
  • 1,454
  • 17
  • 13
  • 1
    I have come across an interesting article. This bug is discussed right at the bottom of the page: [ContentProvider Class Not Found](http://nolanlawson.com/2011/04/06/army-of-testers/) – OldSchool4664 Aug 29 '11 at 18:36
  • 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. – Learn OpenGL ES Aug 02 '12 at 12:21
  • it can be easily checked if it is a proguard problem - works in debug mode / doesnt work in release mode = its proguard problem – AndroidGecko Oct 10 '12 at 12:38
  • 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). – Someone Somewhere Jun 17 '13 at 17:57
  • 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 https://git.io/fNXhm. Please review it. – hasnain_ahmad Aug 06 '18 at 05:49
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.

esteewhy
  • 1,300
  • 13
  • 23
  • 1
    Why is it needed to be fully qualified? Some devices have a bug to fail to recognize not fully qualified name? – Romulus Urakagi Ts'ai Jun 17 '11 at 06:01
  • 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 ClassNotFound – davenpcj Feb 26 '13 at 22:19
  • 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 https://git.io/fNXhm. Please review it. – hasnain_ahmad Aug 06 '18 at 05:44
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.

Community
  • 1
  • 1
sleep
  • 4,855
  • 5
  • 34
  • 51
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)

fasti
  • 2,379
  • 3
  • 16
  • 9
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

user1608385
  • 609
  • 8
  • 17
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
randr0id
  • 159
  • 5