【问题标题】:Using a kotlin library in java code在 Java 代码中使用 kotlin 库
【发布时间】:2017-01-02 21:37:20
【问题描述】:

我有一个 kotlin 库 FlowLayout lib as aar

并想在我的 java 代码 (android) 中使用它但无法解析 kotlin.Pair

FlowLayoutManager layoutManager=new FlowLayoutManager(3, RecyclerView.HORIZONTAL, new FlowLayoutManager.Interface() {
    @Override
     public kotlin.Pair<Integer, Integer> getProportionalSizeForChild(int i) {
         return null;
     }
});

我试过android.support.v4.util.Pair 但不兼容。如何在 java 代码中使用 kotlin 库及其对象依赖项?

【问题讨论】:

  • 您的 Java 项目中是否依赖于 Kotlin 库(不记得工件名称,因为他们一直在更改它,但可能是核心)?

标签: java android compatibility kotlin aar


【解决方案1】:

kotlin.Pair属于标准库org.jetbrains.kotlin:kotlin-stdlib:1.0.0,所以你必须有这个库作为依赖。

不建议在公共 API 中使用 Pair。而不是这样,创建您自己的具有 2 个属性的 data class,并给它们一个比 firstsecond 更清晰的命名。

Pair 的目的是在任何中间操作中充当临时数据载体。如果你在你的 API 中使用它,你就会失去清晰度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多