我们的应用程序中有一些使用 Java 的代码和其他使用 Kotlin 的代码。我们遇到了一个非常奇怪的情况。
该问题出现在某些设备上,在将其传递给构造函数时它报告platform
为存在,尽管我们检查是否不存在并提供常量:null
AutoValue_Session
lastPlatform
null
Platform.MISSING
Caused by java.lang.NullPointerException: Null lastPlatform
x.x.x.x.AutoValue_Session.(SourceFile:27)
x.x.x.x.Session.from(SourceFile:28) // from that calls the child constructor
x.x.x.x.Session.(SourceFile:22) // whereNONE_FOUND is defined
x.x.x.x.Session.from(SourceFile:32) // entry point
[...]
有人遇到过 Android 和 Kotlin 互操作的问题吗?
更新:这发生在野外并且只有少量用户。还不能在本地复制它。
更新2:第二次调用Session.from
是由第一次加载到内存中<clinit>
引起的。Session
这是提到的两个类的代码:
平台.kt
sealed class Platform {
abstract val name : String
sealed class Local : Platform() {
object Default : Local() {
override val name: String = "android"
}
object Amazon : Local() {
override val name: String = "android-amazon"
}
}
object Missing : Platform() {
override val name: String = "missing"
}
data class Remote(override val name: String) : Platform()
companion object {
@JvmName("from")
@JvmStatic
operator fun invoke(name: String?): Platform = when (name) {
Local.Default.name -> Local.Default
Local.Amazon.name -> Local.Amazon
Missing.name -> Missing
null -> Missing
else -> Remote(name)
}
@JvmField
val LOCAL : Platform = if (BuildConfig.amazon) Local.Amazon else Local.Generic
@JvmField
val MISSING : Platform = Missing
}
}
会话.java
@AutoValue
public abstract class Session implements Serializable {
public static final Session NONE_FOUND = from(0, new DateTime(0), Platform.MISSING);
public static Session from(String id, @Nullable Platform lastPlatform) {
Platform platform = lastPlatform == null ? Platform.MISSING : lastPlatform;
return new AutoValue_Session(id, platform);
}
public static Session from(RemoteSession remote) {
return from(
remote.id(),
remote.lastPlatform()
);
}
public abstract String id();
public abstract Platform lastPlatform();
}