求助:创建Expo配置插件以修改Android Manifest文件
解决方案:Expo插件修改Android Manifest配置
1. 创建Expo插件文件
在项目根目录新建plugins/modifyAndroidManifest.js,写入以下代码:
const { withAndroidManifest } = require('@expo/config-plugins'); module.exports = function modifyManifest(config) { return withAndroidManifest(config, async (config) => { const androidManifest = config.modResults.manifest; // 添加所有需要的<uses-feature>配置 const requiredFeatures = [ 'android.hardware.touchscreen', 'android.hardware.faketouch', 'android.hardware.telephony', 'android.hardware.camera', 'android.hardware.nfc', 'android.hardware.location.gps', 'android.hardware.microphone', 'android.hardware.sensor', ]; requiredFeatures.forEach(featureName => { // 检查是否已存在该feature,避免重复添加 const existingFeature = androidManifest['uses-feature']?.find(f => f.$['android:name'] === featureName); if (!existingFeature) { androidManifest['uses-feature'].push({ $: { 'android:name': featureName, 'android:required': 'false' } }); } else { // 如果已存在,强制设置required为false existingFeature.$['android:required'] = 'false'; } }); // 找到主Activity的<intent-filter>并添加LEANBACK_LAUNCHER类别 const mainActivity = androidManifest.application[0].activity.find(activity => activity['intent-filter']?.some(filter => filter.action?.some(action => action.$['android:name'] === 'android.intent.action.MAIN') ) ); if (mainActivity) { // 找到包含LAUNCHER类别的intent-filter const launchIntentFilter = mainActivity['intent-filter'].find(filter => filter.category?.some(category => category.$['android:name'] === 'android.intent.category.LAUNCHER') ); if (launchIntentFilter) { // 检查是否已存在LEANBACK_LAUNCHER类别 const hasLeanback = launchIntentFilter.category?.some(category => category.$['android:name'] === 'android.intent.category.LEANBACK_LAUNCHER' ); if (!hasLeanback) { launchIntentFilter.category.push({ $: { 'android:name': 'android.intent.category.LEANBACK_LAUNCHER' } }); } } } return config; }); };
2. 在app.json中注册插件
打开项目根目录的app.json(或app.config.js),在expo字段下添加plugins数组,引入刚才创建的插件:
{ "expo": { // 其他现有配置... "plugins": [ "./plugins/modifyAndroidManifest.js" ] } }
3. 验证配置
运行以下命令生成Android原生项目文件,检查Manifest是否已正确修改:
npx expo prebuild --platform android
查看android/app/src/main/AndroidManifest.xml,确认所有<uses-feature>和<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>已添加。
内容的提问来源于stack exchange,提问作者Tiago Godoy
相关产品推荐
相关产品推荐

