28

I am working on application which is host on android market. Sometimes (once a month ) I got a crash report:

Unable to instantiate application java.lang.ClassNotFoundException

App downloads are between 10,000-50,000. I don't know why this exceptions raise on some devices not all ( I tested it on 3 different devices & I couldn't re-produce it at my end).

I read articles/suggestions on different android forums regarding the issue but I didn't succeed in solving it. Does anyone face similar issue & suggest me what should I do?

Note: I am extending application class like this

public class MyApplication extends Application {

}

I register it in the manifest.xml like this

<application android:icon="@drawable/app_icon"
    android:label="@string/my_app_name" android:name="MyApplication">

Stack Trace :

java.lang.RuntimeException: Unable to instantiate application  com.xyz.MyApplication      java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk]
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:650)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4238)
at android.app.ActivityThread.access$3000(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-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.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:645)

I don't know why application crash on some devices not all.

6
  • Can you please add the complete stack trace? Commented Apr 25, 2012 at 7:54
  • what is the version of your ADT? Commented Apr 25, 2012 at 9:25
  • try to remove proguard from your project. Commented Apr 25, 2012 at 9:33
  • I didn't change anything in "proguard.cfg". Do you mean I have to remove "proguard.cfg" ? Commented Apr 25, 2012 at 9:37
  • yes yes i mean exactly the same.Try it. Commented Apr 25, 2012 at 9:48

4 Answers 4

11

Some other similar questions indicate that this can be a user error. "/mnt/asec/..." indicates that the app is running from the sdcard. If the sdcard is removed it could cause this error. 3rd party apps or rooted devices can probably move an app to the sdcard even if its not allowed by the manifest.

Similar Question

Sign up to request clarification or add additional context in comments.

2 Comments

I'm thinking that this is the real root of the problem. my app has installLocation="auto" currently. Had problems ever since I switched from "internal". Had other issues with SD card, where app simply disappears from users device. Android broken :(
Agree... In my case the error was reported 44 times for 380k active users. My app has install location auto. Concrete devices reporting this are DROID Razr. HTC Evo, One, Desire, Sensation. Sony Xperia. Then various Samsungs Galaxy S 2,3,4 and Note. All above phones seem to have micro SD card. Interestingly the error was never reported by any Nexus. However I'm not sure if "/mnt/asec/..." points to the microSD card only and not to the USB storage.
6

I think the problem is with getApplication() which I have used in 10 different place. So I have used singleton pattern to solve this.

public class MyApplication extends Application {
    private static MyApplication me;

    @Override
    public void onCreate() {        
        super.onCreate();
        me = this ;

    }
    public static MyApplication getInstance() {
         return me;
    }
}

Now I have used getApplication() like this

     MyApplication application = MyApplication.getInstance();

insted of

     MyApplication application = (MyApplication) getApplication();

I have uploaded the fixed version on the market & now waiting if there is anymore this kind of crash. If everything goes perfect ( if no more crash in 2 weeks) then I will close the question. In meanwhile anyone has better idea or know the solution , please share it.
Thanks,

5 Comments

I have uploaded the new release on market & since then I didn't get any crash report related to "Unable to instantiate application java.lang.ClassNotFoundException" so I think the issue is resolved. I am very thankful for everyone suggestions.
I've used this aproach since my very first release, but I still get ClassNotFoundException once or twice a month. If what you say was the case, it should have crashes with ClassCast exception rather than with ClassNotFound.
Yeah, I get crashes like this from a small % of users. I use the casting approach but I can't see why it would make a difference when using the singleton approach. Since it happens to a small # of people, it could be hard to know when you've actually fixed the problem. @junto have you had crashes like this since changing to the singleton approach?
Yea, I'm wondering about this. It seems like the crash should happen before the app have not even started yet.
Also think this has nothing to do with casting. I'm using the singleton approach all the time and have 44 reports and 380k of active users.
1

In my case, I was compiling and signing with Eclipse ADT (with File > Export > Export Android Application...) but missing some classes if I decompile my .apk. To solve it I use "Export an unsigned APK" and sign it using jarsigner and zipalign.

1 Comment

I just needed to export the signed package again. I did not need to manually use jarsigner and zipalign.
0
 android:name=".MyApplication"

also consider adding full package

1 Comment

Sorry there is no space in actual code, the space was due to editing here & I m sorry for that. Thanks for your time. In my previous build I used full pakcage path but I got same crash report.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.