How to import Google API in Android Studio - android

I'm trying out Android Studio. I want to use Drive API in my project. In eclipse, there is a Google Plugin for Eclipse, but how about Android Studio? Does anyone tried it so far?

Bellow you can find last versions for Google Drive (2014.12.28):
//Google Drive API
compile 'com.google.android.gms:play-services:6.5.+'
compile 'com.google.api-client:google-api-client-xml:1.18.0-rc'
compile 'com.google.http-client:google-http-client-gson:1.18.0-rc'
compile 'com.google.api-client:google-api-client-android:1.18.0-rc'
compile 'com.google.apis:google-api-services-drive:v2-rev155-1.19.0'
To check last version try following links:
https://developer.android.com/google/play-services/setup.html
https://code.google.com/p/google-api-java-client
https://developers.google.com/api-client-library/java/apis/drive/v2
http://mvnrepository.com/artifact/com.google.apis/google-api-services-drive

I was in the same situation and had to find here an there information on how Gradle works to find the right set of dependencies and exclude needed.
Here the lines needed in the dependencies of your build.gradle file
// Replace 'dependencies' in your build.gradle file with the following
// or add these to whatever other dependencies you have.
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.google.android.gms:play-services:4.0.30'
compile('com.google.api-client:google-api-client-xml:1.17.0-rc') {
exclude group: 'com.google.android.google-play-services'
}
compile 'com.google.http-client:google-http-client-gson:1.17.0-rc'
compile('com.google.api-client:google-api-client-android:1.17.0-rc') {
exclude group: 'com.google.android.google-play-services'
}
compile 'com.google.apis:google-api-services-drive:v2-rev105-1.17.0-rc'
}
As they keep changing in time, I've made a gist that I'll keep updated as things changes.
I've wrote an article to modify the Google Drive Quick Start to make it work with Android Studio; if interested you can find it here
EDIT: This Gradle Imports are to use the Google Drive API for Java, not the Developer Preview Google Drive API integrated with the Google Play Services.

The Java client library supports Android, as well. You can download it here: https://code.google.com/p/google-api-java-client/wiki/APIs#Drive_API
Then, unzip the Drive SDK download and move the folder into the libs section of your Project. For example:
/Users/-username-/AndroidStudioProjects/MyProject/MyProjectActivity/libs/
At this point, you can add the library to your project by clicking File -> Project Structure, and then clicking the Libraries tab, and the + sign to add the SDK into your project.
You can read the Android-specific development instructions for the Google API Client Library for Java here: https://code.google.com/p/google-api-java-client/wiki/Android

Did you try
Go to Project Structure > Global Libraries / Libraries > Link to the jar of the API you need in the SDK folder
Link the library with your module

I haven't tried google driver, but I tried google usb accessory api. In my case
Open MyProject/MyProject/build.gradle
add
compile files("libs/usb.jar")
to the dependencies block
Of course, copy the google driver libs from
android-studio\sdk\add-ons\addon-google_apis-google-10\libs\usb.jar
to MyProject/MyProject/libs
Good luck to you.

smokybob's answer worked but then I did some experiments and this also worked for me.
dependencies {
compile files ('libs/libGoogleAnalyticsServices.jar')
compile ('joda-time:joda-time:2.3')
compile ('com.google.code.gson:gson:2.2.4')
compile 'com.google.android.gms:play-services:4.1.+'
}
I am guessing the joda-time and Gson have nothing to do with the drive API.
Note when I do a
gradle dependancies
I get
+--- joda-time:joda-time:2.3
+--- com.google.code.gson:gson:2.2.4
\--- com.google.android.gms:play-services:4.1.+ -> 4.1.32
\--- com.android.support:support-v4:19.0.1

From the Extra's under the SDK manager, I downloaded Google Play Services, Google Repository and Android Support Library. Thereafter, I included dependency as below, sync'd with Gradle and was able to access the APIs.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Pls. install or update the Google Repository through the SDK manager to use this dependency.
compile 'com.google.android.gms:play-services:5.0.+'
}

Related

How to update the dependency libraries (for example Apache collection libraries) in Android Studio?

I know that in order to get the latest version of Android/Google libraries, I can always go to the Android SDK Manager and update the Android Support Repository / Google Repository.
How about to update the other dependency libraries, such as the "commons-net:commons-net:20030805.205232" from Apache collection libraries? Since it is able to add these libraries from a Module's dependencies, I assume there should be a way to update it within the Android Studio IDE, but so far all the solutions I found is to download the specific .jar and put in the libs directory.
Just curious if anyone know there is a way to update these libraries from within the Android Studio IDE like the Android/Google dependencies.
Android Studio & IntelliJ have a dependency setting window themselves. Though, it does not have a "update" feature as the SDK Manager does. Reason being: Updating a library can cause bugs, errors, or complete crashes in your app.
so far all the solutions I found is to download the specific .jar and put in the libs directory
Not sure where you read that... Remote Gradle dependencies are preferred in most cases.
such as the "commons-net:commons-net:20030805.205232"
Go find what you want in Maven Central, find the most recent version, click the "Gradle" tab, the copy that into your build.gradle section.
There is no automated process for this, as far as I know, though you should be able to auto-complete version numbers via the IDE while you type it out.
When you create a project under Android Studio, it generates for your gradle files which let you use dependencies. Gradle is like maven, you can find lot of documents on google.
Under "Project" tab -> Gradle Scripts -> build.gradle (Module app), you'll find something like this :
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.journeyapps:zxing-android-embedded:3.0.2#aar'
compile 'com.android.support:support-v13:23.4.0'
compile 'com.google.zxing:core:3.2.0'
compile 'com.android.support:support-v4:23.4.0'}
So you just have to put your dependencies and gradle will download them for you.
More here.

Gmail API for Android SDK 21

I am trying to develop using the Gmail API for Android for devices with API 21.
In this tutorial: https://developers.google.com/gmail/api/quickstart/android
It says I need to have:
Android SDK packages for API 23 or later, including the latest versions of Google Repository, Android Support Library and Google Play Services.
And the dependencies example there is:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'pub.devrel:easypermissions:0.1.5'
compile('com.google.api-client:google-api-client-android:1.22.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-gmail:v1-rev47-1.22.0') {
exclude group: 'org.apache.httpcomponents'
}
}
But I get an error when I use it saying I cant use:
com.android.support:appcompat-v7:23.4.0
When my compile SDK version is 21.
I've tried changing the appcompact version to v7:21.0.2 which worked for me before, but when I sync the gradle files I get the error:
Error:
Failed to resolve: com.android.support:appcompat-v7:23.1.1
Install Repository and sync project
Open File
Show in Project Structure dialog
But installing this repository will create the problem of the support library not matching the compile version.
Am I doing something wrong?
You can try to go to SDK manager and install three component: Google Repository, Android Support Library and Google Play Services.
like following picture:

Where can i download Googles android package? (Not the apps)

Where can i download the jar file for com.android.support packages such as com.android.support:appcompat-v7 or com.android.support:design ?
I looked at mavenCentral and jCenter but there was nothing there.
EDIT:
probably get wasn't the right word to use. i want to download them and use them offline.
YOu can get it from gradle. If you are using Android Studio just go to build.gradle and add the following under depedencies
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:appcompat-v7:23.2.1'

What is the best way to find out new version is available for external libraries used in Android Studio project

I am new to Android development. so the external libraries (aka. dependencies) are defined in build.gradle like
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v13:21.0.0'
compile 'com.squareup.dagger:dagger:1.2.1'
compile 'com.google.code.gson:gson:2.1'
compile 'de.greenrobot:eventbus:2.4.0'
}
Is there an easy way to find out if any of these libraries has got a newer version for update? I noticed that Android Studio reminds new version available for gson but not for other libraries.
I'm from the iOS background, CocoaPods is the tool we use to manage external libraries. Using pod outdated command, it is very easy to figure out which libs have new version for upgrade.
There is a lint option called "Newer Library Versions Available".
You may need to add this to your build.gradle as well:
android {
...
lintOptions { warning 'NewerVersionAvailable' }
}
Then you can run Analyze > Run Inspection By Name... > Newer Library Versions Available to get a list of outdated dependencies.
Bintray has a watching mechanism that notifies you when a new version of a package you're watching is updated.
I am with JFrog, the company behind Bintray and [artifactory], see my profile for details and links.

Best way to add dependency for Wire using Gradle in Android Studio

I'm using Square's Wire library for my Android app, using Android Studio with Gradle.
I originally added the wire-runtime-1.2.0.jar into a libs folder in my module, and added the dependency to Gradle like this in my build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
That worked fine.
I'm new to Gradle and Android Studio, but based on the way I'm depending on the Google Support and Play Services libraries, I thought I might be able to remove the wire-runtime-1.2.0.jar library from my repository and just declare a dependency like this (the line is from the Maven repository):
dependencies {
compile 'com.squareup.wire:wire:1.0.0'
}
But if I do that then I hit this error:
Gradle: package com.squareup.wire does not exist
Is there a way to set up this dependency without importing the JAR file directly? Or does that only work for libraries that you can install through the SDK Manager?
Some packages, like com.squareup.wire, have multiple artifacts in Maven Central. You need to choose the right one for your needs. In this case, the equivalent of wire-runtime-1.2.0.jar is the wire-runtime artifact, not the wire artifact.
Here's what your dependencies section should look like:
dependencies {
compile 'com.squareup.wire:wire-runtime:1.2.0'
}

Categories

Resources