KMM iOS端使用PopupPositionProvider遇编译错误:IrClassSymbolImpl未绑定
Kotlin多平台iOS编译错误:IrClassSymbolImpl is unbound
错误信息
error: java.lang.IllegalStateException: IrClassSymbolImpl is unbound. Signature: null at org.jetbrains.kotlin.ir.symbols.impl.IrBindableSymbolBase.getOwner(IrPrivateSymbolBase.kt)
问题场景
在Kotlin多平台项目中,尝试通过@Composable函数直接返回匿名PopupPositionProvider实现时,iOS应用出现编译期错误,代码如下:
@Composable private fun rememberPopupPos( side: PopupSide, onAnchorPos: (pos: IntOffset) -> Unit ) = object : PopupPositionProvider { override fun calculatePosition( anchorBounds: IntRect, windowSize: IntSize, layoutDirection: LayoutDirection, popupContentSize: IntSize ): IntOffset { val popupRect = placePopup( rect = popupContentSize.toIntRect(), anchor = anchorBounds.inflate(anchorBounds.minDimension / 10), side = side, dir = layoutDirection ).constrainOffset(windowSize.toIntRect()) onAnchorPos(anchorBounds.center - popupRect.topLeft) return popupRect.topLeft } }
解决方法
1. 抽离匿名类为独立实现类
Kotlin/Native的IR编译器对Composable上下文中的匿名类支持存在限制,将PopupPositionProvider的实现抽成独立类即可解决:
首先定义独立的实现类:
class CustomPopupPositionProvider( private val side: PopupSide, private val onAnchorPos: (IntOffset) -> Unit ) : PopupPositionProvider { override fun calculatePosition( anchorBounds: IntRect, windowSize: IntSize, layoutDirection: LayoutDirection, popupContentSize: IntSize ): IntOffset { val popupRect = placePopup( rect = popupContentSize.toIntRect(), anchor = anchorBounds.inflate(anchorBounds.minDimension / 10), side = side, dir = layoutDirection ).constrainOffset(windowSize.toIntRect()) onAnchorPos(anchorBounds.center - popupRect.topLeft) return popupRect.topLeft } }
然后在Composable函数中通过remember创建实例:
@Composable private fun rememberPopupPos( side: PopupSide, onAnchorPos: (IntOffset) -> Unit ): PopupPositionProvider { return remember(side, onAnchorPos) { CustomPopupPositionProvider(side, onAnchorPos) } }
2. 检查版本兼容性
该错误常出现在Kotlin版本与Compose Multiplatform版本不匹配的场景,需确保两者版本对应(例如Kotlin 1.9.20对应Compose Multiplatform 1.5.1),调整至官方适配的版本组合。
3. 清理构建缓存
执行以下命令清理缓存后重新构建,排除缓存导致的IR绑定异常:
./gradlew clean
iOS端可同时在Xcode中执行Product > Clean Build Folder。
内容的提问来源于stack exchange,提问作者Gulab Sagevadiya
相关产品推荐
相关产品推荐

