React Native安卓构建报错:react-native-gesture-handler编译失败
问题:React Native安卓构建报错
Execution failed for task ':react-native-gesture-handler:compileDebugKotlin 报错信息
Execution failed for task ':react-native-gesture-handler:compileDebugKotlin`
项目依赖配置(package.json)
"dependencies": { "@dynatrace/react-native-plugin": "^2.305.1", "@notifee/react-native": "^7.8.2", "@react-native-async-storage/async-storage": "^1.23.1", "@react-native-community/netinfo": "^11.3.1", "@react-native-cookies/cookies": "^6.2.1", "@react-native-firebase/analytics": "^20.0.0", "@react-native-firebase/app": "^20.0.0", "@react-native-firebase/messaging": "^20.0.0", "@react-native-firebase/remote-config": "^20.0.0", "@react-native/gradle-plugin": "^0.76.3", "@react-navigation/bottom-tabs": "^6.6.1", "@react-navigation/material-top-tabs": "^6.6.14", "@react-navigation/native": "^6.1.18", "@react-navigation/stack": "^6.4.1", "@reduxjs/toolkit": "^1.9.5", "@shopify/restyle": "^1.3.0", "@types/clone": "^2.1.1", "@types/react-redux": "^7.1.9", "axios": "^1.4.0", "better-docs": "^2.7.2", "clone": "^2.1.2", "compare-versions": "^6.1.0", "husky": "^9.1.3", "identity-obj-proxy": "^3.0.0", "lint-staged": "^15.0.2", "moment": "^2.29.1", "react": "^18.2.0", "react-dom": "^18.0.0", "react-native": "^0.71.18", "react-native-adjust": "^4.35.1", "react-native-base64": "^0.2.1", "react-native-blob-util": "0.19.4", "react-native-bootsplash": "^4.3.3", "react-native-contacts": "^7.0.4", "react-native-crypto-js": "^1.0.0", "react-native-device-info": "^11.1.0", "react-native-elements": "^2.3.2", "react-native-exit-app": "^1.1.0", "react-native-fast-image": "^8.3.2", "react-native-fs": "^2.20.0", "react-native-geolocation-service": "^5.3.1", "react-native-gesture-bottom-sheet": "^1.0.9", "react-native-gesture-handler": "^2.9.0", "react-native-gradle-plugin": "^0.71.19", "react-native-hash": "^3.0.3", "react-native-image-helper": "0.0.3", "react-native-inappbrowser-reborn": "^3.7.0", "react-native-offline": "^6.0.2", "react-native-popup-menu": "^0.16.1", "react-native-public-ip": "^1.0.2", "react-native-reanimated": "3.12.0", "react-native-redash": "^18.1.3", "react-native-safe-area-context": "^3.1.8", "react-native-screens": "^2.11.0", "react-native-share": "^11.0.3", "react-native-size-matters": "^0.4.0", "react-native-splash-screen": "^3.2.0", "react-native-store-review": "^0.4.3", "react-native-svg": "15.3.0", "react-native-swrve-plugin": "5.0.0", "react-native-system-setting": "^1.7.6", "react-native-vector-icons": "^10.1.0", "react-native-webview": "^13.8.6", "react-redux": "^8.1.2", "redux": "^4.0.5", "redux-saga": "^1.1.3" }, "devDependencies": { "@babel/cli": "^7.24.1", "@babel/core": "^7.24.4", "@babel/preset-env": "^7.24.4", "@babel/runtime": "^7.24.4", "@react-native-community/eslint-config": "^3.2.0", "@redux-saga/types": "^1.1.0", "@testing-library/react-native": "^12.5.0", "@tsconfig/react-native": "^3.0.5", "@types/enzyme": "^3.10.14", "@types/enzyme-adapter-react-16": "1.0.6", "@types/jest": "^29.5.5", "@types/react": "^18.0.24", "@types/react-dom": "^18.0.0", "@types/react-native": "^0.71.13", "@types/react-native-base64": "^0.2.2", "@types/react-test-renderer": "^18.0.2", "@typescript-eslint/eslint-plugin": "^6.7.3", "@typescript-eslint/parser": "^6.7.3", "babel-jest": "^29.7.0", "enzyme": "^3.11.0", "enzyme-adapter-react-16": "^1.15.6", "enzyme-to-json": "^3.6.1", "eslint": "^8.19.0", "jest": "^29.7.0", "jest-environment-enzyme": "^7.1.2", "jest-enzyme": "^7.1.2", "jsdoc": "^4.0.2", "metro-react-native-babel-preset": "^0.73.10", "react-native-svg-transformer": "^1.0.0", "react-test-renderer": "^18.2.0", "reactotron-react-native": "5.0.0", "reactotron-redux": "3.1.3", "reactotron-redux-saga": "4.2.3", "redux-devtools-extension": "^2.13.8", "typescript": "^5.2.2" }
Android构建配置(android/build.gradle)
buildscript { apply from: "../node_modules/@dynatrace/react-native-plugin/files/plugin.gradle", to: buildscript ext { minSdkVersion = 23 buildToolsVersion = "33.0.0" compileSdkVersion = 34 targetSdkVersion = 34 kotlinVersion = "1.6.0" // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. ndkVersion = "23.1.7779620" androidXAnnotation = "1.2.0" androidXBrowser = "1.3.0" } repositories { google() mavenCentral() flatDir { dirs 'libs' } } dependencies { classpath('com.android.tools.build:gradle:8.7.2') classpath 'com.google.gms:google-services:4.4.1' classpath("com.facebook.react:react-native-gradle-plugin") classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } apply from: "../node_modules/@dynatrace/react-native-plugin/files/dynatrace.gradle"
已尝试操作及问题
- 移除
react-native-gesture-handler版本号前的^,构建成功但出现导航异常。 - 此前应用运行正常,近期突然触发该报错。
解决方案
1. 升级Kotlin版本到兼容版本
报错根源是react-native-gesture-handler因^自动升级到新版本,其对Kotlin版本要求高于项目当前的1.6.21。升级Kotlin版本即可解决:
修改android/build.gradle中的kotlinVersion和对应的gradle插件版本:
buildscript { ext { // ...保留其他配置 kotlinVersion = "1.8.22" // 更新为兼容版本 } dependencies { // ...保留其他依赖 classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.22' // 同步插件版本 } }
注:Gradle 8.7.x推荐搭配Kotlin 1.8.x或1.9.x版本,可根据实际情况调整。
2. 强制锁定兼容的react-native-gesture-handler版本
若不想升级Kotlin,可通过包管理器的版本锁定功能,指定一个兼容当前Kotlin 1.6.x的react-native-gesture-handler版本,同时保留^语义:
- npm:在package.json中添加
overrides字段
"overrides": { "react-native-gesture-handler": "2.16.2" }
- Yarn:在package.json中添加
resolutions字段
"resolutions": { "react-native-gesture-handler": "2.16.2" }
执行npm install/yarn install后,会强制安装指定版本,避免自动升级到不兼容的新版本。
3. 清理缓存并重新构建
无论采用哪种方法,执行以下步骤确保缓存清理干净:
# 清理node_modules和包缓存 rm -rf node_modules package-lock.json yarn.lock npm install # 或 yarn install # 清理Android构建缓存 cd android ./gradlew clean cd .. # 重新构建安卓应用 npm run android
内容的提问来源于stack exchange,提问作者Lucas Acuña
相关产品推荐
相关产品推荐

