Unresolved reference: Observable in Android Studio

2019-07-20 08:29发布

问题:

Updated to Android Studio 3.4, with Gradle 5.1.1 and Kotlin 1.3.10 as minimum versions. Cannot run project anymore due to reactivex.Observable not being found. Has anyone found a solution?

Was using rxBinding lib, also tried using latest vesions RxJava 2.2.8 and RxAndroid 2.1.1 with no success.

回答1:

Downgrading the build.gradle(project) from classpath 'com.android.tools.build:gradle:3.4.0' to classpath 'com.android.tools.build:gradle:3.2.1' might be help you



回答2:

The issue is that you need to use RxJava 2.2.8 and if you check the RxAndroid gradle file they are using the RxJava version 2.2.6 :

https://github.com/ReactiveX/RxAndroid/blob/2.x/rxandroid/build.gradle

If you force RxJava to use version 2.2.8 it will work.

Add to gradle:

implementation 'io.reactivex.rxjava2:rxjava:2.2.8


回答3:

Yeah the workaround* for me was to downgrade from gradle 5.1.1 to 4.10.1 and also to the latest android gradle plugin prior to 3.4.0, which is 3.3.2.

I did this in gradle/wrapper/gradle-wrapper.properties by changing to the following: distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

I then also changed the following in my root build.gradle file:

'com.android.tools.build:gradle:3.4.0' to 'com.android.tools.build:gradle:3.3.2'



回答4:

The accepted answer ended up leading me to the solution: leaving the gradle-wrapper.properties intact and don't increase the gradle version at all. We'll see if a future lib update will solve it for gradle 3.4.



回答5:

Along with the dependency of RxKotlin use this

api 'io.reactivex.rxjava2:rxjava:2.2.0'

Only works for Gradle 5.0 or above