我需要创建一个 expo 插件文件并在 app.json 中调用它,以便在 android 清单中添加以下属性。
<uses-feature android:name="android.hardware.touchscreen"
android:required="false"/>
<uses-feature android:name="android.hardware.faketouch"
android:required="false"/>
<uses-feature android:name="android.hardware.telephony"
android:required="false"/>
<uses-feature android:name="android.hardware.camera"
android:required="false"/>
<uses-feature android:name="android.hardware.nfc"
android:required="false"/>
<uses-feature android:name="android.hardware.location.gps"
android:required="false"/>
<uses-feature android:name="android.hardware.microphone"
android:required="false"/>
<uses-feature android:name="android.hardware.sensor"
android:required="false"/>
<intent-filter>
...
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
我是一名使用 React Native Expo 的 javascript 开发人员,所以我没有自己创建文件的技能,我花了一整天的时间试图弄清楚这一点,但基本上没有来自 Expo 的有用文档。
我在这里找到了类似的帖子:link
我按照以下步骤添加了意图过滤器
const { AndroidConfig, withAndroidManifest } = require("@expo/config-plugins");
const { getMainApplicationOrThrow, addMetaDataItemToMainApplication } =
AndroidConfig.Manifest;
function addAttributesToMainActivity(androidManifest) {
const { manifest } = androidManifest;
if (!Array.isArray(manifest["application"])) {
console.warn(
"withWordlLineIntentActivity: No application array in manifest?"
);
return androidManifest;
}
const application = manifest["application"].find(
(item) => item.$["android:name"] === ".MainApplication"
);
if (!application) {
console.warn("withWordlLineIntentActivity: No .MainApplication?");
return androidManifest;
}
if (!Array.isArray(application["activity"])) {
console.warn(
"withWordlLineIntentActivity: No activity array in .MainApplication?"
);
return androidManifest;
}
const activity = application["activity"].find(
(item) => item.$["android:name"] === ".MainActivity"
);
if (!activity) {
console.warn("withWordlLineIntentActivity: No .MainActivity?");
return androidManifest;
}
const action = {};
action.$ = {
...action.$,
...{
"android:name": "com.domain.action.PROCESS_TRANSACTION",
},
};
const intent = { action: action };
activity["intent-filter"].push(intent);
return androidManifest;
}
module.exports = function withIntentActivity(config) {
return withAndroidManifest(config, (config) => {
config.modResults = addAttributesToMainActivity(config.modResults);
return config;
});
};
在 app.json 添加对创建的插件的引用
“插件”:[ “./plugins/example-intent.js” ],
运行 expo prebuild 来查看结果
希望对你有一点帮助。
这里是通过自定义配置插件编辑 Android Manifest 的当前文档。 https://docs.expo.dev/config-plugins/development-and-debugging/#modify-androidmanifestxml
import { AndroidConfig, ConfigPlugin, withAndroidManifest } from 'expo/config-plugins';
import { ExpoConfig } from 'expo/config';
// Using helpers keeps error messages unified and helps cut down on XML format changes.
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = AndroidConfig.Manifest;
export const withMyCustomConfig: ConfigPlugin = 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: Pick<ExpoConfig, 'android'>,
androidManifest: AndroidConfig.Manifest.AndroidManifest
): Promise<AndroidConfig.Manifest.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`
'my-app-id-key',
// value for `android:value`
appId
);
return androidManifest;
}