javaandroidrx-java3

Rxjava3 not working as expected when minifyEnabled set to true


When I turned minifyEnabled in build.gradle to true, seems like rxjava3 suddenly can't work as expected, when I go through the log and the API response correctly.

So what I do is create a empty new project.

  1. Import Rxjava3 (3.0.13): implementation "io.reactivex.rxjava3:rxjava:3.0.13"

  2. Changed buildTypes to:

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
  1. Change MainActivity.java's onCreate function to following:
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Simply give me "Test"
        Single.just("Test")
                .subscribe(new SingleObserver<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onSuccess(@NonNull String s) {
                        Log.d("TAG", s); // (s expect: "Test", get: null)
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }
                });
    }
  1. Set breakpoint in onSuccess then connect device and press debug.
  2. In onSuccess you will get null instead of "Test".

If you switch debug's minifyEnabled back to false, it will work as expected. I also add these lines in proguard-rules.pre file:

##---------------Begin: proguard configuration for rxjava  ----------
-dontwarn java.util.concurrent.Flow*
##---------------End: proguard configuration for rxjava  ----------

It did not work.

Can someone show me some way to go from here? Thank you in advance.


Solution

  • Try adding this instead in Proguard.

    -dontwarn java.util.concurrent.**