构建React Native Android遇Gradle报错:No variants found
React Native构建Android时出现"No variants found for ':mauron85_react-native-background-geolocation'"错误
问题详情
开发带后台定位追踪功能的React Native应用,使用mauron85/react-native-background-geolocation库,React Native版本为0.67.3。构建Android应用时Gradle抛出错误:
No variants found for ':mauron85_react-native-background-geolocation'. Check build files to ensure at least one variant exists.
项目根目录build.gradle配置如下:
buildscript { ext { buildToolsVersion = "30.0.2" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 supportLibVersion = "28.0.0" googlePlayServicesVersion = "11+" ndkVersion = "21.4.7075529" kotlin_version = "1.6.0" // Update this to your version } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:4.2.2") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
解决方案
锁定兼容的库版本:该库部分版本与RN 0.67.x存在兼容性问题,在
package.json中指定适配版本,比如:"react-native-background-geolocation": "^0.6.3"执行
npm install或yarn install重新安装依赖。升级Gradle与Kotlin版本:当前Gradle 4.2.2和Kotlin 1.6.0可能无法匹配库的构建要求,修改
buildscript配置:buildscript { ext { // 保留原有其他配置 kotlin_version = "1.7.10" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:7.0.4") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") } }清理构建缓存:执行以下命令清除缓存,避免缓存导致的异常:
cd android && ./gradlew clean cd .. && npx react-native clean统一子项目构建配置:在项目根目录
build.gradle中添加子项目配置,强制库的构建参数与项目保持一致:subprojects { afterEvaluate { project -> if (project.name.contains('react-native-background-geolocation')) { android { compileSdkVersion rootProject.ext.compileSdkVersion minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion } } } }
内容的提问来源于stack exchange,提问作者Mohammed Tawfik
相关产品推荐
相关产品推荐

