我无法让ViewModel组件与Proguard一起使用。我必须添加以下内容以防止因NoSuchMethodException导致崩溃:init()
-keep class com .... SlideshowViewModel {*;}
但是,我在活动中的观察者没有收到任何数据。这个工作正常,直到我启用Proguard,所以我知道Proguard是原因,我只是不知道为什么(新手Proguardian在这里)。我需要添加什么规则才能使observable工作?
我的ViewModel(Kotlin)
中有以下内容val currentItem = MediatorLiveData<MediaItem>()
...后...
Timber.d("Setting next image: " + position + " out of " + mediaItemList.size)
currentItem.value = mediaItemList[position]
和活动(Java)
viewModel.getCurrentItem().observe(this, new Observer<MediaItem>() {
@Override
public void onChanged(@Nullable final MediaItem mediaItem) {
Timber.d("Activity received new item");
}
});
在我收到的日志中: D / SlideshowViewModel:设置下一个图像:18个中的0个
但是在onChanged Observable中没有任何东西被触发。
答案 0 :(得分:6)
找到它:https://issuetracker.google.com/issues/62113696
尽管应该很快修复(不是在alpha3中)
## Android architecture components: Lifecycle
# LifecycleObserver's empty constructor is considered to be unused by proguard
-keepclassmembers class * implements android.arch.lifecycle.LifecycleObserver {
<init>(...);
}
# ViewModel's empty constructor is considered to be unused by proguard
-keepclassmembers class * extends android.arch.lifecycle.ViewModel {
<init>(...);
}
# keep Lifecycle State and Event enums values
-keepclassmembers class android.arch.lifecycle.Lifecycle$State { *; }
-keepclassmembers class android.arch.lifecycle.Lifecycle$Event { *; }
# keep methods annotated with @OnLifecycleEvent even if they seem to be unused
# (Mostly for LiveData.LifecycleBoundObserver.onStateChange(), but who knows)
-keepclassmembers class * {
@android.arch.lifecycle.OnLifecycleEvent *;
}