如何通过Expo插件为AndroidManifest添加网络权限?
Got it, let's get this sorted out for you! Since you can't edit AndroidManifest.xml directly in Expo, creating a custom config plugin is the right approach. Here's how to modify the example you found to add the INTERNET permission instead of removing one:
Step 1: Create the Config Plugin
Create a new file (e.g., withInternetPermission.js) in your project root with this code:
const { withAndroidManifest } = require("@expo/config-plugins"); module.exports = function withInternetPermission(config) { return withAndroidManifest(config, async (config) => { const androidManifest = config.modResults.manifest; // Define the INTERNET permission we want to add const internetPermission = { $: { "android:name": "android.permission.INTERNET", }, }; // Check if the permission already exists to avoid duplicates const hasInternetPermission = androidManifest["uses-permission"].some( (perm) => perm.$["android:name"] === "android.permission.INTERNET" ); if (!hasInternetPermission) { // Add the permission to the uses-permission array androidManifest["uses-permission"].push(internetPermission); } return config; }); };
Step 2: Use the Plugin in Your Project
Open your app.json or app.config.js and add the plugin to the plugins array:
{ "expo": { // ... other configs "plugins": ["./withInternetPermission.js"] } }
Step 3: Rebuild Your Android App
After adding the plugin, you'll need to rebuild your Android binary. Run:
eas build --platform android
Or if you're using local build tools:
expo prebuild --platform android
Quick Note on iOS
While Android requires explicit INTERNET permission, iOS allows network requests by default. However, if you're using HTTP (not HTTPS) to communicate with the Zebra printer, you'll need to add an exception to Info.plist to bypass Apple's App Transport Security (ATS). You can add this to your app.json:
{ "expo": { // ... other configs "ios": { "infoPlist": { "NSAppTransportSecurity": { "NSAllowsArbitraryLoads": true } } } } }
Note: For production, it's better to use NSExceptionDomains to specify only your printer's URL instead of the broad NSAllowsArbitraryLoads exception.
That should get your Android app talking to the Zebra printer via POST requests smoothly. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Opie_Eg

