UI 片段 <-> 事件总线 <-> 服务
UI Fragment <-> EventBus <-> Service
我在 UI 片段和后台服务之间有一个交互。
我使用事件总线。
当然,如果 Activity 是 stopped/killed,则没有订阅者。
代码如下,方便你理解:
public class LocationService extends Service {
//...
EventBus.getDefault().post(new MessageEventLocationClient(data));
}
public class ClientFragment extends Fragment {
//...
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEventLocationClient event) {
// update UI
textViewLastSentData.setText(event.trackData.lastLatLon());
}
}
一切顺利。
但是,这是 Google Play Developer Console 发给我的报告:
Devices with errors 14:
Google Nexus 7 (flo) - Android 5.0
Google Nexus 9 (flounder) - Android 5.0
Google Pixel (sailfish) - Android 7.1
Motorola XT1096 (victara) - Android 4.4
...
Exceptions:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.tim4dev.imokhere/com.tim4dev.imokhere.MainActivity}: org.a.a.e:
Subscriber class com.tim4dev.imokhere.c and its super
classes have no public methods with the @Subscribe annotation
...
这是什么意思?
这真的是个问题吗?
那怎么办?
类似的问题是described here。
UPD。 RTM。感谢大家。
如前所述,您需要指示混淆器不要删除使用@Subscribe 注释的方法。如果它们未被使用,Proguard 将删除它们,并且由于 EventBus 将通过反射查找它们,因此它们很可能未被使用。你可以在你的 proguard 配置文件中添加一些指令,来自 here:
## New rules for EventBus 3.0.x ##
# http://greenrobot.org/eventbus/documentation/proguard/
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
我在 UI 片段和后台服务之间有一个交互。 我使用事件总线。 当然,如果 Activity 是 stopped/killed,则没有订阅者。
代码如下,方便你理解:
public class LocationService extends Service {
//...
EventBus.getDefault().post(new MessageEventLocationClient(data));
}
public class ClientFragment extends Fragment {
//...
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEventLocationClient event) {
// update UI
textViewLastSentData.setText(event.trackData.lastLatLon());
}
}
一切顺利。
但是,这是 Google Play Developer Console 发给我的报告:
Devices with errors 14:
Google Nexus 7 (flo) - Android 5.0
Google Nexus 9 (flounder) - Android 5.0
Google Pixel (sailfish) - Android 7.1
Motorola XT1096 (victara) - Android 4.4
...
Exceptions:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.tim4dev.imokhere/com.tim4dev.imokhere.MainActivity}: org.a.a.e:
Subscriber class com.tim4dev.imokhere.c and its super
classes have no public methods with the @Subscribe annotation
...
这是什么意思?
这真的是个问题吗?
那怎么办?
类似的问题是described here。
UPD。 RTM。感谢大家。
如前所述,您需要指示混淆器不要删除使用@Subscribe 注释的方法。如果它们未被使用,Proguard 将删除它们,并且由于 EventBus 将通过反射查找它们,因此它们很可能未被使用。你可以在你的 proguard 配置文件中添加一些指令,来自 here:
## New rules for EventBus 3.0.x ##
# http://greenrobot.org/eventbus/documentation/proguard/
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}