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

使用Expo构建React Native Android应用时Kotlin编译失败求助

解决方案

问题核心

你遇到两个关联问题:

  1. Kotlin Gradle插件在多个子项目中重复加载,违反Gradle插件管理规则
  2. Kotlin编译器尝试使用当前环境不支持的JVM Target版本20

分步修复

1. 统一管理Kotlin插件版本

打开项目根目录下的android/build.gradle文件:

  • 在顶级plugins块中添加统一的Kotlin插件声明(适配React Native 0.72的版本):
plugins {
    // 保留已有的其他插件配置
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}
  • 检查所有子项目(如:expo、:expo-modules-core)的build.gradle文件,确保Kotlin插件声明不携带版本号,例如修改为:
plugins {
    id "org.jetbrains.kotlin.android"
    // 其他插件配置...
}

2. 修正JVM Target版本

在android/build.gradle的android块中,强制指定兼容的JVM版本(React Native 0.72推荐Java 17):

android {
    // 保留已有的其他配置
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}

3. 清理并重新构建

执行以下命令彻底清理缓存并重新生成安卓项目文件:

./gradlew clean
npx expo prebuild --clean

之后再尝试运行构建命令:

npx expo run:android
# 或者EAS构建命令
eas build --profile development --platform android

版本适配说明

你的项目使用Expo 49 + React Native 0.72.4,该组合默认适配Kotlin 1.8.x和Java 17,请勿使用过高的JVM Target版本(如20),避免编译器兼容性冲突。

内容的提问来源于stack exchange,提问作者Thomasino73

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 21:30:18