swiftlang / swiftlang/swift-java
[JavaKit] `@JavaImplementation` method returning a null instance from Swift to Java
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 1.2k
- Forks
- 123
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 16
Description
I'm trying to wrap RecyclerView.Adapter in Android and create the row view from Swift and pass it to Java via JNI. Unfortunately on the Java / Kotlin side the returned value is always null.
RecyclerViewAdapter.kt
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewAdapter.ViewHolder {
val viewHolder = onCreateViewHolderSwift(parent, viewType)
checkNotNull(viewHolder)
checkNotNull(viewHolder.itemView)
return viewHolder
}
RecyclerViewAdapter.swift
@JavaImplementation("com.pureswift.swiftandroid.RecyclerViewAdapter")
extension RecyclerViewAdapter {
@JavaMethod
public func onCreateViewHolderSwift(_ viewGroup: ViewGroup?, _ viewType: Int32) -> RecyclerViewAdapter.ViewHolder? {
log("\(self).\(#function) \(viewType)")
let viewHolder = callback.onCreateViewHolder(viewGroup!, viewType)
log("\(self).\(#function) Created \(viewHolder.getClass().getName()) \(viewHolder.itemView.getClass().getName())")
return viewHolder
}
I've verified from the Swift side that the Java Object I'm returning is a valid instance, and print their Java class name. But the checkNotNull() still fails on the JVM side.
16:57:54.394 D SwiftAndroidApp.RecyclerViewAdapter.onCreateViewHolderSwift(_:_:) Created com.pureswift.swiftandroid.RecyclerViewAdapter$ViewHolder android.widget.TextView
16:57:54.394 D Shutting down VM
16:57:54.395 E FATAL EXCEPTION: main
Process: com.pureswift.swiftandroid, PID: 7551
java.lang.IllegalStateException: Required value was null.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure using the attached Android.zip sample and android-crash.log. Start with the @JavaMethod in RecyclerViewAdapter.swift and its Kotlin caller in RecyclerViewAdapter.kt, then trace the Swift-to-Java return boundary. Done means the returned ViewHolder reaches the JVM as non-null and the checkNotNull failure no longer occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java, kotlin, swift
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100