为 Android 实施新的 GCM 客户端时出现问题
Problems implementing the new GCM Client for Android
我正尝试按照 Google (https://developers.google.com/cloud-messaging/android/client) 的指南在 Android 上实施新的 GCM Client
。
对于两种不同的设备,我收到的错误略有不同。我注意到我收到了两个设备的方法 getNoBackupFilesDir 的 NoSuchMethodError。
这是 Android 4.2.2:
的 logcat
06-08 09:01:09.920: I/dalvikvm(9138): Could not find methodandroid.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve virtual method 273: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x00c2
06-08 09:01:09.920: I/dalvikvm(9138): DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve instance field 12
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x54 at 0x00e1
06-08 09:01:09.920: E/dalvikvm(9138): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve check-cast 25 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x1f at 0x000e
06-08 09:01:09.920: I/dalvikvm(9138): Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve virtual method 528: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x000b
06-08 09:01:09.950: I/dalvikvm(9138): Could not find method android.support.v4.content.ContextCompat.getNoBackupFilesDir, referenced from method com.google.android.gms.iid.zzd.zzde
06-08 09:01:09.950: W/dalvikvm(9138): VFY: unable to resolve virtual method 2080: Landroid/support/v4/content/ContextCompat;.getNoBackupFilesDir (Landroid/content/Context;)Ljava/io/File;
06-08 09:01:09.950: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x0007
06-08 09:01:09.950: W/dalvikvm(9138): threadid=11: thread exiting with uncaught exception (group=0x4174c930)
06-08 09:01:09.960: E/AndroidRuntime(9138): FATAL EXCEPTION: AsyncTask #1
06-08 09:01:09.960: E/AndroidRuntime(9138): java.lang.RuntimeException: An error occured while executing doInBackground()
06-08 09:01:09.960: E/AndroidRuntime(9138): at android.os.AsyncTask.done(AsyncTask.java:299)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-08 09:01:09.960: E/AndroidRuntime(9138): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.lang.Thread.run(Thread.java:856)
06-08 09:01:09.960: E/AndroidRuntime(9138): Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getNoBackupFilesDir
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.zzd.zzde(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.example.MainActivity.doInBackground(MainActivity.java:113)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.example.MainActivity.doInBackground(MainActivity.java:1)
06-08 09:01:09.960: E/AndroidRuntime(9138): at android.os.AsyncTask.call(AsyncTask.java:287)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-08 09:01:09.960: E/AndroidRuntime(9138): ... 4 more
这是 Android 5.1.1 的 logcat:
06-08 09:06:49.786: E/AndroidRuntime(2852): FATAL EXCEPTION: AsyncTask #1
06-08 09:06:49.786: E/AndroidRuntime(2852): Process: com.example, PID: 2852
06-08 09:06:49.786: E/AndroidRuntime(2852): java.lang.RuntimeException: An error occured while executing doInBackground()
06-08 09:06:49.786: E/AndroidRuntime(2852): at android.os.AsyncTask.done(AsyncTask.java:304)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-08 09:06:49.786: E/AndroidRuntime(2852): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.lang.Thread.run(Thread.java:818)
06-08 09:06:49.786: E/AndroidRuntime(2852): Caused by: java.lang.NoSuchMethodError: No virtual method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.example-1/base.apk)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.zzd.zzde(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at it.abgsys.htms.MainActivity.doInBackground(MainActivity.java:113)
06-08 09:06:49.786: E/AndroidRuntime(2852): at it.abgsys.htms.MainActivity.doInBackground(MainActivity.java:1)
06-08 09:06:49.786: E/AndroidRuntime(2852): at android.os.AsyncTask.call(AsyncTask.java:292)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-08 09:06:49.786: E/AndroidRuntime(2852): ... 4 more
在第 113 行我有这个:
InstanceID instanceID = InstanceID.getInstance(applicationContext);
此处使用:
// AsyncTask to register Device in GCM Server
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
InstanceID instanceID = InstanceID.getInstance(applicationContext);
try {
regId = instanceID.getToken(GCM_SENDER_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
if (!TextUtils.isEmpty(regId)) {
Toast.makeText(
applicationContext,
"Registered with GCM Server successfully.\n\n"
+ msg, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Registered with GCM Server successfully. RegID: " + regId);
} else {
Toast.makeText(
applicationContext,
"Reg ID Creation Failed.\n\nEither you haven't enabled Internet or GCM server is busy right now. "
+ "Make sure you enabled Internet and try registering again after some time."
+ msg, Toast.LENGTH_LONG).show();
Log.e(TAG, "Reg ID Creation Failed!!!");
}
}
}.execute(null, null, null);
}
我按照文档中的描述正确导入了 Google Play Services
库。(https://developers.google.com/android/guides/setup)
有人遇到过这个错误吗?我该如何解决这个问题?
使用的开发环境:Eclipse Kepler SR2 with ADT
如果您需要更多信息,请发表评论。
我看到 getNoBackupFilesDir
方法在 android.support.v4.content.ContextCompat 中找到。
问题似乎出在我的项目中包含一个旧的 appcompat-v7。 SDK manager
下载 Android Support Library
的最新版本,然后按照 Adding libraries with resources 的步骤将库添加到您的应用程序项目。
我收到这个错误:
java.lang.NoSuchMethodError: No static method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Lcom/google/android/gms/common/util/zzx; or its super classes (declaration of 'com.google.android.gms.common.util.zzx' appears in /data/data/com.crave.iapdemo/files/instant-run/dex/slice-com.google.android.gms-play-services-basement-10.0.1_b9da1447b99cc6cbc2fa601fb84d0418780bfa55-classes.dex)
at com.google.android.gms.iid.zzd.zzeC(Unknown Source)
at com.google.android.gms.iid.zzd.<init>(Unknown Source)
at com.google.android.gms.iid.zzd.<init>(Unknown Source)
at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
at vodio.labs.crave.gcm.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:39)
在这一行中:
InstanceID.getInstance(this);
我的解决方案是升级这个:
dependencies {
// play services
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.google.android.gms:play-services-analytics:9.0.0'
}
进入:
dependencies {
// play services
compile 'com.google.android.gms:play-services-gcm:10.0.1'
compile 'com.google.android.gms:play-services-analytics:10.0.1'
}
我正尝试按照 Google (https://developers.google.com/cloud-messaging/android/client) 的指南在 Android 上实施新的 GCM Client
。
对于两种不同的设备,我收到的错误略有不同。我注意到我收到了两个设备的方法 getNoBackupFilesDir 的 NoSuchMethodError。
这是 Android 4.2.2:
06-08 09:01:09.920: I/dalvikvm(9138): Could not find methodandroid.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve virtual method 273: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x00c2
06-08 09:01:09.920: I/dalvikvm(9138): DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve instance field 12
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x54 at 0x00e1
06-08 09:01:09.920: E/dalvikvm(9138): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve check-cast 25 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x1f at 0x000e
06-08 09:01:09.920: I/dalvikvm(9138): Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve virtual method 528: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x000b
06-08 09:01:09.950: I/dalvikvm(9138): Could not find method android.support.v4.content.ContextCompat.getNoBackupFilesDir, referenced from method com.google.android.gms.iid.zzd.zzde
06-08 09:01:09.950: W/dalvikvm(9138): VFY: unable to resolve virtual method 2080: Landroid/support/v4/content/ContextCompat;.getNoBackupFilesDir (Landroid/content/Context;)Ljava/io/File;
06-08 09:01:09.950: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x0007
06-08 09:01:09.950: W/dalvikvm(9138): threadid=11: thread exiting with uncaught exception (group=0x4174c930)
06-08 09:01:09.960: E/AndroidRuntime(9138): FATAL EXCEPTION: AsyncTask #1
06-08 09:01:09.960: E/AndroidRuntime(9138): java.lang.RuntimeException: An error occured while executing doInBackground()
06-08 09:01:09.960: E/AndroidRuntime(9138): at android.os.AsyncTask.done(AsyncTask.java:299)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-08 09:01:09.960: E/AndroidRuntime(9138): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.lang.Thread.run(Thread.java:856)
06-08 09:01:09.960: E/AndroidRuntime(9138): Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getNoBackupFilesDir
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.zzd.zzde(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.example.MainActivity.doInBackground(MainActivity.java:113)
06-08 09:01:09.960: E/AndroidRuntime(9138): at com.example.MainActivity.doInBackground(MainActivity.java:1)
06-08 09:01:09.960: E/AndroidRuntime(9138): at android.os.AsyncTask.call(AsyncTask.java:287)
06-08 09:01:09.960: E/AndroidRuntime(9138): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-08 09:01:09.960: E/AndroidRuntime(9138): ... 4 more
这是 Android 5.1.1 的 logcat:
06-08 09:06:49.786: E/AndroidRuntime(2852): FATAL EXCEPTION: AsyncTask #1
06-08 09:06:49.786: E/AndroidRuntime(2852): Process: com.example, PID: 2852
06-08 09:06:49.786: E/AndroidRuntime(2852): java.lang.RuntimeException: An error occured while executing doInBackground()
06-08 09:06:49.786: E/AndroidRuntime(2852): at android.os.AsyncTask.done(AsyncTask.java:304)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-08 09:06:49.786: E/AndroidRuntime(2852): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.lang.Thread.run(Thread.java:818)
06-08 09:06:49.786: E/AndroidRuntime(2852): Caused by: java.lang.NoSuchMethodError: No virtual method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.example-1/base.apk)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.zzd.zzde(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852): at it.abgsys.htms.MainActivity.doInBackground(MainActivity.java:113)
06-08 09:06:49.786: E/AndroidRuntime(2852): at it.abgsys.htms.MainActivity.doInBackground(MainActivity.java:1)
06-08 09:06:49.786: E/AndroidRuntime(2852): at android.os.AsyncTask.call(AsyncTask.java:292)
06-08 09:06:49.786: E/AndroidRuntime(2852): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-08 09:06:49.786: E/AndroidRuntime(2852): ... 4 more
在第 113 行我有这个:
InstanceID instanceID = InstanceID.getInstance(applicationContext);
此处使用:
// AsyncTask to register Device in GCM Server
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
InstanceID instanceID = InstanceID.getInstance(applicationContext);
try {
regId = instanceID.getToken(GCM_SENDER_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
if (!TextUtils.isEmpty(regId)) {
Toast.makeText(
applicationContext,
"Registered with GCM Server successfully.\n\n"
+ msg, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Registered with GCM Server successfully. RegID: " + regId);
} else {
Toast.makeText(
applicationContext,
"Reg ID Creation Failed.\n\nEither you haven't enabled Internet or GCM server is busy right now. "
+ "Make sure you enabled Internet and try registering again after some time."
+ msg, Toast.LENGTH_LONG).show();
Log.e(TAG, "Reg ID Creation Failed!!!");
}
}
}.execute(null, null, null);
}
我按照文档中的描述正确导入了 Google Play Services
库。(https://developers.google.com/android/guides/setup)
有人遇到过这个错误吗?我该如何解决这个问题?
使用的开发环境:Eclipse Kepler SR2 with ADT
如果您需要更多信息,请发表评论。
我看到 getNoBackupFilesDir
方法在 android.support.v4.content.ContextCompat 中找到。
问题似乎出在我的项目中包含一个旧的 appcompat-v7。 SDK manager
下载 Android Support Library
的最新版本,然后按照 Adding libraries with resources 的步骤将库添加到您的应用程序项目。
我收到这个错误:
java.lang.NoSuchMethodError: No static method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Lcom/google/android/gms/common/util/zzx; or its super classes (declaration of 'com.google.android.gms.common.util.zzx' appears in /data/data/com.crave.iapdemo/files/instant-run/dex/slice-com.google.android.gms-play-services-basement-10.0.1_b9da1447b99cc6cbc2fa601fb84d0418780bfa55-classes.dex)
at com.google.android.gms.iid.zzd.zzeC(Unknown Source)
at com.google.android.gms.iid.zzd.<init>(Unknown Source)
at com.google.android.gms.iid.zzd.<init>(Unknown Source)
at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
at vodio.labs.crave.gcm.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:39)
在这一行中:
InstanceID.getInstance(this);
我的解决方案是升级这个:
dependencies {
// play services
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.google.android.gms:play-services-analytics:9.0.0'
}
进入:
dependencies {
// play services
compile 'com.google.android.gms:play-services-gcm:10.0.1'
compile 'com.google.android.gms:play-services-analytics:10.0.1'
}