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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Mudassar
  • 1,566
  • 4
  • 21
  • 31

4 Answers4

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

Community
  • 1
  • 1
paul
  • 795
  • 1
  • 7
  • 19
  • 2
    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 :( – Fraggle Mar 30 '13 at 16:06
  • 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. – petrsyn Feb 11 '14 at 19:58
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,

Mudassar
  • 1,566
  • 4
  • 21
  • 31
  • 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. – Mudassar May 09 '12 at 12:57
  • 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. – Alexey Jul 01 '12 at 06:16
  • 1
    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? – Fraggle Mar 30 '13 at 16:03
  • Yea, I'm wondering about this. It seems like the crash should happen before the app have not even started yet. – Edison May 20 '13 at 04:17
  • 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. – petrsyn Feb 11 '14 at 18:28
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.

Ricardo
  • 2,086
  • 25
  • 35
  • I just needed to export the signed package again. I did not need to manually use jarsigner and zipalign. – faizal Nov 11 '14 at 05:10
0
 android:name=".MyApplication"

also consider adding full package

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • 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. – Mudassar Apr 25 '12 at 07:58