我有一个单例服务类,可以按设定的时间表从服务器中提取数据。客户端收到数据后,我立即触发bus.post(new NewServerResponseEvent());
(http://square.github.io/otto/)
然后在我的 fragment 中,我这样做:
@Override
public void onResume() {
super.onResume();
eventBus.register(this);
}
@Override
public void onPause() {
super.onPause();
eventBus.unregister(this);
}
@Subscribe
public void handleNewServerData(NewServerResponseEvent e) {
refreshView();
}
只要我在测试设备上开发时就运行它,一切都会非常顺利。一旦我构建了发行版本并将其放入Play商店,就不会调用
handleNewServerData()
函数。我无法理解这一点。将整个内容作为发行版运行有什么区别?可能在无法发布到我的订阅者的另一个线程中发生了什么事?
有人可以指出我正确的方向吗?
提前致谢
最佳答案
您的发布版本可能会通过ProGuard运行,并推断出由于未直接调用订户方法,因此可以安全地将它们作为未使用的代码删除。奥托通过反射调用方法,而ProGuard无法看到。
将以下内容添加到您的proguard配置文件中,以保留用@Subscribe
或@Produce
注释的方法:
-keepattributes *Annotation*
-keepclassmembers class ** {
@com.squareup.otto.Subscribe public *;
@com.squareup.otto.Produce public *;
}