android – java.lang.NoClassDefFoundError retrofit2.Utils

我正在使用Retrofit来处理来自Mobile的Serverside数据.实施改造后,我得到以下例外.

我究竟做错了什么?

com.name.App_idea W/System.err: java.lang.NoClassDefFoundError:
retrofit2.Utils
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:434)
at com.name.App_idea.utils.Idea.onCreate(Idea.java:103)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4541)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)

改造初始

mRetrofit = new Retrofit.Builder()
                    .baseUrl(AppConstance.APP_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(getOkHttpClient())
                    .build();

Gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "com.name.App_idea"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "9"
        multiDexEnabled true


    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
    compile 'com.android.support:support-v4:23.3.0'
}

应用类

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.Log;

import java.io.File;
import java.security.cert.CertificateException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class Idea extends Application {
    public static Retrofit mRetrofit;
    public static IdeaService Iservice;


    public static LoginResponceModel loinResponce;
    public static SettingsModel settingModel;

    public static LocationModel location = new LocationModel();

    private static SQLiteDatabase dbase;
    private static String FILE_PATH;

    public static SQLiteDatabase getDataBase() {
        return dbase;
    }

    public static String getFilePath() {
        return FILE_PATH;
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, "App", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(AppConstance.DbConstans.tblLogin);
            Log.i("DB", "Created");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onCreate(db);
        }
    }

    public static void deleteAllTables() {
        getDataBase().execSQL("DELETE FROM login");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(AppConstance.APP_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(getOkHttpClient())
                    .build();
            Iservice = mRetrofit.create(IdeaService.class);
            MultiDex.install(this);
            DatabaseHelper dbHelper = new DatabaseHelper(this);
            dbase = dbHelper.getWritableDatabase();

            AppDataService appDataService = new AppDataService();
            loinResponce = appDataService.getLoginDetails();
            settingModel = appDataService.getSettings();

            FILE_PATH = getAppFilePath();
            startService(new Intent(Idea.this, LocationTracker.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String combineFilePath(String path1, String path2) {
        File file1 = new File(path1);
        File file2 = new File(file1, path2);
        return file2.getPath();
    }

    public String getAppFilePath() {

        String dsPath;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED))
            dsPath = combineFilePath(Environment
                            .getExternalStorageDirectory().getAbsolutePath(),
                    "android/data/Idea/");
        else
            dsPath = this.getDir(
                    this.getPackageName(), 0).getAbsolutePath();

        new File(dsPath).mkdirs();
        return dsPath;
    }

    private OkHttpClient getOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };
            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory);
            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

解决方法:

这是Gradle bulid配置问题我也有同样的问题

在Gradle你的使用

compile 'com.google.android.gms:play-services:8.4.0'

Enire Google Playservice Lib可以更改您的项目中使用的lib

com.google.android.gms:play-services-gcm:8.4.0 
com.google.android.gms:play-services-maps:8.4.0
com.google.android.gms:play-services-auth:8.4.0

请参阅此URl https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project

上一篇:java – Retrofit:500内部服务器错误


下一篇:java – Retrofit不适用于特定版本的android