Using observeOn() the Android UI thread crashes my emulator testing

1.1k views Asked by At

When I use RxAndroid and .observeOn(AndroidSchedulers.mainThread()), and run tests on an emulator using Android Studio, the whole test run crashes with:

Instrumentation run failed due to 'java.lang.NoSuchMethodError'

and logcat output like the following:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:54)
       at android.os.Handler.handleCallback(Handler.java:587)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:123)
       at android.app.ActivityThread.main(ActivityThread.java:4627)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:521)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
       at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodError: rx.internal.schedulers.ScheduledAction.lazySet
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:46)
       ... 9 more
Error in app net.tbmcv.rxtest running instrumentation ComponentInfo{net.tbmcv.rxtest.test/android.test.InstrumentationTestRunner}:
  java.lang.NoSuchMethodError
  java.lang.NoSuchMethodError: rx.internal.schedulers.ScheduledAction.lazySet
Force stopping package net.tbmcv.rxtest uid=10042

Neither Android Studio nor the logcat output mention any lines of code in my project or my tests. Here are some relevant parts of a short example project I created that reproduces this crash:

app/build.gradle

dependencies {
    compile 'io.reactivex:rxandroid:0.24.0'
}

MainActivity.java

public class MainActivity extends Activity {
    public void setTitleFrom(Observable<String> observable) {
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        setTitle(s);
                    }
                });
    }
}

MainActivityFuncTest.java

public class MainActivityFuncTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public void testSetTitle() {
        final String newTitle = "Hello World";
        getActivity().setTitleFrom(Observable.just(newTitle));
        getInstrumentation().waitForIdleSync();
        assertEquals(newTitle, getActivity().getTitle());
    }
}

My test runs correctly if I modify my MainActivity code to not use observeOn():

public void setTitleFrom(Observable<String> observable) {
    observable.subscribe(new Action1<String>() {
        @Override
        public void call(final String s) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setTitle(s);
                }
            });
        }
    });
}

But I'd rather use observeOn(), because it's cleaner and more modular. How can I get my tests to run?

My emulator is running API version 8 (Android 2.2).

1

There are 1 answers

0
Dan Getz On BEST ANSWER

According to RxJava's GitHub page, RxJava versions 1.0.x only work on Android 2.3 or later (corresponding to API 9 or later). RxAndroid depends on RxJava, of course, so the same restriction applies (even though it's currently not mentioned on the RxAndroid page.)

The above tests run fine on an emulator running a more recent version of Android.