Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Play, Drive API sample code in IntelliJ

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 :)

like image 889
2 revs, 2 users 97% Avatar asked Apr 07 '13 01:04

2 revs, 2 users 97%


People also ask

Is there a Google Drive API?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.


1 Answers

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.

like image 106
CrazyCoder Avatar answered Oct 11 '22 23:10

CrazyCoder