RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

Add visibility of proguard rules to README or wiki please

Open isuPatches opened this issue 11 years ago • 28 comments

Are there examples of proguard rules that will work with minifyEnabled set to true? I'm currently getting:

 java.lang.NoSuchMethodError: No virtual method doOnSubscribe(Lrx/functions/Action0;)Lrx/Observable

isuPatches avatar Jul 21 '15 21:07 isuPatches

+1

The unsafe package does some reflective lookups of fields that get lost in obfuscation, and the resulting NoSuchFieldException gets thrown up.

ZacSweers avatar Aug 13 '15 06:08 ZacSweers

+1 Actually, I have NoSuchFieldException: producerIndex with the release 1.0.14 (not happens in 1.0.13)

My current proguard settings for RxJava is

-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();
}

julienbanse avatar Aug 13 '15 21:08 julienbanse

I'm able to avoid it by keeping everything in rx.internal.util.unsafe, but would be nice to avoid the reflection entirely if possible.

ZacSweers avatar Aug 13 '15 21:08 ZacSweers

Yes, this is what I did to patch this issue.

julienbanse avatar Aug 13 '15 21:08 julienbanse

Full stack trace for NoSuchFieldException, that @julienbanse mentioned.

08-14 14:58:28.619    7362-7429/com.markavip.app E/AndroidRuntime﹕ FATAL EXCEPTION: pool-1-thread-1
    Process: com.markavip.app, PID: 7362
    java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
            at rx.internal.schedulers.ScheduledAction.run(SourceFile:62)
            at rx.schedulers.ExecutorScheduler$ExecutorSchedulerWorker.run(SourceFile:98)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.InternalError
            at rx.internal.util.unsafe.UnsafeAccess.a(SourceFile:103)
            at rx.internal.util.unsafe.SpscArrayQueueProducerFields.<clinit>(SourceFile:39)
            at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.<init>(SourceFile:102)
            at rx.internal.operators.OperatorObserveOn.a(SourceFile:64)
            at rx.internal.operators.OperatorObserveOn.a(SourceFile:44)
            at rx.Observable$2.a(SourceFile:158)
            at rx.Observable$2.a(SourceFile:154)
            at rx.Observable.a(SourceFile:7710)
            at rx.internal.operators.OperatorSubscribeOn$1$1.a(SourceFile:62)
            at rx.internal.schedulers.ScheduledAction.run(SourceFile:55)
            at rx.schedulers.ExecutorScheduler$ExecutorSchedulerWorker.run(SourceFile:98)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NoSuchFieldException: producerIndex
            at java.lang.Class.getDeclaredField(Class.java:890)
            at rx.internal.util.unsafe.UnsafeAccess.a(SourceFile:100)
            at rx.internal.util.unsafe.SpscArrayQueueProducerFields.<clinit>(SourceFile:39)
            at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.<init>(SourceFile:102)
            at rx.internal.operators.OperatorObserveOn.a(SourceFile:64)
            at rx.internal.operators.OperatorObserveOn.a(SourceFile:44)
            at rx.Observable$2.a(SourceFile:158)
            at rx.Observable$2.a(SourceFile:154)
            at rx.Observable.a(SourceFile:7710)
            at rx.internal.operators.OperatorSubscribeOn$1$1.a(SourceFile:62)
            at rx.internal.schedulers.ScheduledAction.run(SourceFile:55)
            at rx.schedulers.ExecutorScheduler$ExecutorSchedulerWorker.run(SourceFile:98)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

gokhanbarisaker avatar Aug 14 '15 11:08 gokhanbarisaker

Thing is, this used to JustWork(tm) because the reflective call was done directly on the target class (e.g. SpscArrayQueueProducerFields.class.getDeclaredField("producerIndex")) and proguard could figure out that this field needed to be kept (see the "reflection" chapter of the proguard documentation). Starting with 1.0.14 the reflective call is now indirected through a utility class (UnsafeAccess#addressOf) and I guess proguard don't detect the field to keep anymore.

For now, we can either keep everything in package unsafe or selectively keep reflected field with

-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
    long producerIndex;
    long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    long producerNode;
    long consumerNode;
}

while waiting for them to be annotated appropriately in the source.

desseim avatar Aug 26 '15 18:08 desseim

FWIW for Android at least this is going to be handled by https://github.com/ReactiveX/RxAndroid/issues/219 automatically for consumers. This obviously does not solve if you are ProGuarding for another target (or simply not using RxAndroid on Android).

JakeWharton avatar Aug 26 '15 18:08 JakeWharton

@julienbanse + @desseim responses together worked for me.

arturonaredo avatar Sep 16 '15 11:09 arturonaredo

@desseim Adding those rules produces these "noted" warnings but it works!:

Note: the configuration refers to the unknown field 'long producerNode' in class 'rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef'
Note: the configuration refers to the unknown field 'long consumerNode' in class 'rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef'

jaredsburrows avatar Nov 20 '15 07:11 jaredsburrows

@jaredsburrows You're right, thanks for noticing. Seems like a dumb copy-paste error... Anyway, the proper settings should be:

-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;
}

On my code base (and yours too apparently), BaseLinkedQueueProducerNodeRef and BaseLinkedQueueConsumerNodeRef classes altogether were stripped away by Proguard anyway so that no problem arose. For anyone whose code ended up BaseLinkedQueue though, it would have probably thrown at runtime.

desseim avatar Nov 20 '15 18:11 desseim

@desseim @felipesci Thank you so much for posting this.

I have recently updated rxandroid and rxjava:

    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.0'

and those current proguard rules do not seem to work anymore.

jaredsburrows avatar Dec 18 '15 08:12 jaredsburrows

@jaredsburrows I've backed ProGuard rules for RxJava as aar and we successfully use it in production for several months (updated each RxJava release), can you please try it? https://github.com/artem-zinnatullin/RxJavaProGuardRules

// if you'll face a problem — feel free to create an issue in the repository of the rules and I'll make a fix and ship new version.

artem-zinnatullin avatar Dec 18 '15 09:12 artem-zinnatullin

@artem-zinnatullin Sure! I believe I am using the same rules as your dependency is providing. You have tested this against 1.1.0?

jaredsburrows avatar Dec 22 '15 20:12 jaredsburrows

@jaredsburrows yep, we use it with 1.1.0, can you please post your problem (stacktraces/etc) as an issue to the repository? Or here as a comment, though I'm not sure that it's the right place.

artem-zinnatullin avatar Dec 22 '15 20:12 artem-zinnatullin

@artem-zinnatullin I am using this with Retrofit and I see the logs with proguard on but I am not seeing the UI get updated. I am still investigating and I will try to keep you updated.

jaredsburrows avatar Dec 22 '15 20:12 jaredsburrows

@artem-zinnatullin Just a quick update. The issue I was seeing was related to another issue with a few proguard rules and not related to the rules you have provided. Your rules work very well. Thanks for your support.

jaredsburrows avatar Feb 01 '16 03:02 jaredsburrows

Ok, thanks for update :+1:

artem-zinnatullin avatar Feb 01 '16 10:02 artem-zinnatullin

I'm closing this issue due to inactivity. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.

akarnokd avatar Apr 02 '16 23:04 akarnokd

We're using RxJava 1.1.5 in production currently for Android with ProGuard, and according to crash logs this issue pops up time to time on certain device + os conditions. To be specific the conditions seem to be (out of literally millions of sessions):

  • Only on Android 5.0, 5.0.1 or 5.0.2
  • Only on Samsung devices (sm_g900f, sm_n9005, gt_i9505 and some unknown, but still supposedly Samsung ones)
  • ProGuard is on, but obfuscation is off and the relevant ProGuard config is as follows:

-keep class rx.* { ; } -dontwarn rx.internal.

The crash rate is really low, but it's certainly happening. Has anyone encountered with this? There seem to be some funky vendor-specific thing going on there. So far we couldn't test this in house with the problematic devices.

Edit: Apparently Samsung doesn't really give much support on this: http://developer.samsung.com/forum/board/thread/view.do?boardName=General&messageId=286452

rzsombor avatar Jun 12 '16 07:06 rzsombor

See https://github.com/ReactiveX/RxJava/issues/3459

artem-zinnatullin avatar Jun 12 '16 09:06 artem-zinnatullin