Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is android.util.LruCache.* not found when using android-support-v4?

I am writing a project that uses LruCache, which is included in the android-support-v4.jar compat library. When running on devices with JB, the code works fine, but when I run it on my Droid X with GB, the app dies with the following errors:

I/dalvikvm(2459): Could not find method android.util.LruCache.put, referenced from method blah.blah.Utility.getBitmap
W/dalvikvm(2459): VFY: unable to resolve virtual method 2641: Landroid/util/LruCache;.put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
D/dalvikvm(2459): VFY: replacing opcode 0x6e at 0x0026
... [repeats similar lines] ...
W/dalvikvm(2459): Unable to resolve superclass of Lblah/blah/Utility$1; (353)
W/dalvikvm(2459): Link of class 'Lblah/blah/Utility$1;' failed
E/dalvikvm(2459): Could not find class 'blah.blah.Utility$1', referenced from method blah.blah.Utility.initCaches
W/dalvikvm(2459): VFY: unable to resolve new-instance 559 (Lblah/blah/Utility$1;) in Lblah/blah/Utility;
D/dalvikvm(2459): VFY: replacing opcode 0x22 at 0x000d
D/dalvikvm(2459): VFY: dead code 0x000f-002c in Lblah/blah/Utility;.initCaches (ILandroid/content/Context;)V
I/dalvikvm(2459): Could not find method android.util.LruCache.get, referenced from method blah.blah.Utility.mCacheGet
W/dalvikvm(2459): VFY: unable to resolve virtual method 2640: Landroid/util/LruCache;.get (Ljava/lang/Object;)Ljava/lang/Object;
D/dalvikvm(2459): VFY: replacing opcode 0x6e at 0x0008
D/dalvikvm(2459): VFY: dead code 0x000b-000e in Lblah/blah/Utility;.mCacheGet (Ljava/lang/String;)Landroid/graphics/Bitmap;
... [repeats similar lines] ...
D/AndroidRuntime(2459): Shutting down VM
W/dalvikvm(2459): threadid=1: thread exiting with uncaught exception (group=0x40018560)
E/AndroidRuntime(2459): FATAL EXCEPTION: main
E/AndroidRuntime(2459): java.lang.NoClassDefFoundError: blah.blah.Utility$1
E/AndroidRuntime(2459):     at blah.blah.Utility.initCaches(Utility.java:49)
E/AndroidRuntime(2459):     at blah.blah.MainActivity.onCreate(MainActivity.java:40)
... [etc] ...
W/ActivityManager(1318):   Force finishing activity blah.blah/.MainActivity

Utility.java:49 looks like this:

    mCache = new LruCache<String, Bitmap>( mCacheSize )   // <-- line 49 --
    {
        @Override
        protected int sizeOf( String key, Bitmap bitmap )
        {
            return bitmap.getByteCount();
        }
    };

So everywhere I referenced LruCache or its methods causes an error. The first thing that comes to mind is that android-support-v4.jar was somehow left out of the APK file, but that doesn't look like the case. I've placed the jar file under libs/, and checking classes.dex.d after running ant debug, I see that it has added the following line:

/home/paul/workspace/blah/libs/android-support-v4.jar \

My second thought was that maybe LruCache isn't in android-support-v4, but looking at the source I see that the following source file is in there, and that it has the methods I am trying to use:

/home/paul/bin/android-sdk/extras/android/support/v4/src/java/android/support/v4/util/LruCache.java

So why isn't it found?

like image 920
paulscode Avatar asked Aug 06 '12 23:08

paulscode


1 Answers

The classes in the support library use a different namespace.

You need to import:

import android.support.v4.util.LruCache;

or alternatively you can copy & compile LruCache.java directly into your app.

like image 191
Rob Avatar answered Oct 21 '22 02:10

Rob