1

I am trying to instantiate a class on my applications onCreate method as follows:

volleyQueueInstance = VolleySingleton.getInstance(getApplicationContext());

Following is my VolleySingleton class

public class VolleySingleton {
    private static VolleySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private VolleySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(30);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized VolleySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new VolleySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
}

But my app crashes on launch with the following stacktrace that says no class definition found for class VolleySingleton

java.lang.NoClassDefFoundError: rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton$1
        at rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton.<init>(VolleySingleton.java:25)
        at rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton.getInstance(VolleySingleton.java:44)
        at rides.even.odd.oddorevenrides.MyApplication.instantiateVolleyQueue(MyApplication.java:35)
        at rides.even.odd.oddorevenrides.MyApplication.onCreate(MyApplication.java:31)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
        at android.app.ActivityThread.access$1300(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

I am fairly new to java and android and am unable to figure out why is it not able find the class. As far as I understand, It is able to find the class but the exception occurs inside the constructor where I am trying to define mImageLoader. I am not sure though. Any help would be really appreciated.

update

Ok, I tried launching the application with another device (Samsung s4) and the application works fine. But when I try it with another device (Samsung Google Nexus S) it crashes with the above stacktrace.

update #2

I missed some log entries in the above stacktrace, Maybe this can help figure out whats going on.

01-05 05:46:12.933    3029-3029/rides.even.odd.oddorevenrides E/dalvikvm﹕ Could not find class 'rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton$1', referenced from method rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton.<init>
01-05 05:46:12.933    3029-3029/rides.even.odd.oddorevenrides W/dalvikvm﹕ VFY: unable to resolve new-instance 9625 (Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton$1;) in Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton;
01-05 05:46:12.933    3029-3029/rides.even.odd.oddorevenrides D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x001d
01-05 05:46:12.949    3029-3029/rides.even.odd.oddorevenrides D/dalvikvm﹕ DexOpt: unable to opt direct call 0xfd9f at 0x1f in Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton;.<init>
01-05 05:46:13.343    3029-3029/rides.even.odd.oddorevenrides D/AndroidRuntime﹕ Shutting down VM
01-05 05:46:13.343    3029-3029/rides.even.odd.oddorevenrides W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d2a300)
01-05 05:46:14.070    3029-3029/rides.even.odd.oddorevenrides E/AndroidRuntime﹕ FATAL EXCEPTION: main
Amyth
  • 32,527
  • 26
  • 93
  • 135
  • Usual this kind of exception (`java.lang.NoClassDefFoundError`) happens when java able to find this class at compile time but on runtime it isn't accessible. I haven't experience in Android but usually `manifest.mf` file responsible for runtime dependencies. Try to google in this direction. – Anatoly Jan 04 '16 at 22:55
  • Thanks @Anatoly will definitely try researching this direction. Is it possible though that it works on one device but not on the other if it is a manifest issue ? – Amyth Jan 04 '16 at 22:57
  • 1
    Just wild guess, if different devices has different runtime Android 5 vs Android 4 for example, and Android 4 dependencies persist but Android 5 absent it may happen, again it's wild guess. – Anatoly Jan 04 '16 at 23:00

1 Answers1

4

Ok so after hours of research finally I was able to resolve it. The problem here was the MultiDex support. I had partial multidex support in my app as I had

multiDexEnabled true

in my build.gradle file but my application was extending the Application class.

Just had my application class extend MultiDexApplication class and also added the following method in my application class file.

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

Was able to resolve it by researching towards the following log entry.

01-05 05:46:12.949    3029-3029/rides.even.odd.oddorevenrides D/dalvikvm﹕ DexOpt: unable to opt direct call 0xfd9f at 0x1f in Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton;.<init>
Amyth
  • 32,527
  • 26
  • 93
  • 135
  • According to [Configure your app for multidex](https://developer.android.com/studio/build/multidex#mdex-gradle) you need to either inherit `MultiDexApplication` **or** override `attachBaseContext`. – Afilu Jul 03 '18 at 17:51