2017-06-27 25 views
5

Tôi đang gặp sự cố khi nhận thành phần ViewModel hoạt động với Proguard. Tôi đã có thêm những điều sau đây để ngăn chặn một vụ tai nạn do NoSuchMethodException: init()Cấu phần Kiến trúc Android - ViewModel Quan sát & Proguard

-keep lớp com .... SlideshowViewModel {*;}

Tuy nhiên, các nhà quan sát của tôi trong hoạt động này không nhận được bất kỳ dữ liệu nào. Điều này làm việc tốt cho đến khi tôi kích hoạt Proguard, vì vậy tôi biết Proguard là lý do, tôi chỉ không biết tại sao (người mới Proguardian ở đây). Tôi cần thêm quy tắc gì để làm cho các quan sát hoạt động?

Tôi có sau trong ViewModel của tôi (Kotlin)

val currentItem = MediatorLiveData<MediaItem>() 

.... sau ...

 Timber.d("Setting next image: " + position + " out of " + mediaItemList.size) 
     currentItem.value = mediaItemList[position] 

và Hoạt động (Java)

viewModel.getCurrentItem().observe(this, new Observer<MediaItem>() { 
     @Override 
     public void onChanged(@Nullable final MediaItem mediaItem) { 
      Timber.d("Activity received new item"); 
     } 
    }); 

Trong nhật ký tôi nhận được: D/SlideshowViewModel: Đặt hình ảnh tiếp theo: 0 trong số 18

Nhưng không có gì được kích hoạt trong OnChanged Observable.

Trả lời

5

Tìm thấy nó trên: https://issuetracker.google.com/issues/62113696

Nó cần được cố định sớm mặc dù (không phải trong alpha3 chưa)

## 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 *; 
} 
Các vấn đề liên quan