因为不赞成使用Google Drive Android API,所以我已迁移到Google Drive REST API。
在我的调试版本中,一切正常。这些文件夹和文件在根文件夹或隐藏的“ appDataFolder”内部创建。但是,一旦创建了signed APK,它就不再起作用。
登录并且正在请求许可。但是,如果我想创建一个文件夹或文件,则不会获得FILE ID。并且始终在Google云端硬盘的根目录中(无论是否使用appDataFolder),都会创建一个名为“ Untitled”且文件大小为0kb的文件。
经过数小时的搜索,我没有找到无法在签名的APK中运行的原因。
创建具有范围的登录:
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();
创建文件夹:
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();
问题是ProGuard!如果我不使用ProGuard,它将运行良好。
将这2行添加到ProGuard规则文件:
-keep,allowshrinking class com.google.api.services.drive.model.** { *;}
-keep,allowshrinking class com.google.api.services.drive.** { *;}
之后它开始工作!
或者,如果您仍然有问题,可以使用此解决方案:Google Drive Rest API: Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup