运行Expo Android应用时遭遇Kotlin-Gradle编译错误
运行Expo Android应用时遭遇Kotlin-Gradle编译错误
问题描述
我在执行npx expo run:android的时候遇到了Gradle Kotlin编译失败的问题,具体错误日志如下:
npx expo run:android › Building app... Starting a Gradle Daemon, 4 stopped Daemons could not be reused, use --status for details Configuration on demand is an incubating feature. Task :expo-module-gradle-plugin:compileKotlin e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ExpoModulesGradlePlugin.kt:10:28 Unresolved reference 'extensions'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ExpoModulesGradlePlugin.kt:45:32 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ExpoModulesGradlePlugin.kt:50:32 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ProjectConfiguration.kt:18:28 Unresolved reference 'extensions'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ProjectConfiguration.kt:34:3 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ProjectConfiguration.kt:35:3 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ProjectConfiguration.kt:53:32 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ProjectConfiguration.kt:55:28 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/ProjectConfiguration.kt:57:31 Unresolved reference 'extra'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/gradle/ExpoModuleExtension.kt:10:28 Unresolved reference 'extensions'. e: file:///home/kevin-langat/Code/React/rovify-mob-app/node_modules/expo-modules-core/expo-module-gradle-plugin/src/main/kotlin/expo/modules/plugin/gradle/ExpoModuleExtension.kt:34:32 Unresolved reference 'extra'. Task :expo-module-gradle-plugin:compileKotlin FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':expo-module-gradle-plugin:compileKotlin'. > A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction > Compilation error. See log for more details * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 58s 26 actionable tasks: 21 executed, 5 from cache Error: /home/kevin-langat/Code/React/rovify-mob-app/android/gradlew app:assembleDebug -x lint -x test --configure-on-demand --build-cache -PreactNativeDevServerPort=8081 -PreactNativeArchitectures=armeabi-v7a exited with non-zero code: 1
解决思路与步骤
这些错误的核心是Gradle的Kotlin插件无法识别extensions、extra这些Gradle标准API,通常是版本不兼容、缓存冲突或者Expo模块配置异常导致的。下面是几个我在项目中亲测有效的解决方法,你可以按顺序尝试:
1. 确保Gradle与Kotlin版本兼容
Gradle和Kotlin插件版本必须严格匹配,否则会出现这类API无法识别的问题:
- 打开
android/gradle/wrapper/gradle-wrapper.properties,查看当前Gradle版本(比如distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip对应Gradle 8.2) - 找到对应兼容的Kotlin版本(比如Gradle 8.x对应Kotlin 1.8.x,Gradle 7.x对应Kotlin 1.6.x)
- 修改
android/build.gradle中的Kotlin插件版本:buildscript { ext { kotlinVersion = "1.8.22" // 替换为你Gradle版本对应的兼容Kotlin版本 } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" } }
2. 彻底清理缓存并重建依赖
缓存冲突是这类问题的高发原因,我们可以从Expo、Gradle、Node三个层面清理:
- 清理Expo缓存:
npx expo r -c - 进入Android目录清理Gradle缓存并刷新依赖:
cd android ./gradlew clean ./gradlew build --refresh-dependencies - 回到项目根目录,重置Node依赖:
rm -rf node_modules package-lock.json yarn.lock npm install # 或yarn install,根据你用的包管理器选择
3. 升级/修复expo-modules-core版本
错误直接出在expo-modules-core的Gradle插件中,大概率是版本不匹配:
- 升级到最新兼容版本:
npm install expo-modules-core@latest - 如果你的Expo SDK版本比较旧,建议安装对应SDK的兼容版本(比如SDK 49对应
expo-modules-core@1.5.x,SDK 50对应1.10.x)
4. 重置Android项目配置
如果你的项目手动修改过Android配置,可能和Expo的模块冲突:
- 备份
android/build.gradle和android/app/build.gradle后,执行以下命令重新生成默认配置:npx expo prebuild --clean - 这会重置Android目录的所有配置到Expo默认状态,排除自定义配置的干扰
5. 检查Java版本
Gradle 8.x及以上版本要求Java 17,如果你用的是Java 8或11,可能会出现这类编译错误:
- 安装并切换到Java 17(可以用版本管理工具比如
jabba或asdf来管理多版本Java) - 确保项目的JAVA_HOME环境变量指向Java 17
最后排查
如果以上方法都无效,建议创建一个空白的Expo项目(npx create-expo-app test-app),然后尝试运行npx expo run:android,如果空白项目能正常运行,说明你的项目存在依赖或配置冲突,可以逐步迁移代码和依赖,定位问题根源。




