Search code examples
androidfirebasepicassookhttp

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);
            }
        });

Solution

  • 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.