求助:socket调用触发libcore.io.ErrnoException权限拒绝问题
libcore.io.ErrnoException: socket failed: EACCES (Permission denied) in Android Hey there, sorry to hear you're stuck with this stubborn socket permission error—let's walk through some common (and easy-to-miss) fixes that might resolve your issue, even if you've already tried basic permission additions.
1. Double-Check Permission Placement & Required Permissions
First, make sure your permissions are declared in the right spot in your AndroidManifest.xml. They need to be direct children of the <manifest> tag, not nested inside <application> or any other component.
You'll definitely need this core permission:
<uses-permission android:name="android.permission.INTERNET" />
If your app needs to check network state before creating the socket, add this too:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2. Avoid Privileged Port Numbers
If you're trying to bind to or connect to a port below 1024 (like 80, 21, etc.), regular Android apps don't have permission to use these—they're reserved for system-level processes. Switch to a port number 1024 or higher (e.g., 8080, 9000) and test again.
3. Account for Android Version-Specific Restrictions
Different Android versions have stricter network rules that might be blocking your socket:
- Android 9 (API 28)+: If your
targetSdkVersionis 28 or higher, and you're using unencrypted socket connections, ensure you've enabled cleartext traffic in your<application>tag:<application ... android:usesCleartextTraffic="true"> - Android 10 (API 29)+: If your app is running in the background, it might be restricted from accessing the network. To fix this, either:
- Use a foreground service with a notification to keep network access active
- Request the
REQUEST_IGNORE_BATTERY_OPTIMIZATIONSpermission to avoid battery saver blocking network
- Android 11+: Check if your app has been granted network permissions in Settings > Apps > [Your App] > Permissions. Sometimes system updates or app misconfigurations can revoke these automatically.
4. Review Network Security Config
If you've set up a networkSecurityConfig XML file, ensure it doesn't block outgoing socket connections. For example, if you've restricted domains, make sure your target socket host/IP is allowed, or remove unnecessary restrictions for testing.
5. Verify Socket Implementation in Code
A few code-related checks:
- Never create or use sockets on the main thread—this can lead to ANRs, and in some cases, indirect permission-related issues. Always use a background thread or coroutine.
- Double-check your socket initialization code. For example, if you're binding to a local address, make sure you're not using
InetAddress.getByName("localhost")incorrectly (try usingInetAddress.getLoopbackAddress()instead for local connections).
To help narrow this down further, could you share:
- The full content of your AndroidManifest.xml (redact any sensitive info)
- The specific code snippet where you're creating/using the socket
- Your app's
targetSdkVersionand the Android version you're testing on
内容的提问来源于stack exchange,提问作者Progga Ilma

