GoogleDriveBackUp, as it stands, is a very simple library to handle your Android app backup onto the Google Drive of your users.
It stems from the need of a simple example to solve this use case. Example that I couldn't find anywhere online, including Google's documentation, despite many people struggling to find an answer.
It does so while respecting the privacy of users. It does so in a way you can simply implement in your app. It does so with the latest Google non-deprecated APIs (as of Aug 2025).
- DRIVE_APPDATA scope only - meaning you never get the user's Google account's data in your hands
- Upload of any files given
- Callback for all events, including progress of upload
- Download (as of Aug 20th 2025 - coming soon)
- Reconciliation strategy, that's up to you to determine which version if more up to date, especially if the user has several devices.
Before using this library, you need to:
- Create a Google Cloud project
- Enable the Drive API with the DRIVE_APPDATA scope.
- Create 2 ClientIds: one for the DEBUG, and one for the RELEASE version of your app
First, pull the library from GitHub
$ cd MyAndroidRootProjectFolder
$ git submodule add https://github.com/licryle/GoogleDriveBackUp.git googledrivebackup
Add a dependency to your Root folder build.grade.kts (the app one)
depedencies {
implementation(project(":googledrivebackup"))
}
Very simply, import the classes:
import fr.berliat.googledrivebackup.GoogleDriveBackup
import fr.berliat.googledrivebackup.GoogleDriveBackupFile
import fr.berliat.googledrivebackup.GoogleDriveBackupInterface
In your Fragment or Application's onCreate or onCreateView, initialize it with a listener
class ConfigFragment : Fragment(), GoogleDriveBackupInterface {
private lateinit var gDriveBackUp : GoogleDriveBackup
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
...
gDriveBackUp = GoogleDriveBackup(this, requireActivity(), getString(R.string.app_name))
gDriveBackUp.addListener(this)
...
Then, when you want to start the action, call login()
gDriveBackUp.login()
The callbacks will lead you to the rest, in particular onReady() is a good place to have the files listed for upload or download, but also in a more Kotlinic way, login supports a success callBack:
gDriveBackUp.login {
val sourcePath = "${requireContext().filesDir.path}/file_to_backup"
gDriveBackUp.backup(
listOf(GoogleDriveBackupFile.UploadFile(
"database.sqlite",
FileInputStream(sourcePath),
"application/octet-stream",
File(sourcePath).length()))
)
}
or
gDriveBackUp.login {
val sourcePath = "${requireContext().cacheDir}/restore/restored_file"
File(sourcePath).parentFile?.mkdirs()
gDriveBackUp.restore(
listOf(GoogleDriveBackupFile.DownloadFile(
"database.sqlite",
FileOutputStream(sourcePath)
))
)
}
Callbacks at the moment:
interface GoogleDriveBackupInterface {
fun onLogout()
fun onReady()
fun onNoAccountSelected()
fun onScopeDenied(e: Exception)
fun onBackupStarted()
fun onBackupProgress(fileName: String, fileIndex: Int, fileCount: Int, bytesSent: Long, bytesTotal: Long)
fun onBackupSuccess()
fun onBackupCancelled()
fun onBackupFailed(e: Exception)
fun onRestoreEmpty()
fun onRestoreStarted()
fun onRestoreProgress(fileName: String, fileIndex: Int, fileCount: Int, bytesSent: Long, bytesTotal: Long)
fun onRestoreSuccess(files: List<GoogleDriveBackupFile.DownloadFile>)
fun onRestoreCancelled()
fun onRestoreFailed(e: Exception)
fun onDeletePreviousBackupFailed(e: Exception)
fun onDeletePreviousBackupSuccess()
}