Android Studio 3.0中的OkHttpClient错误

时间:2017-11-05 11:45:16

标签: android android-studio firebase okhttp

我试图在android studio中使用firebase从Youtube tutorial构建一个简单的博客应用程序一切都很好但是当我添加一个类 SimpleBlog 时,从那时我运行我的应用程序继续崩溃。

错误信息是:

11-05 17:32:35.525 2485-2485/naim.google.com.photographyblog E/UncaughtException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/OkHttpClient;
                                                                                      at com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31)
                                                                                      at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)
                                                                                      at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65)
                                                                                      at naim.google.com.photographyblog.SimpleBlog.onCreate(SimpleBlog.java:22)
                                                                                      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
                                                                                      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5361)
                                                                                      at android.app.ActivityThread.-wrap2(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                                   Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.okhttp.OkHttpClient" on path: DexPathList[[zip file "/data/app/naim.google.com.photographyblog-1/base.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_dependencies_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_0_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_1_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_2_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_3_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_4_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_5_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_6_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_7_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_8_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/naim.google.com.photographyblog-1/lib/x86, /system/lib, /vendor/lib]]
                                                                                      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                      at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                                      at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                      at com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31) 
                                                                                      at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76) 
                                                                                      at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65) 
                                                                                      at naim.google.com.photographyblog.SimpleBlog.onCreate(SimpleBlog.java:22) 
                                                                                      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024) 
                                                                                      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5361) 
                                                                                      at android.app.ActivityThread.-wrap2(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
11-05 17:32:36.753 2485-2485/naim.google.com.photographyblog E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: naim.google.com.photographyblog, PID: 2485
                                                                               java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/OkHttpClient;
                                                                                   at com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31)
                                                                                   at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)
                                                                                   at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65)
                                                                                   at naim.google.com.photographyblog.SimpleBlog.onCreate(SimpleBlog.java:22)
                                                                                   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
                                                                                   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5361)
                                                                                   at android.app.ActivityThread.-wrap2(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                                Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.okhttp.OkHttpClient" on path: DexPathList[[zip file "/data/app/naim.google.com.photographyblog-1/base.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_dependencies_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_0_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_1_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_2_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_3_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_4_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_5_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_6_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_7_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_8_apk.apk", zip file "/data/app/naim.google.com.photographyblog-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/naim.google.com.photographyblog-1/lib/x86, /system/lib, /vendor/lib]]
                                                                                   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                   at com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:31) 
                                                                                   at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76) 
                                                                                   at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:65) 
                                                                                   at naim.google.com.photographyblog.SimpleBlog.onCreate(SimpleBlog.java:22) 
                                                                                   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024) 
                                                                                   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5361) 
                                                                                   at android.app.ActivityThread.-wrap2(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:154) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

这是我的 build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "naim.google.com.photographyblog"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-auth:11.0.4'
    implementation 'com.google.firebase:firebase-database:11.0.4'
    implementation 'com.google.firebase:firebase-storage:11.0.4'
    implementation 'com.google.android.gms:play-services:11.0.4'
    implementation 'com.firebaseui:firebase-ui-database:2.3.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.squareup.okhttp3:okhttp:2.4.0'
    implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'
    implementation 'com.android.support:multidex:1.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}


apply plugin: 'com.google.gms.google-services'

这是我的 SimpleBlog.java 文件:

package naim.google.com.photographyblog;

import android.app.Application;

import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;

/**
 * Created by bd on 11/5/2017.
 */

public class SimpleBlog extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseDatabase.getInstance().setPersistenceEnabled(true);

        Picasso.Builder builder = new Picasso.Builder(this);
        builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
        Picasso built = builder.build();
        built.setIndicatorsEnabled(false);
        built.setLoggingEnabled(true);
        Picasso.setSingletonInstance(built);


    }
}

我认为问题出在我的主要活动中,所以这是我的 MainActivity.java 文件:

package naim.google.com.photographyblog;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mBlogList;

    private DatabaseReference mDatabase;

    private DatabaseReference mDatabaseUsers;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListner;

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

        mAuth = FirebaseAuth.getInstance();
        mAuthListner = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                if(firebaseAuth.getCurrentUser() == null)
                {
                    Intent intent = new Intent(MainActivity.this , LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                }

            }
        };

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
        mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");

        mDatabase.keepSynced(true);
        mDatabaseUsers.keepSynced(true);

        mBlogList = findViewById(R.id.blog_list);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);

        mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(layoutManager);
    }


    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListner);



        FirebaseRecyclerAdapter<Blog , BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
                Blog.class,
                R.layout.blog_row,
                BlogViewHolder.class,
                mDatabase

        ) {
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {


                viewHolder.setTitle(model.getTitle());
                viewHolder.setDescription(model.getDescription());
                viewHolder.setImage(getApplicationContext() , model.getImage());

            }
        };
        mBlogList.setAdapter(firebaseRecyclerAdapter);

    }


    private void checkUserExist()
    {

        if(mAuth.getCurrentUser()!=null)
        {
            final String user_id = mAuth.getCurrentUser().getUid();

            mDatabaseUsers.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    if(!dataSnapshot.hasChild(user_id)){

                        Intent intent = new Intent(MainActivity.this , SetupActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);

                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

    }


    public static class BlogViewHolder extends RecyclerView.ViewHolder{

        View mView;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setTitle(String title){

            TextView post_title = mView.findViewById(R.id.post_title);
            post_title.setText(title);

        }

        public void setDescription(String description){

            TextView post_description = mView.findViewById(R.id.post_description);
            post_description.setText(description);

        }

        public void setImage(Context ctx ,String image) {

            ImageView post_image = mView.findViewById(R.id.post_image);
            Picasso.with(ctx).load(image).into(post_image);

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_menu,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if(item.getItemId()==R.id.action_add)
        {
            startActivity(new Intent(MainActivity.this,PostActivity.class));
        }

        if(item.getItemId() == R.id.action_logout)
        {
            logout();
        }

        return super.onOptionsItemSelected(item);
    }


    private void logout()
    {
        mAuth.signOut();
    }

}

0 个答案:

没有答案