我很难将ViewModel组件与Proguard一起使用。我已经不得不添加以下内容以防止由于nosuchmethodexception而导致的崩溃:init((
- keep class com .... slideShowViewModel {*;}
但是,我在活动中的观察者没有收到任何数据。在启用Proguard之前,这很好,所以我知道Proguard是原因,我只是不知道为什么(在这里新手Proguardian(。我需要添加什么规则才能使观察值正常工作?
我在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,但没有任何东西在可观察到的可观察到。
在: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 *;
}