java.lang.ClassNotFoundException 在 API 16 到 19 的 AsyncTask 中

java.lang.ClassNotFoundException in AsyncTask on API 16 to 19

我有一个错误只发生在 android 4(API 16 到 19)。

我使用下面的代码

private class AcceptBooking extends AsyncTask<String, Void, String> {
    int response;

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(String s) {
        switch (response){
            case NO_UPDATE:
                startApp();
                break;
            case SUGEST_UPDATE:
                adviceUpdate();
                break;
            case FORCE_UPDATE:
                forceUpdate();
                break;
            default:
                startApp();
                break;
        }

    }

    @Override
    protected String doInBackground(String... params) {
        response = DBConnect.updateApp();

        return "done";
    }
}

当我尝试执行此 AsyncTask 时,出现以下错误:

com.socialcar E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.NoClassDefFoundError: com/socialcar/api_connection/DBConnect
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:154)
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:126)
    at android.os.AsyncTask.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
    at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
    at java.lang.Thread.run(Thread.java:856) 
 Caused by: java.lang.ClassNotFoundException: com.socialcar.api_connection.DBConnect
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:154) 
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:126) 
    at android.os.AsyncTask.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
    at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
    at java.lang.Thread.run(Thread.java:856) 

这是我在 gradle 中的依赖项:

compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'org.apache.httpcomponents:httpmime:4.2.3'
    compile 'com.google.guava:guava-jdk5:17.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'com.android.support:cardview-v7:23.0.+'
    compile 'com.android.support:recyclerview-v7:23.0.+'

我试图在这个论坛和其他论坛中找到解决方案,显然 com.android.support:appcompat-v7 版本可能是问题所在,但更改它是行不通的。而且我 java.util.objects 是在 Java 7 中引入的,这也会影响,但我不确定如何解决这个问题。

欢迎提出任何其他想法。

编辑

这是给出错误的 BDConnect class:

import android.content.pm.PackageInfo;
import android.os.Build;

import com.socialcar.BuildConfig;
import com.socialcar.Config;
import com.socialcar.booking.NavigationActivity;
import com.socialcar.custom.SHA1;
import com.socialcar.http.RequestMethod;
import com.socialcar.http.RestClient;
import com.socialcar.model.Booking;
import com.socialcar.model.BookingList;
import com.socialcar.model.LoginResult;
import com.socialcar.model.Token;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by PcCom1 on 14/03/2016.
 */
public class DBConnect {
    private static String URL = "https://someurl.com/";
    private static String SOCIAL_CAR_URL_OAUTH = URL + "test/auth";
    private static String SOCIAL_CAR_URL = URL + "test";
    private static String SOCIAL_CAR_URL_NO_V1 = URL + "test";
    private static String SOCIAL_CAR_URL_LOGIN = URL + "test/login";


    private static void checkResponseCode(RestClient restClient) throws IOException, IllegalStateException {
        int responseCode = restClient.getResponseCode();
        if (responseCode == 500 || responseCode == 503) {
            // ERRO no servidor
        } else if (responseCode == 404) {
            // ERRO pagina nao encontrada
        } else if (responseCode == 401) {
            if (restClient.getResponse().contains("Invalid JWT")){
                throw new IOException("JWT");
            }
        } else if (responseCode != 200) {
            // ERRO se a resposta n?o for a correta
            throw new IOException();
        }

    }

    public static int updateApp(){
        String response = "";
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL+"/mobile/updates");
            String model = Build.MODEL;
            int version = Build.VERSION.SDK_INT;
            int versionCode = BuildConfig.VERSION_CODE;
            JSONObject data = new JSONObject();
            data.put("mobile", model);
            data.put("sdk-version", version);
            data.put("version-code", versionCode);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST);
            checkResponseCode(restClient);
            response = restClient.getResponse();
            JSONObject jsonObject = new JSONObject(response);
            return jsonObject.getInt("update");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

    public static Token refreshToken(String refreshToken){
        String response = "";
        Token token = new Token();
        try {
            //password = SHA1.toSHA1(password);
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_NO_V1+"/token-refresh");
            JSONObject data = new JSONObject();
            data.put("_token", refreshToken);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Error de conexión
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(response);
            token.setToken(jsonObject.getString("token"));
            token.setRefreshToken(jsonObject.getString("refresh_token"));
            return token;
        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            return null;
        }
    }

    public static int logout(String email, String registration_id, String token){
        String response = "";
        try {
            //password = SHA1.toSHA1(password);
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/user/logouts", token, null);
            JSONObject data = new JSONObject();
            data.put("_username", email);
            data.put("device_token", registration_id);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST_TOKEN);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Error de conexión
            return -999;
        }
        try {
            JSONObject jsonObject = new JSONObject(response);
            return jsonObject.getInt("error");

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            return -1000;
        }
    }

    public static Token login(String email, String password){
        String response = "";
        Token token = new Token();
        try {
            //password = SHA1.toSHA1(password);
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_NO_V1+"/login-check");
            JSONObject data = new JSONObject();
            data.put("_username", email);
            data.put("_password", password);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Error de conexión
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(response);
            token.setToken(jsonObject.getString("token"));
            if(jsonObject.has("refresh_token"))
                token.setRefreshToken(jsonObject.getString("refresh_token"));
            return token;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            return null;
        }
    }

    public static LoginResult getUserInfo(String regId, Token token){
        String response = "";
        LoginResult loginResult;
        try {

            RestClient restClient = new RestClient(
                    SOCIAL_CAR_URL_OAUTH+"/user/info?device_token=" + regId,
                    token.getToken(), null);
            restClient.execute(RequestMethod.GET_TOKEN);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            loginResult = new LoginResult();
            // Error de conexión
            loginResult.setError(999);
            return loginResult;
        }
        try {

            loginResult  = JSONParser.getLoginResultFromJson(response);


            return loginResult;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            loginResult = new LoginResult();
            loginResult.setError(1000);
            return loginResult;
        }
    }

    public static Token loginFacebook(String facebookId, String facebookToken, String email, String regId){
        String response = "";
        Token token = new Token();
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_LOGIN+"/facebooks", null, facebookId);
            JSONObject data = new JSONObject();
            data.put("email", email);
            data.put("facebook_id", facebookId);
            data.put("token-facebook", facebookToken);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST_OAUTH);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.has("token")){
                token.setToken(jsonObject.getString("token"));
                token.setRefreshToken(jsonObject.getString("refresh_token"));
                token.setError(0);
                return token;
            }
            token.setError(jsonObject.getInt("error"));
            return token;


        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            return null;
        }
    }

    public static Token loginGooglePlus(String googleId, String googleToken, String email, String regId){
        String response = "";
        Token token = new Token();
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_LOGIN+"/googles", null, googleId);
            JSONObject data = new JSONObject();
            data.put("email", email);
            data.put("google_id", googleId);
            data.put("token_google", googleToken);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST_OAUTH);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        try {

            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.has("token")){
                token.setToken(jsonObject.getString("token"));
                token.setRefreshToken(jsonObject.getString("refresh_token"));
                token.setError(0);
                return token;
            }
            token.setError(jsonObject.getInt("error"));
            return token;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            return null;
        }
    }

    public static BookingList getPendingBookingList(int ownerId, String token){
        String response = "";
        BookingList bookingList;
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/pendings/"+ownerId, token, null);
            restClient.execute(RequestMethod.GET_TOKEN);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if(e.getMessage() == "JWT"){
                bookingList = new BookingList();
                // Error de conexión
                bookingList.setError(Config.JWT_EXPIRED);
                bookingList.setBookings(new ArrayList<Booking>());
                return bookingList;
            }
            bookingList = new BookingList();
            // Error de conexión
            bookingList.setError(999);
            bookingList.setBookings(new ArrayList<Booking>());
            return bookingList;
        }
        try {

            bookingList  = JSONParser.getBookingListFromJson(response);
            return bookingList;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            bookingList = new BookingList();
            bookingList.setError(1000);
            bookingList.setBookings(new ArrayList<Booking>());
            return bookingList;
        }
    }

    public static Booking getBookingDetailsOwner(int booking_line, String token){
        String response = "";
        Booking booking;
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/owner_details/"+booking_line, token, null);
            restClient.execute(RequestMethod.GET_TOKEN);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if(e.getMessage() == "JWT"){
                booking = new Booking();
                // Error de conexión
                booking.setError(Config.JWT_EXPIRED);
                return booking;
            }
            booking = new Booking();
            // Error de conexión
            booking.setError(999);
            return booking;
        }
        try {

            booking  = JSONParser.getBookingFromJson(response);


            return booking;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            booking = new Booking();
            //loginResult.setError(1000);
            return booking;
        }
    }

    public static int cancelBookingByOwner(int booking_line, String reason, String description, String token){
        String response = "";
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/cancels/"+booking_line, token, null);
            JSONObject data = new JSONObject();
            data.put("reason", reason);
            data.put("reason_description", description);
            restClient.setJsonObject(data);
            restClient.execute(RequestMethod.POST_TOKEN);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if(e.getMessage() == "JWT") {
                return Config.JWT_EXPIRED;
            }
            // Error de conexión
            return 999;
        }
        try {

            int error  = JSONParser.getErrorResultFromJson(response);


            return error;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            //loginResult.setError(1000);
            return 1000;
        }
    }

    public static int acceptBookingByOwner(int booking_line, String token){
        String response = "";
        try {
            RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/accepts/"+booking_line, token, null);
            restClient.execute(RequestMethod.GET_TOKEN);
            checkResponseCode(restClient);
            response = restClient.getResponse();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Error de conexión
            return 999;
        }
        try {
            int error  = JSONParser.getErrorResultFromJson(response);
            return error;

        } catch (Exception e) {
            //error desconocido de parseo
            e.printStackTrace();
            if(e.getMessage() == "JWT") {
                return Config.JWT_EXPIRED;
            }
            //loginResult.setError(1000);
            return 1000;
        }
    }

}

和 gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.socialcar"
        minSdkVersion 16
        targetSdkVersion 23
        multiDexEnabled true
        versionCode 33
        versionName "3.0.1"
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    packagingOptions {
        exclude 'META-INF/NOTICE.txt' // will not include NOTICE file
        exclude 'META-INF/LICENSE.txt' // will not include LICENSE file
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:17.2.0'
    compile 'com.android.support:design:23.2.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'org.apache.httpcomponents:httpmime:4.2.3'
    compile 'com.google.guava:guava-jdk5:17.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'com.android.support:cardview-v7:23.0.+'
    compile 'com.android.support:recyclerview-v7:23.0.+'
}

清单:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".login.LoginActivity"
            android:label="@string/app_name"
            android:configChanges="orientation"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".booking.NavigationActivity"
            android:label="@string/action_booking"
            android:theme="@style/AppTheme.NoActionBar"
            android:configChanges="orientation"
            android:screenOrientation="portrait">
        </activity>

        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name"
            android:screenOrientation="portrait"/>

        <receiver
            android:name="com.socialcar.gcm.GcmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.socialcar"/>
            </intent-filter>
        </receiver>
        <service
            android:name="com.socialcar.gcm.GcmMessageHandler"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
    </application>

在 build.gradle 中添加此行:

compile 'com.android.support:multidex:1.0.1'

现在,在您的 mainfest 的应用程序标签内添加这一行,

android:name="android.support.multidex.MultiDexApplication"

然后创建以下 class:

public class MultiDexApplication extends Application { 
public MultiDexApplication() { } 
    protected void attachBaseContext(Context base) {   super.attachBaseContext(base); 
MultiDex.install(this);
} 
    }