简体   繁体   English

proguard导致谷歌播放服务'ActivityRecognitionResult getMostProbableActivity崩溃

[英]proguard causes a crash in google play services' ActivityRecognitionResult getMostProbableActivity

I've recently released an app to the play store, and while it works perfectly fine without proguard, i've got an unexpected crash when i did decide to use it. 我最近发布了一个应用程序到游戏商店,虽然它没有proguard工作得很好,但当我决定使用它时,我遇到了意外的崩溃。

I've looked here for the recommended proguard rules for google play services, i've also tried adding another line for this case. 我在这里寻找谷歌播放服务推荐的proguard规则,我也试过为这种情况添加另一行。 Here's what I got (the third line is for my app): 这是我得到的(第三行是我的应用程序):

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}
-keep class * implements com.google.android.gms.internal.ae
-keep class * extends il.co.kix.minitasker.EntityBase

Here's the crash report after doing a retrace 这是回溯后的崩溃报告

android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.google.android.gms.location.ActivityRecognitionResult
at android.os.Parcel.readParcelable(Parcel.java:2086)
at android.os.Parcel.readValue(Parcel.java:1965)
at android.os.Parcel.readMapInternal(Parcel.java:2226)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.containsKey(Bundle.java:271)
at android.content.Intent.hasExtra(Intent.java:4116)
at com.google.android.gms.location.ActivityRecognitionResult.boolean hasResult(android.content.Intent)(Unknown Source)
                                                             com.google.android.gms.location.DetectedActivity getMostProbableActivity()
at il.co.kix.minitasker.ActivityRecognitionIntentService.void onHandleIntent(android.content.Intent)(Unknown Source)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)

The offending lines of code probably are: 令人讨厌的代码行可能是:

...
   @Override
    protected void onHandleIntent(Intent intent) {
        if (ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            DetectedActivity mostProbableActivity = result.getMostProbableActivity();
...

Can anyone help out with a rule to add? 任何人都可以帮助添加规则吗? I don't want to disable it all together but it does fix the issue. 我不想一起禁用它,但确实解决了这个问题。

The Android runtime accesses these CREATOR fields by means of reflection, which is generally impossible to detect using static analysis. Android运行时通过反射访问这些CREATOR字段,这通常无法使用静态分析进行检测。 You therefore need to tell ProGuard to preserve them: 因此,您需要告诉ProGuard保留它们:

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

This doesn't seem to be a standard setting in android-sdk/tools/proguard/proguard-android.txt , but it probably should be. 这似乎不是android-sdk/tools/proguard/proguard-android.txt的标准设置,但它可能应该是。

This problem drove me insane. 这个问题让我疯了。 Proguard is stripping internal classes that are not explicitly imported. Proguard正在剥离未明确导入的内部类。 Even worse, this problem did not exist for me (after using Proguard) then one day is suddenly showed up after a few little code changes. 更糟糕的是,这个问题对我来说并不存在(使用Proguard之后),然后在一些小代码更改后突然出现了一天。

I added a swathe of Proguard flags to fix the issue. 我添加了一大堆Proguard标志来解决这个问题。 In the end, I am not sure which one did the trick: 最后,我不确定是哪一个做了这个伎俩:

Definitely add these three: 绝对添加这三个:

-keep class android.os.Parcelable.Creator
-keep class com.google.android.gms.location.ActivityRecognitionResult
-keep class com.google.android.gms.** {*;}

You can also try: 你也可以尝试:

-dontshrink
-dontoptimize

at the top 在顶部

Honestly it's a dependency-walker type problem and Proguard should be better than this, but I did eventually fix it as above. 老实说,这是一个依赖性 - 步行者类型问题,而Proguard应该比这更好,但我最终确定如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM