6

I'm following the Google Drive Quickstart on Android instructions and have it working in Eclipse/Kepler. (Juno is just plain dodgy.) However, I'm much more familiar with IntelliJ, so I'm trying to figure out how to install these APIs there in a corresponding manner.

I think I'm getting tripped up on the Drive API part. The Eclipse plugin installs Drive in some special way or place. I added Google-api-services-drive-v2-rev63-1.14.1-beta.jar as a library, same as the others (Google-play-services and the api_java_client). But when I run the application it comes back with

java.lang.NoClassDefFoundError: com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential

And I've also gotten a NoClassDefFoundError having to do with Drive$Builder.

In IntelliJ the .jar scopes are set to "Compile" except for the api_java_client libraries which are set to "Provided." None of them have "Export" checked.

Edit: added the module & dependency. I now have a different error:

04-07 00:39:59.766: ERROR/AndroidRuntime(10697): FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError:
com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
    at com.fallinghawks.weight4.App.onCreate(App.java:15)

App.java:15 is

credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);

I can find the class it's asking for....

Edit 2, after adding libraries per CrazyCoder's instructions: It compiles, it runs, but I get the following error. I am sure I missed doing something exactly right, I'll try it again in a minute but in the meantime the error is:

04-10 18:02:35.120: ERROR/AndroidRuntime(740): FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
    at com.google.api.services.drive.Drive$Builder.build(Drive.java:7301)
    at com.example.DriveQuickstart.MyActivity.getDriveService(MyActivity.java:110)
    at com.example.DriveQuickstart.MyActivity.onActivityResult(MyActivity.java:49)
    at android.app.Activity.dispatchActivityResult(Activity.java:5293)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
    at android.app.ActivityThread.access$1100(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NoClassDefFoundError: com.google.common.base.Preconditions
    at com.google.api.services.drive.Drive.<clinit>(Drive.java:63)

(Hopefully final edit): Yes, I missed something and the beast is working! Thank you so much!!
Now I can use IntelliJ again to work on my "real" project... sooooo much better :)

Andrea
  • 214
  • 3
  • 8
  • You have to import `ANDROID_SDK\extras\google\google_play_services\libproject\google-play-services_lib` as a new module in IDEA, then set your main module to [depend on it](http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html). **Export** checkbox must be enabled for the library jars inside this library module. – CrazyCoder Apr 07 '13 at 07:04
  • Thanks for the reply @CrazyCoder. I now have a different error which I'll add to the OP. – Andrea Apr 07 '13 at 07:41
  • Looks like some jars from https://code.google.com/p/google-api-java-client/ are missing in the module dependencies. – CrazyCoder Apr 07 '13 at 08:03
  • Do I need to add that in the same way as the play services? As a dependent module? It shouldn't be missing anything; it's exactly as downloaded, but maybe I'm not adding it to the project in the right way. – Andrea Apr 07 '13 at 16:23
  • Just add a library jar, not a module. – CrazyCoder Apr 07 '13 at 16:39
  • That's the way it was, with "Provided" checked. Thanks for taking the time, though. – Andrea Apr 07 '13 at 19:05
  • `Provided` is wrong, `google-api-java-client` is not pre-installed on Android, you must set the scope to **Compile**. – CrazyCoder Apr 07 '13 at 19:09
  • When I set it to compile, it gives me a rather lengthy error about how I am using a core library and could, but shouldn't, use the --core-library option to suppress the error. IOW I shouldn't be including it. – Andrea Apr 07 '13 at 20:37
  • Please share a sample project to reproduce the problem so that we can verify your configuration. – CrazyCoder Apr 08 '13 at 07:48
  • If one follows the guide he can end up with a different configuration, you can save us all a lot of time by sharing YOUR project configuration. – CrazyCoder Apr 09 '13 at 08:26
  • CrazyCoder, that is my original question. I am trying to find out if someone out there ACTUALLY KNOWS the analogous configuration in IntelliJ. If I were creating the OP project in IntelliJ, which libraries should be added and how? I spent over 3 days adding different libraries different ways before throwing in the towel and getting it to work in Eclipse. I don't think I could list every configuration I tried. I am new to this kind of programming (Java/libraries/modules etc) and am trying to understand how they work. – Andrea Apr 09 '13 at 18:54
  • [I use Google Drive in my project and this answer helped me to fix errors in Android Studio.][1] [1]: http://stackoverflow.com/questions/21229447/drivequickstart-sample-project-in-android-studio/21350589#21350589 – Michal Jul 26 '14 at 09:33

1 Answers1

6

I went ahead and created IntelliJ IDEA project from scratch, spent about 15 minutes, works just fine.

  1. Download Drive API v2 library and dependencies.

  2. Unpack it somewhere, open readme.html, spend a couple of minutes reading it to understand what jars are needed.

  3. Create a new Android application project in IDEA using a wizard with com.example.drivequickstart package and MainActivity. Use Android 4.2.2 Google APIs as the Platform.

  4. Copy the following jars from the downloaded Drive API library into the project libs folder (you should have learned which jars to use from the step 2):

    • google-api-client-1.14.1-beta.jar
    • google-api-client-android-1.14.1-beta.jar
    • google-http-client-1.14.1-beta.jar
    • google-http-client-android-1.14.1-beta.jar
    • google-http-client-gson-1.14.1-beta.jar
    • google-oauth-client-1.14.1-beta.jar
    • gson-2.1.jar
    • jsr305-1.3.9.jar
    • google-api-services-drive-v2-rev65-1.14.1-beta.jar
  5. Configure a project library from these jars. It can be done by selecting the jars in the Project View, right click, Add as Library..., add this library to the dependencies of your main module.

  6. File | Import Module, browse to ANDROID_SDK\extras\google\google_play_services\libproject\google-play-services_‌​lib, from the existing sources.

  7. Add google-play-services.jar to the dependencies of the imported module, enable the Export checkbox, it should look like this.

  8. Add the module created in step 6 to the dependencies of your main application module.

  9. Copy MainActivity sample code from the Quick Start, step 4 into the MainActivity.java file replacing the old code, edit AndroidManifest.xml as well per the guide.

  10. Build, run in the emulator or on the device to test (don't forget steps 1-2 from the Quick Start).


Or you can just download and use the completely isolated sample that I've created following the above steps (module from the step 6 is included in the project).

It failed for me in the emulator because of some Camera app error (the sample uses Camera and my emulator didn't have it configured), but worked on the Galaxy Nexus device. The sample takes a picture using the device camera and uploads it to your Google Drive.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 1
    You are FANTASTIC! This is way more convoluted than I ever would have imagined... still have one funky thing going on, I'll put it in the OP – Andrea Apr 11 '13 at 01:03