React Native集成react-native-google-mobile-ads遇Kotlin编译失败求助
React Native 0.72.5接入react-native-google-mobile-ads编译失败问题
问题描述
在基于React Native 0.72.5的项目中接入react-native-google-mobile-ads@12.3.0时,运行项目触发错误:Execution failed for task ':react-native-google-mobile-ads:compileDebugKotlin',但该包在空RN项目中可正常运行。
环境配置
package.json依赖
"@react-native-async-storage/async-storage": "^1.19.3", "@react-native-picker/picker": "^2.5.1", "@react-navigation/bottom-tabs": "^6.5.8", "@react-navigation/drawer": "^6.6.3", "@react-navigation/material-bottom-tabs": "^6.2.17", "@react-navigation/native": "^6.1.7", "@react-navigation/native-stack": "^6.9.13", "@react-navigation/stack": "^6.3.17s", "axios": "^1.5.1", "cheerio": "^1.0.0-rc.12", "lottie-react-native": "^6.3.1", "metro-config": "^0.77.0", "react": "18.2.0", "react-native": "0.72.5", "react-native-dropdown-picker": "^5.4.6", "react-native-gesture-handler": "^2.12.0", "react-native-google-mobile-ads": "^12.3.0", "react-native-paper": "^5.10.6", "react-native-picker-select": "^8.1.0", "react-native-reanimated": "^3.3.0", "react-native-safe-area-context": "^4.7.0", "react-native-screens": "^3.22.1", "react-native-vector-icons": "^10.0.0"
build.gradle配置
buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 kotlinVersion = "1.9.0" ndkVersion = "23.1.7779620" } repositories { google() mavenCentral() jcenter() } dependencies { classpath("com.android.tools.build:gradle") classpath("com.facebook.react:react-native-gradle-plugin") classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0" } }
可能成因分析
- Kotlin版本冲突:项目指定Kotlin版本为1.9.0,而
react-native-google-mobile-ads@12.3.0可能依赖更低版本的Kotlin插件。空项目默认Kotlin版本与包兼容,但现有项目的高版本Kotlin导致编译语法或API不兼容。 - 第三方依赖冲突:项目中其他库(如
react-native-reanimated、导航系列库)可能修改了Android构建配置,比如调整compileSdkVersion、buildToolsVersion,或引入与谷歌广告包冲突的依赖。 - Gradle版本不匹配:
com.android.tools.build:gradle未指定版本,可能与Kotlin插件1.9.0不兼容。React Native 0.72.5对应Gradle版本应为7.4.2左右,版本不匹配会导致Kotlin编译任务失败。 - 缓存残留问题:现有项目Gradle缓存中存在旧的冲突依赖或编译产物,空项目无缓存所以正常,清理缓存后可能解决问题。
- 配置覆盖问题:项目中模块级build.gradle或其他配置文件,可能覆盖了谷歌广告包所需的构建参数(如minSdkVersion、Kotlin版本),导致编译环境不满足要求。
内容的提问来源于stack exchange,提问作者daSilva
相关产品推荐
相关产品推荐

