Integrating Google Drive in Android

See how to integrate the Google Drive SDK in order to let your users access and download their Drive files directly to your app. By Kevin D Moore.

Leave a rating/review
Download materials
Save for later
Share

There are times when a user is required to choose a file to upload into an app. There are many places from which to upload files: local storage on the device, Dropbox, Google Drive and other services. In this tutorial, you will create an app that will authenticate a user with Google, launch Google Drive, and then allow a user to choose a file. Once the user selects a file, the app will download and open it.

The Google Drive SDK is used to connect to a user’s Google Drive files. This tutorial will focus on allowing a user to choose an existing file, download it and display it through the app. You will use Google Drive’s built-in file picker, which will allow us to choose any file that is on the user’s Google drive.

Note: If you’re new to Android development or the Kotlin language, it’s highly recommended that you start with Beginning Android Development with Kotlin to learn your way around the basic tools and concepts.

Getting Started

Make sure you have Android Studio and the Kotlin plugin installed before you begin. To install Android Studio go to developer.android.com. Your phone or emulator will need up-to-date Google Play services on the device to run the app.

Since your UI will be bare-bones, open up Android Studio 3.1.1 or later and create a new project. From Android Studio, select Start a new Android Studio project from the startup screen or New Project from the File menu.

Enter the name GoogleDriveDemo, your company domain (or example.com if your wish) and a project location. Make sure that Kotlin support is selected and then press Next.

Create a Project - Google Drive Demo

You shouldn’t need to change anything on this screen. Just click Next.

Create a Project - Google Drive Demo

Select Empty Activity and press Next.

Create a Project - Google Drive Demo

Click Finish.

Registering for Google Drive

In order to use the Google Drive SDK, you need to enable the API in your app.

Walking through the steps:

Create a Project - Google Drive Demo

Credentials

Search API

Drive API

Enable API

Google Drive API Consent Screen Warning

Google Drive API Consent Screen

Create Credentials

OAuth Client ID

Create Client ID

If everything works correctly, you should see something like this:

Command Line

Client ID

Credentials Page

  • Go to Google Console.
  • Sign up for a developer’s account if you don’t have one or sign in.
  • Create a project and click Continue.
  • Add a project name and click Create.
  • Select Library on the left-hand side to go to the Search screen.
  • Type “Google Drive” and select Google Drive API.
  • Select Enable.
  • At this point, you may be asked to set a name on the consent screen before you create a credential. Do so and then create your credential.
  • You don’t need to complete the form that appears — only the name is required. Enter a name and press Save:
  • Click Create credentials.
  • Select OAuth Client ID.
  • Choose Android, enter a name and the package name that you used to create your app. Although the hint refers to the package name in AndroidManifest.xml, it has to match the applicationId in build.gradle instead — otherwise, the login flow will fail.
  • Copy the keytool text (press the Copy icon) and paste it into a terminal.
  • Change the path-to-debug-or-production-keystore to your default debug keystore location:
    • On Mac or Linux, ~/.android/debug.keystore
    • On Windows, %USERPROFILE%/.android/debug.keystore
  • After you execute the command, you will be prompted to enter the keystore password. The password for the debug keystore is blank by default, so you can just press Return or Enter.
  • Copy the SHA1 value from the terminal into the text field and press Create. The Client ID dialog will appear. Press OK.
  • Now, you are taken to the Credentials page. Authorization on Android uses the SHA1 fingerprint and package name to identify your app, so you don’t have to download any JSON file or copy any API key or secret to your project.
  • On Mac or Linux, ~/.android/debug.keystore
  • On Windows, %USERPROFILE%/.android/debug.keystore

Building Your Android App

Go back to the Android app and you can start adding settings and code.

Updating the Gradle File

First, you’ll update the build.gradle file inside the app folder. Add two variables for the version of Google Play services and Android support libraries after the apply plugin statements near the top:

ext {
  play_services_version = "15.0.1"
  support_version = "27.1.1"
}

This will let you re-use the variables and easily change the versions. Replace the version of com.android.support:appcompat-v7 with the variable, and add the support design library, both in the dependencies block:

implementation "com.android.support:appcompat-v7:$support_version"
implementation "com.android.support:design:$support_version"

The appcompat library contains all of the compatibility classes that help you write code that works on many versions of the Android OS. The design support library is used in this tutorial to display a Snackbar message.

Next, add the libraries for the Google Drive SDK and the Okio library from Square for downloading files.

// Google Drive
implementation "com.google.android.gms:play-services-auth:$play_services_version"
implementation "com.google.android.gms:play-services-drive:$play_services_version"
implementation 'com.squareup.okio:okio:1.14.0'

Now, sync the project Gradle files (File ▸ Sync Project with Gradle Files)

Modifying the Android Manifest

Next, you’ll modify AndroidManifest.xml. Open the file and add internet permissions right above the tag:

<uses-permission android:name="android.permission.INTERNET"/>

Add the following code to specify the version of Google Play Services as the first sub-element in the tag:

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

If you command-click (or Ctrl-click on PC) on the @integer/google_play_services_version you will see that it takes you to the play services values file that let’s the Google Play SDK know which version you are using.

Creating a File Provider

Next, you’ll create a FileProvider. This is required for Android 8.0 Oreo and above to access local files.

First, create a new directory by right-clicking on the app/res directory and selecing New ▸ Android Resource Directory. Name it xml. Right-click on the xml directory and select New ▸ File; name it provider_paths.

This is needed since Android Oreo does not support sharing file:// urls. Open the new file and paste in the following:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
   <external-path name="external_files" path="."/>
</paths>

Now, in the Android Manifest file, after the meta-data tag you recently added, add:

<provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="${applicationId}.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths"/>

This sets up your app to use Android’s FileProvider class to handle local files as urls instead of as files. This is a security restriction that Google has implemented.