Expo集成react-native-google-nearby-messages时EAS构建失败求助
Expo集成react-native-google-nearby-messages构建失败问题排查与解决
在Expo中集成包含原生代码的react-native-google-nearby-messages包时,执行命令eas build --profile development --platform android失败,遇到以下三类问题:
- @expo/config-plugins版本不符合要求
- Gradle构建找不到
maven插件 - 未指定
compileSdkVersion
以下是具体错误日志及自定义配置插件代码,附解决方案:
Expo Doctor检测结果
Running "expo doctor" - Finding all copies of expo-modules-autolinking - Finding all copies of @expo/config-plugins [stderr] [20:13:25] Expected package @expo/config-plugins@^5.0.0 [stderr] [20:13:25] Found invalid: [stderr] [20:13:25] @expo/config-plugins@4.1.5 [stderr] [20:13:25] (for more info, run: npm why @expo/config-plugins) - Finding all copies of @expo/prebuild-config - Finding all copies of @unimodules/core - Finding all copies of @unimodules/react-native-adapter - Finding all copies of react-native-unimodules Command "expo doctor" failed. bash exited with non-zero code: 1
Gradle构建错误日志
[stderr] FAILURE: Build completed with 2 failures. [stderr] 1: Task failed with an exception. [stderr] ----------- [stderr] * Where: [stderr] Build file '/home/expo/workingdir/build/node_modules/react-native-google-nearby-messages/android/build.gradle' line: 24 [stderr] * What went wrong: [stderr] A problem occurred evaluating project ':react-native-google-nearby-messages'. [stderr] > Plugin with id 'maven' not found. [stderr] * Try: [stderr] > Run with --stacktrace option to get the stack trace. [stderr] > Run with --info or --debug option to get more log output. [stderr] > Run with --scan to get full insights. [stderr] ============================================================================== [stderr] 2: Task failed with an exception. [stderr] ----------- [stderr] * What went wrong: [stderr] A problem occurred configuring project ':react-native-google-nearby-messages'. [stderr] > compileSdkVersion is not specified. Please add it to build.gradle [stderr] * Try: [stderr] > Run with --stacktrace option to get the stack trace. [stderr] > Run with --info or --debug option to get more log output. [stderr] > Run with --scan to get full insights. [stderr] ============================================================================== [stderr] * Get more help at Gradle官方文档 [stderr] BUILD FAILED in 1m 49s Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See Gradle命令行警告相关文档 6 actionable tasks: 6 executed Error: Gradle build failed with unknown error. See logs for the "Run gradlew" phase for more information.
自定义配置插件代码
const { AndroidConfig, withAndroidManifest } = require("@expo/config-plugins"); const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = AndroidConfig.Manifest; module.exports = withMyCustomConfig = (config) => { return withAndroidManifest(config, async (config) => { // Modifiers can be async, but try to keep them fast. config.modResults = await setCustomConfigAsync(config, config.modResults); return config; }); }; // Splitting this function out of the mod makes it easier to test. async function setCustomConfigAsync(config, androidManifest) { const appId = "my-app-id"; // Get the <application /> tag and assert if it doesn't exist. const mainApplication = getMainApplicationOrThrow(androidManifest); addMetaDataItemToMainApplication( mainApplication, // value for `android:name` "com.google.android.nearby.messages.API_KEY", // value for `android:value` "myabikey" ); return androidManifest; }
解决方案
1. 升级@expo/config-plugins到指定版本
执行命令更新依赖:
npm install @expo/config-plugins@^5.0.0 --save-dev # 或使用yarn yarn add @expo/config-plugins@^5.0.0 --dev
2. 修复react-native-google-nearby-messages的Gradle配置
原插件仅处理了Manifest配置,需扩展插件修复Gradle相关问题:
const { AndroidConfig, withAndroidManifest, withProjectBuildGradle, withAppBuildGradle } = require("@expo/config-plugins"); const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = AndroidConfig.Manifest; const { mergeContents } = require('@expo/config-plugins/build/utils/generateCode'); module.exports = (config) => { // 保留原Manifest配置逻辑 config = withAndroidManifest(config, async (config) => { config.modResults = await setCustomConfigAsync(config, config.modResults); return config; }); // 替换废弃的maven插件为maven-publish config = withProjectBuildGradle(config, async (config) => { if (config.modResults.language === 'groovy') { config.modResults.contents = config.modResults.contents.replace( "apply plugin: 'maven'", "apply plugin: 'maven-publish'" ); } return config; }); // 注入compileSdkVersion等必要配置 config = withAppBuildGradle(config, async (config) => { if (config.modResults.language === 'groovy') { const buildGradleContent = config.modResults.contents; const newContent = mergeContents({ src: buildGradleContent, newSrc: ` android { compileSdkVersion rootProject.ext.compileSdkVersion defaultConfig { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion } } `, anchor: /android\s*{/, offset: 1, comment: '// Added by Expo config plugin for react-native-google-nearby-messages', }); config.modResults.contents = newContent.contents; } return config; }); return config; }; async function setCustomConfigAsync(config, androidManifest) { const mainApplication = getMainApplicationOrThrow(androidManifest); addMetaDataItemToMainApplication( mainApplication, "com.google.android.nearby.messages.API_KEY", "myabikey" // 替换为你的实际API Key ); return androidManifest; }
3. 重新执行构建
清理缓存后重新构建:
npx expo prebuild --clean --platform android eas build --profile development --platform android
内容的提问来源于stack exchange,提问作者Niccolò Caselli
相关产品推荐
相关产品推荐

