Expo云构建React Native:minSdkVersion修改被覆盖求助
解决Expo云构建中修改Android minSdkVersion到24的问题
问题回顾
使用Expo云构建React Native安卓应用时,需将minSdkVersion从21改为24,过程中遇到以下问题:
- 初始报错:
uses-sdk:minSdkVersion 21 cannot be smaller than version 24 declared in library - 修改AndroidManifest.xml为24后,出现提示:
uses-sdk:minSdkVersion value (24) specified in the manifest file is ignored. It is overridden by the value declared in the DSL or the variant API, or 1 if not declared/present. Current value is (21). - 尝试多种方法无效:错误的自定义插件、app.json配置未生效、字符串替换插件报错
.replace不是函数
可行解决方案
方案一:使用官方expo-build-properties插件(推荐)
这是最简洁的官方解决方案,确保正确安装并配置:
- 安装插件:
npx expo install expo-build-properties - 在
app.json或app.config.js中配置:{ "expo": { "plugins": [ [ "expo-build-properties", { "android": { "minSdkVersion": 24 } } ] ] } } - 重新提交云构建:
npx expo build:android
方案二:编写正确的自定义Config Plugin
如果官方插件不适用,编写自定义插件时需注意修改app模块的build.gradle(而非项目根目录的),并正确操作AST对象:
const { withAppBuildGradle } = require("@expo/config-plugins"); module.exports = function withMinSdkVersion(config) { return withAppBuildGradle(config, async (config) => { // 遍历AST找到android.defaultConfig节点并修改minSdkVersion if (config.modResults.contents.android) { const defaultConfig = config.modResults.contents.android.defaultConfig || {}; defaultConfig.minSdkVersion = 24; config.modResults.contents.android.defaultConfig = defaultConfig; } return config; }); };
在app.json中注册该插件:
{ "expo": { "plugins": ["./path-to-your-plugin.js"] } }
常见错误原因分析
- 错误使用
withProjectBuildGradle:minSdkVersion定义在app模块的build.gradle中,而非项目根目录的build.gradle,需使用withAppBuildGradle。 - 直接操作字符串:Expo的Config Plugin处理build.gradle时,
config.modResults.contents是AST对象,不是原始字符串,不能直接调用.replace(),需修改AST节点。 - app.json配置错误:
expo-build-properties需放在plugins数组中,而非顶级的android或mods节点下。
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

