A simple library to upload, download and delete file in user's google drive app specific folder.
Step 1: Setup Google Cloud Project
-
Log in to Google Cloud Console
-
Create/Select a project
-
Navigate to
APIs & Services>Enable APIs & Services -
Search for
Google Drive APIand Enable it. -
Now navigate to
APIs & Services>OAuth Concent Screenand configure it. For now, provide only theApp name,User support Emailand a email forDeveloper contact information. Save and continue. -
Configure the scope, click on
Add or Remove scopeand addauth/drive.appdatascope. Save and continue. -
Add a test user. Then save and continue.
-
Review the information, if everything is okay, back to Dashboard.
Step 2: Create credentials
-
Navigate to
APIs & Services>Credentialspage. -
Create Credentials>Create OAuth client ID. -
Select
Web Applicationas Application type (mandatory). You'll need the credential ID to use this project. Just give a name, no other info needed for now. Click onCreate. -
Similarly create a credential for your app. Just select
Android Appinstead ofWeb Applicationthis time.
Now that you've configured the cloud project, let's configure the app.
Step 3: Configure android app
-
add this library in your app level gradle file:
implementation('com.github.fcat97:driveBackupApi:1.0.8') -
If your
minSdkversion is below25you may need to add Google's guava library manually as well. -
Also you need to exclude
/META-INF/AL2.0and/META-INF/DEPENDENCIESto work.
android {
packagingOptions {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
excludes += '/META-INF/DEPENDENCIES'
}
}
dependencies {
implementation('com.github.fcat97:driveBackupApi:1.0.8')
// if minSdk < 25
// https://stackoverflow.com/a/71085378/8229399
implementation("com.google.guava:guava") {
version {
strictly '31.1-android'
}
}
}Your project is configured.
Step 4: How to use the library?
First create an instance of GoogleDriveBackupManager in any activity. You must initialize it before onCreate() of the activity. This is because the library uses registerActivityForResult() internally which requires to attach it with the activity before onCreate() is called.
NOTE: The credentialID is the Web Application's credential id, not the android's one.
All done. Now you can use the GoogleDriveBackupManager and all of its methods. All the methods are lifecycle aware. All the network operations are also run on worker thread. Just remember to add network permission in the manifest file.
Happy coding...