Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/OkHttpClient error when using Picasso offline capability - Android

I am working on a project which has offline feature capability using Picasso library in Android. So far, I'm just following the tutorial.

This is my gradle:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.firebase:firebase-database:9.0.2'
compile 'com.google.firebase:firebase-core:9.0.2'
compile 'com.google.firebase:firebase-auth:9.0.2'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.google.firebase:firebase-storage:9.0.2'
compile 'com.google.firebase:firebase-database:9.0.2'
compile 'com.firebaseui:firebase-ui-database:0.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
}

This is my Picasso setting:

    Picasso.Builder builder = new Picasso.Builder(this);
    builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE)); //always give me the error to this line
    Picasso built = builder.build();
    built.setIndicatorsEnabled(false);
    built.setLoggingEnabled(true);
    Picasso.setSingletonInstance(built);

And this is how I call Picasso in my actvity.java:

Picasso.with(this.getApplicationContext()).load(stringUriProfile).networkPolicy(NetworkPolicy.OFFLINE).into(mUriImageProfile, new Callback() {
            @Override
            public void onSuccess() {
                //the function fires whenever the picasso doesnt find picture from offline way
            }

            @Override
            public void onError() {
                Context ctx = getApplicationContext();
                Picasso.with(ctx).load(stringUriProfile).into(mUriImageProfile);
            }
        });
like image 941
A N Syafiq. Avatar asked Dec 15 '22 03:12

A N Syafiq.


2 Answers

I don't fully understand the problem and this solution. It seems that in recent revisions to the OkHttp library, the package that contains OkHttpClient changed. The 2.5.2 version of Picasso expects to find it in the old package, com.squareup.okhttp. One solution is to replace:

compile 'com.squareup.okhttp3:okhttp:3.4.1'

with

compile 'com.squareup.okhttp:okhttp:2.5.0'

There may be better solution. This is the only one I have found.

like image 117
Bob Snyder Avatar answered Dec 27 '22 14:12

Bob Snyder


If you want to keep okhttp3, you can use Jake Wharton's solution:

add to gradle:

 compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'

initialize with:

 OkHttpClient client = // ...
 Picasso picasso = new Picasso.Builder(context)
     .downloader(new OkHttp3Downloader(client))
     .build()
like image 33
Chad Bingham Avatar answered Dec 27 '22 13:12

Chad Bingham