Search code examples
androidgoogle-drive-android-api

Google Drive REST API AppDataFolder not working if APK is signed


Because the Google Drive Android API is deprecated I have migrated to the Google Drive REST API.

In my debug build all is working fine.
The folders, and files are created inside the root folder or hidden "appDataFolder".
But once I create an signed APK it isn't working anymore.

Login and the requesting for permission is working.
But if I want to create a folder or file I don't get a FILE ID back.
And always in the root of Google Drive (no matter if I use the appDataFolder or not) a file is created called "Untitled" with 0kb.

After hours of searching I don't find the cause why it is not working in the signed APK.

Creation of the Sign In with Scopes:

GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(DriveScopes.DRIVE_APPDATA), new Scope(DriveScopes.DRIVE_FILE))
            .build();
    return GoogleSignIn.getClient(context, signInOptions);

GoogleAccountCredential:

// Use the authenticated account ot sign in to the Drive service
    List<String> scopes = new ArrayList<>(2);
    scopes.add(DriveScopes.DRIVE_FILE);
    scopes.add(DriveScopes.DRIVE_APPDATA);
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scopes);
    credential.setSelectedAccount(googleSignInAccount.getAccount());

    Drive googleDriveService = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
            new GsonFactory(), credential)
            .setApplicationName("Test Application")
            .build();

Creation of the folder:

File fileMetadata = new File();
    fileMetadata.setName("Testfolder");

    // Add parent folder
    fileMetadata.setParents(Collections.singletonList("appDataFolder"));
    fileMetadata.setMimeType("application/vnd.google-apps.folder");

    File file;
    try {
        file = mDriveService.files().create(fileMetadata)
                .setFields("id")
                .execute();
    } catch (IOException e) {
        Log.e(TAG, "Can't create folder " + folderName, e);
        return "";
    }

    Log.i(TAG, "Folder " + folderName + " ID: " + file.getId());
    return file.getId();

Solution

  • The Problem is ProGuard!
    If I don't use ProGuard it is working fine.

    Adding this 2 lines to the ProGuard rules file:

    -keep,allowshrinking class com.google.api.services.drive.model.** { *;}
    -keep,allowshrinking class com.google.api.services.drive.** { *;}
    

    Afterwards it is working!

    Alternative if you have still problems you can use this solution: Google Drive Rest API: Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup