You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.80 集成 lottie-react-native 7.2.2 因Kotlin类型不匹配导致Android构建失败

React Native 0.80 集成 lottie-react-native 7.2.2 因Kotlin类型不匹配导致Android构建失败

最近在React Native 0.80项目里集成lottie-react-native 7.2.2时,很多开发者都碰到了Android构建失败的问题,我来详细梳理下问题的来龙去脉和解决办法:

问题现象

构建过程中会触发Kotlin编译错误,具体报错信息如下:

> Task :lottie-react-native:compileDebugKotlin FAILED
e: file:///path/to/node_modules/lottie-react-native/android/src/main/java/com/airbnb/android/react/lottie/LottieAnimationViewPropertyManager.kt:249:26 Initializer type mismatch: expected 'Int', actual 'Int?'.
e: file:///path/to/node_modules/lottie-react-native/android/src/main/java/com/airbnb/android/react/lottie/LottieAnimationViewPropertyManager.kt:250:13 Type mismatch: inferred type is 'Int?', but 'Int' was expected.

环境信息

  • React Native版本:0.80
  • lottie-react-native版本:7.2.2
  • 目标平台:Android
  • 构建工具:Gradle + Kotlin

问题根因

这个问题的核心是空安全类型不匹配:React Native 0.80对API做了空安全优化,其中ColorPropConverter.getColor()方法现在返回的是可空的Int?类型,但lottie-react-native 7.2.2的代码里还是按照旧的非空Int类型来接收,这就导致了编译时的类型冲突,具体出现在LottieAnimationViewPropertyManager.kt文件的parseColorFilter方法中。

解决办法

优先方案:升级lottie-react-native到兼容版本

官方已经在7.3.0及以上版本修复了这个适配问题,直接升级库就能彻底解决:

# npm用户
npm install lottie-react-native@^7.3.0 --save

# yarn用户
yarn add lottie-react-native@^7.3.0

升级完成后,执行npx react-native run-android重新构建项目即可。

临时应急方案:手动修改依赖代码(不推荐长期使用)

如果暂时没法升级库,可以临时修改node_modules里的对应文件救急:

  1. 找到文件路径:node_modules/lottie-react-native/android/src/main/java/com/airbnb/android/react/lottie/LottieAnimationViewPropertyManager.kt
  2. 定位到报错的249-250行,处理可空类型:
    • 如果你能确定getColor()不会返回null,可以用非空断言:
      val color = ColorPropConverter.getColor(colorValue, context)!!
      
    • 保险起见,可以添加空判断并设置默认颜色:
      val color = ColorPropConverter.getColor(colorValue, context) ?: Color.BLACK
      
  3. 执行清理和构建命令:
cd android && ./gradlew clean assembleDebug

⚠️ 注意:这种修改会在重新安装依赖时被覆盖,所以只是临时救急的办法,后续还是建议升级到官方修复版本。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 09:18:09