在 Android 中使用 Java 21 模式匹配时出现编译器异常

问题描述 投票:0回答:1

我看到 Java 21 在 Switch 语句中引入了模式匹配。这对于我拥有的一些代码来说是理想的,我想使用它而不是一堆 if-if-else。

这是我做的测试课:

public class Test {
    static <T> void checkType(T value) {
        switch (value) {
            case Integer i -> System.out.println("Integer");
            case String s -> System.out.println("String");
            default -> throw new IllegalArgumentException("Unsupported type: " + value.getClass());
        }
    }

    public static void main(String[] args) {
        checkType("Some string");
        checkType(21);
    }
}

java Test.java
具有以下输出:

String
Integer

当我尝试在 Android 中执行相同的操作时,出现以下编译错误:

An exception has occurred in the compiler (21.0.3). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for java.lang.runtime.SwitchBootstraps not found

两者都使用Java 21.0.3,我尝试过22.0.1。我尝试过 Oracle JDK 和 Microsoft 的 OpenJDK。

我也在我的

build.gradle.kts
中设置了适当的参数(
compileOptions
&
kotlinOptions
)。

java android gradle
1个回答
0
投票

我绝对是个小丑,我以前确实处理过这个问题,我只是不认为是这样的。 Android 本身不支持 Java 21,目前最大值为 17:请阅读此处,即使如此,它也可能不支持所有 Java 17 API。

无论如何,我设置的最小 sdk 会让它变得更低。

© www.soinside.com 2019 - 2024. All rights reserved.