6

I have a Broadcast receiver setup so that a pop-up message is displayed to the user after each upgrade of my app, or if this is the first time the package is installed. I tested this on my Droid running Android 2.2 both as a fresh install and after upgrading my app, as well in the Emulator running 1.5 and 1.6, and I see everything run fine.

However, I received an error report from a user that lists the following exception:

java.lang.RuntimeException: Unable to instantiate receiver 
com.name.pkg.FirstRunBroadcastReceiver: java.lang.ClassNotFoundException: com.name.pkg.app_name.FirstRunBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.name.pkg.app_name.apk]
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
at android.app.ActivityThread.access$3200(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.name.pkg.app_name.FirstRunBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.name.pkg.app_name.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.handleReceiver(ActivityThread.java:2780)
... 10 more

Can someone tell me why one of my users received this exception?

In my Manifest file, I have things setup like this:

<receiver android:name=".FirstRunBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACE"/>
        <data android:scheme="package" android:path="com.name.pkg.app_name">
    </intent-filter>
</receiver>

The class FirstRunBroadcastReceiver is setup like this:

package com.name.pkg.app_name;

public class FirstRunBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Contenxt context, Intent intent)
    {
        Uri uri = intent.getData();
        if( uri.toString().compareTo("package:com.name.pkg.app_name") == 0 )
        {
            //set shared prefs data to determine if start-up message should be shown
        }
    }
}

Upon a first time install, the default value for the shared prefs is set so that the pop-up message will be displayed, but I wouldn't think that would have anything to do with this exception.

I tested it again, and I don't receive an exception. Thanks for any help you can provide.

Michael
  • 118
  • 1
  • 1
  • 7
  • Is this a continuous thing for this user? Or did it just happen once? – Falmarri Aug 24 '10 at 17:49
  • Hi Falmarri, unfortunately I have no idea if this is a continuous exception or not. I got the report through the Android Market. Whomever encountered this error did not send me an email, which would've been nice. So I just don't know. There is only one error report for this in my Market account, but that doesn't necessarily mean anything. – Michael Aug 24 '10 at 19:58
  • Did you solve this? - Post an answer. – levi Aug 07 '14 at 01:41
  • I have this error also. After 10 years there are no solution? – Adrian Grygutis May 29 '20 at 07:23

3 Answers3

4

from the android documentation on "android:exported" attribute for a receiver:

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID. The default value depends on whether the broadcast receiver contains intent filters. The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name. This implies that the receiver is intended only for application-internal use (since others would not normally know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the broadcast receiver is intended to receive intents broadcast by the system or other applications, so the default value is "true"

Since your receiver has child intents the default value for android:exported is true. Just state this explicitly and it should function fine.

ie,

<receiver android:name=".FirstRunBroadcastReceiver" android:exported="true">
<intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACE"/>
    <data android:scheme="package" android:path="com.name.pkg.app_name">
</intent-filter>

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
manu
  • 49
  • 3
0

I am a newbie with android development and my solution was a simple one caused my stupidity. I had renamed my broadcastreceiver file and when I tried to run the application I received the classdefnotfound error. I ended up cleaning and rebuilding the project and the error was removed.

JenniferG
  • 602
  • 1
  • 5
  • 13
0

Is package declaration in your FirstRunBroadcastReceiver class as follows?

package com.name.pkg.app_name;

According to your exception stack it should be so.

plugmind
  • 7,926
  • 4
  • 34
  • 39
  • Hi radek-k, yes, my package declaration is in FirstRunBroadcastReceiver. It's the very first line in the source file. I'll update my original post and add this. – Michael Aug 24 '10 at 19:59
  • My question was: is the package declaration exactly the same as I wrote above? – plugmind Aug 24 '10 at 20:16
  • Exactly as you wrote? If you're talking about the actual name of the app, then obviously the app name is not called "app_name" and other than that, I edited my post to show exactly what the source code says. – Michael Aug 24 '10 at 21:21
  • 1
    I think it may be a case that Android tries to instantiate your broadcast receeiver during replacement of your app when previous version is removed and new one is not there yet. – plugmind Aug 27 '10 at 07:45
  • Do you know what could explain the randomness of it (it's worked every time on my Droid with 2.2 and on the Emulator, but someone out there encountered it)? Is there a coding solution that would alleviate this problem, or am I better off just removing this functionality, or trying to get this type of behavior some other kind of way? – Michael Aug 30 '10 at 18:22