Kotlin与RxKotlinFX堆栈给出无法访问类错误

时间:2017-11-08 19:12:30

标签: kotlin rx-kotlin

当我在我的pom文件中使用以下依赖项时:

<properties>
    <rxkotlinfx.version>2.2.0</rxkotlinfx.version>
    <rxkotlin.version>2.1.0</rxkotlin.version>
    <kotlin.version>1.1.51</kotlin.version>
    <tornadofx.version>1.7.12</tornadofx.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.github.thomasnield</groupId>
        <artifactId>rxkotlinfx</artifactId>
        <version>${rxkotlinfx.version}</version>
    </dependency>
    <dependency>
        <groupId>io.reactivex.rxjava2</groupId>
        <artifactId>rxkotlin</artifactId>
        <version>${rxkotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>no.tornado</groupId>
        <artifactId>tornadofx</artifactId>
        <version>${tornadofx.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

运行以下代码:

fun main(args: Array<String>) {
    val source = listOf("A", "B", "C").toObservable()
            .filter { it == "B" }
            .subscribeBy(
                    onNext = { println(it) }
            )
}

我收到以下错误:

Error:(37, 40) Kotlin: Cannot access class 'io.reactivex.Observable'. Check your module classpath for missing or conflicting dependencies

为什么我会收到此错误以及我需要使用哪种依赖关系才能使用此堆栈?

2 个答案:

答案 0 :(得分:1)

我找到的唯一解决方案是明确依赖:

<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxjava</artifactId>
    <version>2.1.3</version>
</dependency>

答案 1 :(得分:0)

io.reactivex.Observable 驻留在 rxjava-2.1.3.jar 库中,这是由您在pom.xml中声明的2个依赖项带来的传递依赖:< / p>

  • com.github.thomasnield:rxkotlinfx:2.2.0

  • io.reactivex.rxjava2:rxkotlin:2.1.0

我用gradle尝试了你的设置(抱歉,我喜欢比maven更多的gradle)并且我使用gradle命令行来查看冲突解决方案是什么: 结果如下:

io.reactivex.rxjava2:rxjava:2.1.3 (conflict resolution)
\--- io.reactivex.rxjava2:rxjavafx:2.2.0
     \--- com.github.thomasnield:rxkotlinfx:2.2.0
          \--- compileClasspath

io.reactivex.rxjava2:rxjava:2.1.0 -> 2.1.3
\--- io.reactivex.rxjava2:rxkotlin:2.1.0
     \--- compileClasspath

io.reactivex.rxjava2:rxjavafx:2.2.0
\--- com.github.thomasnield:rxkotlinfx:2.2.0
     \--- compileClasspath

io.reactivex.rxjava2:rxkotlin:2.1.0 (selected by rule)
\--- compileClasspath

和gradle冲突解决方案选择v 2.1.3

我知道gradle中的依赖管理比maven更强大,但我会强制maven通过排除不需要的依赖版本来使用特定版本的传递依赖。

我会尝试以下方法:

<dependency>
  <groupId>io.reactivex.rxjava2</groupId>
  <artifactId>rxkotlin</artifactId>
  <version>${rxkotlin.version}</version>
  <exclusions>
    <exclusion>
      <groupId>io.reactivex.rxjava2</groupId>
      <artifactId>rxjava</artifactId>
    </exclusion>
  </exclusions>
</dependency>

或者,如果您不需要某些API,可以删除

    <dependency>
        <groupId>com.github.thomasnield</groupId>
        <artifactId>rxkotlinfx</artifactId>
        <version>${rxkotlinfx.version}</version>
    </dependency>