Expo React-Native应用部署亚马逊应用商店设备兼容问题求助
解决Expo React Native应用在Amazon Appstore自动添加camera.autofocus硬件要求的问题
问题背景
为孩子开发的Expo React Native应用,在Google Play Store部署正常,但上传到Amazon Appstore时,因平台自动添加的android.hardware.camera.autofocus硬件要求,仅支持最新款平板。查询后得知这是因为应用声明了CAMERA permission,亚马逊会自动强制添加该硬件要求,而我并未显式或隐式设置过这个要求。开发环境是Windows 10,通过Docker+eas-cli执行构建命令:eas build --platform android --profile production --local,之前尝试的两种方案都失败了:
- 预构建后手动修改AndroidManifest.xml无效,构建后配置被覆盖
- 自定义插件时出现
Cannot find module './plugins/withRequiredHardware'错误,导致构建失败
可行解决方案
方案一:使用Expo Config插件(推荐)
这种方式不需要手动修改原生文件,插件会在构建过程中自动修改AndroidManifest,避免被覆盖。
- 创建插件文件
在项目根目录新建plugins/withRequiredHardware.ts,写入以下代码:
import { ConfigPlugin, withAndroidManifest } from '@expo/config-plugins'; const withRequiredHardware: ConfigPlugin = (config) => { return withAndroidManifest(config, async (config) => { const manifest = config.modResults.manifest; // 查找是否已存在camera.autofocus的声明 const featureIndex = manifest['uses-feature']?.findIndex( feature => feature.$['android:name'] === 'android.hardware.camera.autofocus' ); if (featureIndex !== -1 && featureIndex !== undefined) { // 存在则修改为非必需 manifest['uses-feature'][featureIndex].$['android:required'] = 'false'; } else { // 不存在则添加新声明 if (!manifest['uses-feature']) manifest['uses-feature'] = []; manifest['uses-feature'].push({ $: { 'android:name': 'android.hardware.camera.autofocus', 'android:required': 'false', }, }); } return config; }); }; export default withRequiredHardware;
- 配置插件引用
在项目根目录的app.config.js(或app.json)中,将插件添加到plugins数组:
export default { expo: { // 其他原有配置(name、slug等) plugins: [ './plugins/withRequiredHardware.ts' ] } };
- 安装依赖
如果项目中没有安装@expo/config-plugins,执行以下命令安装:
npm install @expo/config-plugins --save-dev
- 重新构建
直接执行原来的构建命令即可:
eas build --platform android --profile production --local
方案二:预构建后锁定原生配置
如果坚持用预构建方式,需要确保修改后的AndroidManifest不会被构建流程覆盖:
- 执行预构建
npx expo prebuild --clean
- 修改AndroidManifest.xml
打开android/app/src/main/AndroidManifest.xml,添加以下代码到<manifest>标签内:
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
- 禁止构建时重新预构建
在项目根目录的eas.json中,修改production构建配置,设置prebuildCommand为空字符串:
{ "build": { "production": { "android": { "prebuildCommand": "" } } } }
- 执行构建
再次运行构建命令即可:
eas build --platform android --profile production --local
失败原因分析
- 预构建方案失败:默认情况下,eas build会重新执行预构建流程,覆盖你手动修改的原生文件,需要通过
prebuildCommand禁用该步骤。 - 自定义插件方案失败:大概率是插件文件路径写错、未安装
@expo/config-plugins依赖,或者插件代码中未正确导入Expo的配置模块。
内容的提问来源于stack exchange,提问作者MattWeiler
相关产品推荐
相关产品推荐

