2

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();
chrisonline
  • 6,949
  • 11
  • 42
  • 62
  • In the google API console did you upload the singing key for you debug build only? – Quinn Feb 07 '19 at 18:45
  • The Google Drive Android API has worked before. I have now checked the API console and I have the signing key for the upload key still added. I use also the places API and this is working fine also with the signed APK. I don't get any errors. The file "undefined" is created and I get no ID back. So it seems that something with the "appDataFolder" is bad. – chrisonline Feb 07 '19 at 18:48
  • OK I have now tried to save it to the root insted of "appDataFolder" and the same problem. – chrisonline Feb 07 '19 at 19:02

1 Answers1

1

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

chrisonline
  • 6,949
  • 11
  • 42
  • 62