Google 驱动 REST API 如果 APK 已签名,AppDataFolder 将无法运行
Google Drive REST API AppDataFolder not working if APK is signed
因为 Google 驱动器 Android API 已被弃用,我已迁移到 Google 驱动器 REST API.
在我的调试版本中一切正常。
文件夹和文件在根文件夹内创建或隐藏 "appDataFolder".
但是一旦我创建了一个 签名的 APK 它就不再工作了。
登录并正在请求权限。
但是如果我想创建一个文件夹或文件,我不会得到一个文件 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);
Google账户凭证:
// 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,它工作正常。
将这两行添加到 ProGuard 规则文件:
-keep,allowshrinking class com.google.api.services.drive.model.** { *;}
-keep,allowshrinking class com.google.api.services.drive.** { *;}
之后就可以了!
如果您仍有问题,可以使用以下解决方案:
因为 Google 驱动器 Android API 已被弃用,我已迁移到 Google 驱动器 REST API.
在我的调试版本中一切正常。
文件夹和文件在根文件夹内创建或隐藏 "appDataFolder".
但是一旦我创建了一个 签名的 APK 它就不再工作了。
登录并正在请求权限。
但是如果我想创建一个文件夹或文件,我不会得到一个文件 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);
Google账户凭证:
// 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,它工作正常。
将这两行添加到 ProGuard 规则文件:
-keep,allowshrinking class com.google.api.services.drive.model.** { *;}
-keep,allowshrinking class com.google.api.services.drive.** { *;}
之后就可以了!
如果您仍有问题,可以使用以下解决方案: