Google 意识 API 抛出 SecurityException

Google Awareness API SecurityException is thrown

我正在使用 DetectedActivityFence from Google Awareness API。它在我自己的设备上运行良好,但我在 Crashlytics 中收到了几次关于 SecurityException.

的崩溃
Fatal Exception: java.lang.SecurityException: Invalid API Key for package = [package_name] .Status code received = -1
   at android.os.Parcel.readException(Parcel.java:1540)
   at android.os.Parcel.readException(Parcel.java:1493)
   at com.google.android.gms.common.internal.zzu$zza$zza.zza(Unknown Source)
   at com.google.android.gms.common.internal.zzd.zzqz(Unknown Source)
   at com.google.android.gms.internal.zzpw$zzc.zzapl(Unknown Source)
   at com.google.android.gms.internal.zzpw$zzf.run(Unknown Source)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at com.google.android.gms.internal.zzrn.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:818)

这在 Android 版本之间没有任何关联,即它影响来自不同制造商和不同 Android 版本的设备(5.x-6.x)。这些设备没有获得 root 权限,看起来像是在 Stock ROM 上工作。 10% 的用户会出现此问题。

我确定我使用的是正确的 API 密钥,否则我可以在自己的设备上看到崩溃,可以吗?

有人遇到同样的问题吗?

有一个错误会错误地将网络操作中的一些错误误认为是表明 API 密钥未正确设置的应用程序,即使实际错误是其他原因。这已在内部修复,将在下一个 Google Play 服务版本中推出。

目前,最好的解决方法是在调用 GoogleApiClient.connect() 的线程上添加 UncaughtExceptionHandler。如果你在主线程上调用 GoogleApiClient.connect() ,那么你可以通过 Looper.myLooper().getThread().

获取 Thread 实例

发件人:https://code.google.com/p/android/issues/detail?id=223751#c2

There is a bug in our checks that is sometimes throwing this SecurityException when there is a network error to authenticate. The fix will rollout in the next version of Google Play services.

For now, the best workaround would be set up an UncaughtExceptionHandler on the thread that you call GoogleApiClient.connect(). For most folks, this would be on the main thread, which you can get via Looper.myLooper().getThread().

但您也会注意到回复表明这不是一个可靠的解决方法。

您可能还注意到该线程的 OP 引用了此 SO post,但接受的答案没有反向引用该线程,即使他们逐字复制了该线程。非常糟糕的形式!

我遇到了这个问题,因为我遇到了同样的错误,我认为这可能是 Awareness API 键没有同步到 Google 开发者控制台,经过大量的查找和获取同样的错误,我回到 Google Developer Console 上的 Awareness API 并意识到我的 Awareness API 没有启用,尽管我已经设置了包并且有一个有效的 API 键。还要确保在您的清单中设置了 API 键。希望这对某人有所帮助。

折腾了将近一天,找到了另一个答案。虽然当时接受的答案是正确的。

唯一帮助我的是在 Google 开发者控制台上生成 API 密钥,使用调试密钥库进行测试,并在生产时将其替换为发布密钥库。

如何生成API密钥?请查看 Google 的字词本身 Here

希望对以后的人有所帮助。