編輯解決方案:如果您有相同的情況,請不要使用以下組合。android rxjava observable fromCallable onNext函數在proguard激活後不會調用
RxJava,傑克&吉爾的Java 8支持和Proguard的
我從我的gradle這個文件禁用傑克&吉爾和加入復古拉姆達插件拉姆達支持。問題解決了 !
proguard-rules.pro應包含以下RxJava規則:
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
問題
我使用Observable's fromCallable
功能來生成測試數據。代碼運行良好沒有proguard但是當我激活proguard,Subscription's onNext
函數不會調用。
可觀察到呼叫
public void loadNearbyUserEvents() {
Log.w(TAG, "loadNearbyUserEvents");
try {
getMvpView().showProgress();
Subscription subscription = dataManager.getEventImplementer().getNearbyUsersEvents()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(getSubscriber());
subscriptions.add(subscription);
Log.w(TAG, "aaaa");
} catch (Exception e) {
e.printStackTrace();
getMvpView().showError(e.getMessage());
getMvpView().hideProgress();
}
}
private Subscriber<List<Event>> getSubscriber() {
return new Subscriber<List<Event>>() {
@Override
public void onCompleted() {
Log.w(TAG, "onCompleted");
}
@Override
public void onError(Throwable e) {
Log.w(TAG, "onError");
e.printStackTrace();
}
@Override
public void onNext(List<Event> events) {
Log.w(TAG, "size of list : " + events.size());
getMvpView().hideProgress();
if (events.size() == 0) {
getMvpView().showEmpty();
return;
}
getMvpView().showEvents(events);
}
};
}
EventImplementer的getNearbyUserEvents
@Override
public Observable<List<Event>> getNearbyUsersEvents() {
return Observable.fromCallable(() -> EventProducer.produce(20));
}
這不proguard的工作遠遠高於碼。
proguard-rules.pro
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\developer\Android\Application\windows\android-sdk-windows/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# okhttp rulez
-dontwarn okio.**
# okhttp rulezzz
# retrofit rulez
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
# retrofit rulezzz
# searchView rulez
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class android.support.v7.app.** { *; }
-keep interface android.support.v7.app.** { *; }
-keep class android.support.v7.widget.SearchView { *; }
-keep public class * extends android.support.v7.widget.SearchView {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
}
# searchview rulezzz
# parcel rulez
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
# parcel rulezzz
# picasso rulez
-dontwarn com.squareup.okhttp.**
# picasso rulezzz
# rx rulez
# rxjava
-keep class rx.schedulers.Schedulers {
public static <methods>;
}
-keep class rx.schedulers.ImmediateScheduler {
public <methods>;
}
-keep class rx.schedulers.TestScheduler {
public <methods>;
}
-keep class rx.schedulers.Schedulers {
public static ** test();
}
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
long producerNode;
long consumerNode;
}
# rx rulezzz
什麼是錯我的代碼?爲什麼onCompleted函數可以工作,但onNext和onError函數不會調用?
注意:當我使用Observable.just(() -> EventProducer.produce(20))
函數它正在工作。
嘿,我得到了完全相同的問題。你找到了解決方案嗎? –
是的,我寫了問題帖的解決方案。 **如果您使用的是jack 8功能,則應該禁用它。如果你需要lambda表達式,你可以使用** retrolambda插件**。 – okarakose