title: Google Drive call causes Android app to crash
body: <p>I am working on an Android app which has Google Drive but even though it compiles fine it crashes when I try and do anything. </p>
  
  <p>This is a pre-existing, it hasn't been worked on in and was originally an Eclipse project. Someone reported so imported into Android Studio and fixed the bug however when I try and do anything with Google Drive I get the following app crash. </p>
  
  <pre><code>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.&lt;init&gt;(GoogleAccountManager.java:41)
              at com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager.&lt;init&gt;(GoogleAccountManager.java:48)
              at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.&lt;init&gt;(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.&lt;init&gt;(GoogleAccountManager.java:41)
              at com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager.&lt;init&gt;(GoogleAccountManager.java:48)
              at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.&lt;init&gt;(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
  </code></pre>
  
  <p>Below is the part of the code that crashes:</p>
  
  <pre><code>private void enableGoogleDrive()
          {
              credential = GoogleAccountCredential.usingOAuth2(context, DriveScopes.DRIVE);
              startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
          }
  </code></pre>
  
  <p>Its the <code>credential = ...</code> that is causing the crash. </p>
  
  <p>Below is my build.gradle file </p>
  
  <pre><code>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'
      }
  
  }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I've found there is was a jar library I was missing. I've added this jar called google-collect-1.0.jar and rebuilt. When I run the above code it now works and I get a list of Google accounts that I select. However, when it then tries to do something with Drive I now get a different error. </p>
  
  <p><code>FileList file = service.files().list().execute();</code></p>
  
  <p>On the above line is what causes the crash. service is created by calling </p>
  
  <pre><code>credential.setSelectedAccountName(settings.getString("google_drive_account_name", ""));
                      Drive service = getDriveService(credential);
  </code></pre>
  
  <p>getDriveService is as follows:</p>
  
  <pre><code>private Drive getDriveService(GoogleAccountCredential credential)
      {
          return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
      }
  </code></pre>
  
  <p>The error I now get is:</p>
  
  <pre><code>java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/primitives/UnsignedInteger;
              at com.google.api.client.util.Data.&lt;clinit&gt;(Data.java:81)
              at com.google.api.client.util.FieldInfo.&lt;init&gt;(FieldInfo.java:131)
              at com.google.api.client.util.FieldInfo.of(FieldInfo.java:104)
              at com.google.api.client.util.ClassInfo.&lt;init&gt;(ClassInfo.java:174)
              at com.google.api.client.util.ClassInfo.of(ClassInfo.java:92)
              at com.google.api.client.util.GenericData.&lt;init&gt;(GenericData.java:79)
              at com.google.api.client.util.GenericData.&lt;init&gt;(GenericData.java:61)
              at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.&lt;init&gt;(AbstractGoogleClientRequest.java:101)
              at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.&lt;init&gt;(AbstractGoogleJsonClientRequest.java:57)
              at com.google.api.services.drive.DriveRequest.&lt;init&gt;(DriveRequest.java:51)
              at com.google.api.services.drive.Drive$Files$List.&lt;init&gt;(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.&lt;clinit&gt;(Data.java:81)
              at com.google.api.client.util.FieldInfo.&lt;init&gt;(FieldInfo.java:131)
              at com.google.api.client.util.FieldInfo.of(FieldInfo.java:104)
              at com.google.api.client.util.ClassInfo.&lt;init&gt;(ClassInfo.java:174)
              at com.google.api.client.util.ClassInfo.of(ClassInfo.java:92)
              at com.google.api.client.util.GenericData.&lt;init&gt;(GenericData.java:79)
              at com.google.api.client.util.GenericData.&lt;init&gt;(GenericData.java:61)
              at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.&lt;init&gt;(AbstractGoogleClientRequest.java:101)
              at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.&lt;init&gt;(AbstractGoogleJsonClientRequest.java:57)
              at com.google.api.services.drive.DriveRequest.&lt;init&gt;(DriveRequest.java:51)
              at com.google.api.services.drive.Drive$Files$List.&lt;init&gt;(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
  </code></pre>
  
  <h2>Update 2</h2>
  
  <p>I think I have made some progress but I'm still getting a crash although different now. </p>
  
  <p>Below is how my gradle file now looks</p>
  
  <pre><code>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') {
  
      }
  }
  </code></pre>
  
  <p>Below is a screenshot of what my libs directory contains</p>
  
  <p><img src="https://i.stack.imgur.com/cpO8D.png" alt="Libs directory of project"></p>
  
  <p>The error that I am now getting is:</p>
  
  <pre><code>Process: com.BoardiesITSolutions.PasswordManager, PID: 25495
      java.lang.NoSuchMethodError: No direct method &lt;init&gt;(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.&lt;init&gt;(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)
  </code></pre>
  
  <p>I cannot see what is wrong with this at all, everything I try seems to result in the same problem. Everything compiles fine its just when the code executes it crashes. </p>
  
  <p>The line that it crashes on now is below:</p>
  
  <pre><code>return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
  </code></pre>