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

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

我正在开发一个项目,该项目使用 Android 中的 Picasso 库具有离线功能。到目前为止,我只是按照教程进行操作。

这是我的 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'
}

这是我的毕加索设置:

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

这就是我在 actvity.java 中对 Picasso 的称呼:

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

我不完全理解这个问题和这个解决方案。似乎在最近对 OkHttp 库的修订中,包含 OkHttpClient 的包发生了变化。 Picasso 2.5.2 版本希望在旧包中找到它,com.squareup.okhttp。一种解决方案是替换:

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

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

可能有更好的解决方案。这是我找到的唯一一个。

如果想保留okhttp3,可以使用Jake Wharton's solution:

添加到 gradle:

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

初始化为:

 OkHttpClient client = // ...
 Picasso picasso = new Picasso.Builder(context)
     .downloader(new OkHttp3Downloader(client))
     .build()