java.lang.NoClassDefFoundError with HttpResponseCache and DiskLruCache

痞子三分冷 提交于 2019-12-10 20:04:43

问题


I´m trying test this cache Library in a new project Android.
When I run the project, I get this Logcat:

01-24 03:45:31.109: E/AndroidRuntime(1983): FATAL EXCEPTION: main
01-24 03:45:31.109: E/AndroidRuntime(1983): Process: com.test_cache, PID: 1983
01-24 03:45:31.109: E/AndroidRuntime(1983): java.lang.NoClassDefFoundError: com.jakewharton.DiskLruCache
01-24 03:45:31.109: E/AndroidRuntime(1983):     at com.integralblue.httpresponsecache.compat.libcore.net.http.HttpResponseCache.<init>(HttpResponseCache.java:83)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at com.integralblue.httpresponsecache.HttpResponseCache.<init>(HttpResponseCache.java:155)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at com.integralblue.httpresponsecache.HttpResponseCache.install(HttpResponseCache.java:192)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at com.test_cache.MainActivity.onCreate(MainActivity.java:21)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.Activity.performCreate(Activity.java:5243)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.os.Looper.loop(Looper.java:137)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at java.lang.reflect.Method.invokeNative(Native Method)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at java.lang.reflect.Method.invoke(Method.java:515)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-24 03:45:31.109: E/AndroidRuntime(1983):     at dalvik.system.NativeStart.main(Native Method)

I copied both jars from projects HttpResponseCache by Craig Andrews and DiskLruCache by Jake Wharton in my libs folder:

This is my MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
    final File httpCacheDir = new File(getCacheDir(), "http");

    try {
        com.integralblue.httpresponsecache.HttpResponseCache.install(httpCacheDir, httpCacheSize);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }        
}

I read in other threads that it´s problem of the order, so in the Order & Export tab in the Java Build Path, I put this in first place:


I clean the proyect, rebuild and nothing work, the problem persist...
Any idea...??

Thanks.


回答1:


Because you used disklrucache-2.0.2.jar and httpresponsecache-1.3.jar

In library httpresponsecache-1.3.jar use disklrucache-1.2.1.jar

In disklrucache-1.2.1.jar the package is: com.jakewharton but in version 2.0.2 the package is changed to com.jakewharton.disklrucache

That is the root cause.

If you wanna use disklrucache you should use version 1.2.1

You can download from here: http://www.mediafire.com/download/e2q4vz7kdlwxxr3/disklrucache-1.2.1.jar (.jar)

or source file: https://www.mediafire.com/?933nqqe9j4227cc (.jar)

Hope this can help you.



来源:https://stackoverflow.com/questions/21329326/java-lang-noclassdeffounderror-with-httpresponsecache-and-disklrucache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!