我的实现显然没有错误,当我尝试加载此示例时,我得到异常
解组时找不到类: com.google.android.gms.common.api.Scope 抛出java.lang.ClassNotFoundException: com.google.android.gms.common.api.Scope at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:308) at java.lang.Class.forName(Class.java:272) 在android.os.Parcel.readParcelableCreator(Parcel.java:2275) 在android.os.Parcel.readParcelable(Parcel.java:2239) 在android.os.Parcel.readParcelableArray(Parcel.java:2332) 在android.os.Parcel.readValue(Parcel.java:2200) 在android.os.Parcel.readArrayMapInternal(Parcel.java:2479) 在android.os.BaseBundle.unparcel(BaseBundle.java:221) 在android.os.BaseBundle.getString(BaseBundle.java:918) 在android.content.Intent.getStringExtra(Intent.java:5386) 在com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1799) 在com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1353) 在com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:5214) 在com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:257) 在com.android.server.am.ActivityManagerService.startActivityIntentSender(ActivityManagerService.java:5012) 在android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:260) 在com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3128) 在android.os.Binder.execTransact(Binder.java:461) 引起:java.lang.ClassNotFoundException:没找到类 " com.google.android.gms.common.api.Scope"在路径上: DexPathList [[directory"。"],nativeLibraryDirectories = [/ vendor / lib, /系统/ lib中]] 在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
这是我的代码:
public class MainActivity extends AppCompatActivity implements ConnectionCallbacks,
OnConnectionFailedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
private static final int REQUEST_CODE_CREATOR = 2;
private static final int REQUEST_CODE_RESOLUTION = 3;
private GoogleApiClient mGoogleApiClient;
private Bitmap mBitmapToSave;
private void saveFileToDrive() {
Log.i(TAG, "saveFileToDrive() Creating new content.");
final Bitmap image = mBitmapToSave;
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new content.");
return;
}
Log.i(TAG, "New content has been created.");
OutputStream outputStream = result.getDriveContents().getOutputStream();
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
// Create the initial metadata - MIME type and title.
// Note that the user will be able to change the title later.
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("image/jpeg").setTitle("Android Photo.png").build();
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
} catch (SendIntentException e) {
Log.i(TAG, "Failed to launch file chooser.");
}
}
});
}
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_CAPTURE_IMAGE:
if (resultCode == Activity.RESULT_OK) {
mBitmapToSave = (Bitmap) data.getExtras().get("data");
}
break;
case REQUEST_CODE_CREATOR:
if (resultCode == RESULT_OK) {
Log.i(TAG, "Image successfully saved.");
mBitmapToSave = null;
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
REQUEST_CODE_CAPTURE_IMAGE);
}
break;
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
if (mBitmapToSave == null) {
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
REQUEST_CODE_CAPTURE_IMAGE);
return;
}
saveFileToDrive();
}
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
}
我的build.gradle
文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.jorgesys.googledrive"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-drive:8.4.0'
}
答案 0 :(得分:0)
我发现真正的问题是在我将此依赖项添加到build.gradle
文件时引起的:
compile 'com.google.android.gms:play-services-drive:8.4.0'
所以我试着启用multidex,认为可能我的问题是64k的引用限制,然后我添加了 multidex support ,现在没有问题。
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
...
...
multiDexEnabled true
}
...
...
答案 1 :(得分:0)
我通过使用“应用程序签名密钥”解决了我的问题(它与通常的签名密钥不同)。看here