Google云端硬盘通话会导致Android应用崩溃

时间:2015-03-25 18:30:06

标签: android google-drive-android-api

我正在开发一款拥有Google云端硬盘的Android应用,但即使它编译得很好,它也会在我尝试做任何事情时崩溃。

这是一个预先存在的,它尚未进行过,最初是一个Eclipse项目。有人报告如此导入Android Studio并修复了该错误,但是当我尝试对Google云端硬盘做任何事情时,我得到以下应用程序崩溃。

Process: com.BoardiesITSolutions.PasswordManager, PID: 23121
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/base/Preconditions;
            at com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager.<init>(GoogleAccountManager.java:41)
            at com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager.<init>(GoogleAccountManager.java:48)
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.<init>(GoogleAccountCredential.java:78)
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.usingOAuth2(GoogleAccountCredential.java:97)
            at com.BoardiesITSolutions.PasswordManager.Settings$SynchronisationSettings.enableGoogleDrive(Settings.java:575)
            at com.BoardiesITSolutions.PasswordManager.Settings$SynchronisationSettings.access$000(Settings.java:420)
            at com.BoardiesITSolutions.PasswordManager.Settings$SynchronisationSettings$1.onPreferenceChange(Settings.java:510)
            at android.preference.Preference.callChangeListener(Preference.java:928)
            at android.preference.ListPreference.onDialogClosed(ListPreference.java:281)
            at android.preference.DialogPreference.onDismiss(DialogPreference.java:391)
            at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.base.Preconditions" on path: DexPathList[[zip file "/data/app/com.BoardiesITSolutions.PasswordManager-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager.<init>(GoogleAccountManager.java:41)
            at com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager.<init>(GoogleAccountManager.java:48)
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.<init>(GoogleAccountCredential.java:78)
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.usingOAuth2(GoogleAccountCredential.java:97)
            at com.BoardiesITSolutions.PasswordManager.Settings$SynchronisationSettings.enableGoogleDrive(Settings.java:575)
            at com.BoardiesITSolutions.PasswordManager.Settings$SynchronisationSettings.access$000(Settings.java:420)
            at com.BoardiesITSolutions.PasswordManager.Settings$SynchronisationSettings$1.onPreferenceChange(Settings.java:510)
            at android.preference.Preference.callChangeListener(Preference.java:928)
            at android.preference.ListPreference.onDialogClosed(ListPreference.java:281)
            at android.preference.DialogPreference.onDismiss(DialogPreference.java:391)
            at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
    Suppressed: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 19 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

以下是崩溃代码的一部分:

private void enableGoogleDrive()
        {
            credential = GoogleAccountCredential.usingOAuth2(context, DriveScopes.DRIVE);
            startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
        }

导致崩溃的credential = ...

下面是我的build.gradle文件

apply plugin: 'com.android.application'

android {
    compileOptions.encoding = 'ISO-8859-1'
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    defaultConfig {
        applicationId "com.BoardiesITSolutions.PasswordManager"
        minSdkVersion 14
        targetSdkVersion 21
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_5
            targetCompatibility JavaVersion.VERSION_1_5
        }
    }

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

    repositories {
        mavenLocal()
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.google.android.gms:play-services:7.0.0') {
        exclude module: 'support-v4'
    }
    compile files('libs/commons-codec-1.4.jar');

    compile files('libs/google-api-services-drive-v2-rev30-1.12.0-beta.jar')

    compile ('com.android.support:appcompat-v7:21.0.0') {
        exclude module: 'support-v4'
    }
    //compile 'com.android.support:support-v4:21.0.0'
    compile ('com.BoardiesITSolutions:CritiMon:1.0') {
        exclude module: 'support-v4'
    }
    compile ('com.BoardiesITSolutions:Library:1.1') {
        exclude module: 'support-v4'
    }

}

更新

我发现有一个我失踪的jar库。我添加了名为google-collect-1.0.jar的jar并重建。当我运行上面的代码时,它现在可以工作了,我得到了一个我选择的Google帐户列表。然而,当它尝试使用Drive I做某事时,我现在得到一个不同的错误。

FileList file = service.files().list().execute();

在上面这一行是导致崩溃的原因。通过调用

创建服务
credential.setSelectedAccountName(settings.getString("google_drive_account_name", ""));
                    Drive service = getDriveService(credential);

getDriveService如下:

private Drive getDriveService(GoogleAccountCredential credential)
    {
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
    }

我现在得到的错误是:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/primitives/UnsignedInteger;
            at com.google.api.client.util.Data.<clinit>(Data.java:81)
            at com.google.api.client.util.FieldInfo.<init>(FieldInfo.java:131)
            at com.google.api.client.util.FieldInfo.of(FieldInfo.java:104)
            at com.google.api.client.util.ClassInfo.<init>(ClassInfo.java:174)
            at com.google.api.client.util.ClassInfo.of(ClassInfo.java:92)
            at com.google.api.client.util.GenericData.<init>(GenericData.java:79)
            at com.google.api.client.util.GenericData.<init>(GenericData.java:61)
            at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.<init>(AbstractGoogleClientRequest.java:101)
            at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.<init>(AbstractGoogleJsonClientRequest.java:57)
            at com.google.api.services.drive.DriveRequest.<init>(DriveRequest.java:51)
            at com.google.api.services.drive.Drive$Files$List.<init>(Drive.java:2914)
            at com.google.api.services.drive.Drive$Files.list(Drive.java:2904)
            at com.BoardiesITSolutions.PasswordManager.Classes.DriveSync$1.run(DriveSync.java:137)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.primitives.UnsignedInteger" on path: DexPathList[[zip file "/data/app/com.BoardiesITSolutions.PasswordManager-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.google.api.client.util.Data.<clinit>(Data.java:81)
            at com.google.api.client.util.FieldInfo.<init>(FieldInfo.java:131)
            at com.google.api.client.util.FieldInfo.of(FieldInfo.java:104)
            at com.google.api.client.util.ClassInfo.<init>(ClassInfo.java:174)
            at com.google.api.client.util.ClassInfo.of(ClassInfo.java:92)
            at com.google.api.client.util.GenericData.<init>(GenericData.java:79)
            at com.google.api.client.util.GenericData.<init>(GenericData.java:61)
            at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.<init>(AbstractGoogleClientRequest.java:101)
            at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.<init>(AbstractGoogleJsonClientRequest.java:57)
            at com.google.api.services.drive.DriveRequest.<init>(DriveRequest.java:51)
            at com.google.api.services.drive.Drive$Files$List.<init>(Drive.java:2914)
            at com.google.api.services.drive.Drive$Files.list(Drive.java:2904)
            at com.BoardiesITSolutions.PasswordManager.Classes.DriveSync$1.run(DriveSync.java:137)
            at java.lang.Thread.run(Thread.java:818)
    Suppressed: java.lang.ClassNotFoundException: com.google.common.primitives.UnsignedInteger
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 15 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

更新2

我想我已经取得了一些进展,但我现在仍然遇到了崩溃,尽管现在有所不同。

以下是我的gradle文件现在的样子

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}

apply plugin: 'com.android.application'

android {
    compileOptions.encoding = 'ISO-8859-1'
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'README'
        exclude 'com.google.api.client.json.JsonFactory.class'
        //exclude 'GwtCompatible.class'
        /*exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/notice.txt'
        exclude 'com.google.api.client.auth.oauth2.AuthorizationCodeFlow.class'

    }

    defaultConfig {
        applicationId "com.BoardiesITSolutions.PasswordManager"
        minSdkVersion 14
        targetSdkVersion 21
    }

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

    repositories {
        mavenLocal()
        mavenCentral()
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile('com.google.android.gms:play-services:7.0.0') {
        exclude module: 'support-v4'
    }

    compile('com.google.http-client:google-http-client:1.18.0-rc') {
        exclude group: 'xpp3'
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'junit'
        exclude group: 'com.google.android'
    }
    compile('com.google.http-client:google-http-client-jackson2:1.18.0-rc')



    compile('com.android.support:appcompat-v7:21.0.0') {
        exclude module: 'support-v4'
    }

    compile('com.BoardiesITSolutions:CritiMon:1.0') {
        exclude module: 'support-v4'
    }
    compile('com.BoardiesITSolutions:Library:1.1') {

    }
    compile ('com.google.api-client:google-api-client-android:1.18.0-rc') {

    }
    compile ('com.google.http-client:google-http-client-gson:1.18.0-rc') {

    }
}

下面是我的libs目录包含的截图

Libs directory of project

我现在得到的错误是:

Process: com.BoardiesITSolutions.PasswordManager, PID: 25495
    java.lang.NoSuchMethodError: No direct method <init>(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/http/HttpRequestInitializer;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/json/JsonObjectParser;Lcom/google/api/client/googleapis/services/GoogleClientRequestInitializer;Ljava/lang/String;Z)V in class Lcom/google/api/client/googleapis/services/json/AbstractGoogleJsonClient; or its super classes (declaration of 'com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient' appears in /data/app/com.BoardiesITSolutions.PasswordManager-2/base.apk)
            at com.google.api.services.drive.Drive.<init>(Drive.java:135)
            at com.google.api.services.drive.Drive$Builder.build(Drive.java:6542)
            at com.BoardiesITSolutions.PasswordManager.Classes.DriveSync.getDriveService(DriveSync.java:365)
            at com.BoardiesITSolutions.PasswordManager.Classes.DriveSync.access$000(DriveSync.java:65)
            at com.BoardiesITSolutions.PasswordManager.Classes.DriveSync$1.run(DriveSync.java:134)
            at java.lang.Thread.run(Thread.java:818)

我根本看不出这有什么问题,我尝试的一切似乎都会导致同样的问题。当代码执行崩溃时,一切都很好。

它崩溃的线路如下:

return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();

1 个答案:

答案 0 :(得分:1)

我终于想出了一个解决方案。下面是我的libs文件夹中的库的截图

libs folder

以下是我的gradle文件

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}

apply plugin: 'com.android.application'

android {
    compileOptions.encoding = 'ISO-8859-1'
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'com.google.gson.JsonSerializer'
        exclude 'constant-values.html'
        exclude 'overview-summary.html'
        exclude 'package-list'
        exclude 'overview-tree.html'
        exclude 'allclasses-frame.html'
        exclude 'allclasses-noframe.html'
        exclude 'stylesheet.css'
        exclude 'index-all.html'
        exclude 'deprecated-list.html'
        exclude 'index.html'
        exclude 'overview-frame.html'
        exclude 'help-doc.html'
    }

    defaultConfig {
        applicationId "com.BoardiesITSolutions.PasswordManager"
        minSdkVersion 14
        targetSdkVersion 21
        multiDexEnabled = true
    }

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

    repositories {
        mavenLocal()
        mavenCentral()
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
}

dependencies {
    compile ('com.google.android.gms:play-services-ads:7.0.0') {
        exclude module: 'support-v4'
    }
    compile ('com.google.android.gms:play-services-drive:7.0.0') {
        exclude module: 'support-v4'
    }
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile('com.google.android.gms:play-services:7.0.0') {
        exclude module: 'support-v4'
    }
    compile 'com.google.guava:guava:17.0'

    compile('com.android.support:appcompat-v7:21.0.0') {
        exclude module: 'support-v4'
    }

    compile('com.BoardiesITSolutions:CritiMon:1.0') {
        exclude module: 'support-v4'
    }
    compile('com.BoardiesITSolutions:Library:1.1') {

    }
}