Android applications frequently communicate with devices connected to the same Wi-Fi network. Examples include smart home devices, wireless printers, media streaming devices, local servers, IoT controllers, and network-connected cameras.
Previously, an application with the INTERNET permission could generally communicate with devices on the local network without requesting a separate runtime permission. This behavior changes in Android 17.
Android 17 introduces mandatory local network protection for applications targeting API level 37 or higher. These applications must either request the new ACCESS_LOCAL_NETWORK runtime permission or use supported system-mediated device pickers for specific discovery and connection scenarios.
This change is designed to protect user privacy. Unrestricted local network access can expose information about connected devices and network environments, which can contribute to device fingerprinting and user tracking.
In this article, you will learn how Android 17 handles local network access, how to declare and request the new permission, how to handle permission denial, and how to update existing Android applications without breaking their network functionality.
What Is Local Network Access?
A Local Area Network (LAN) is a network that connects devices within a limited area, such as a home, office, or building.
For example, a home Wi-Fi network may contain:
- Android phones and tablets.
- Smart TVs and streaming devices.
- Wireless printers.
- Smart lights and security cameras.
- Home automation controllers.
- Local development servers.
- Network-attached storage devices.
An Android application may need to discover these devices, establish connections, or exchange data with them.
Consider a smart home application that connects to a local controller at an address such as 192.168.1.100.
val address = InetAddress.getByName("192.168.1.100")
val socket = Socket(address, 8080)This connection uses the local network rather than a remote internet server.
Before Android 17's enforcement, applications could generally perform this operation with the INTERNET permission. Applications targeting Android 17 or higher must now obtain local network access through the new permission or an applicable system-mediated alternative.
What's New in Android 17?
Android 17 introduces the ACCESS_LOCAL_NETWORK runtime permission.
The permission is required for applications targeting Android 17 (API level 37) or higher when they directly access devices on the local network.
The important changes include:
- Local network access is blocked by default for applications targeting API level 37 or higher.
- Applications requiring broad LAN access must declare ACCESS_LOCAL_NETWORK in their manifest.
- Applications must request the permission at runtime before performing protected network operations.
- The permission belongs to the existing NEARBY_DEVICES permission group.
- Some device discovery and connection scenarios can use system-mediated pickers instead of requesting broad access.
- Applications targeting API level 36 or lower retain the existing compatibility behavior.
The restrictions apply to different networking approaches, not just direct socket connections. They also affect libraries such as OkHttp and Cronet when those libraries communicate with local network addresses.
Understanding the INTERNET and ACCESS_LOCAL_NETWORK Permissions
The INTERNET permission allows an application to open network connections. It does not provide unrestricted access to every network destination under Android 17's new enforcement model.
ACCESS_LOCAL_NETWORK specifically controls communication with devices on the local network.
| Permission | Purpose |
|---|---|
| INTERNET | Allows the application to open network connections |
| ACCESS_LOCAL_NETWORK | Allows protected communication with devices on the local network |
| NEARBY_WIFI_DEVICES | Supports specific Wi-Fi device discovery and connectivity use cases |
These permissions serve different purposes. NEARBY_WIFI_DEVICES is not a replacement for ACCESS_LOCAL_NETWORK when an application requires broad local network communication.
For an application targeting Android 17, declaring INTERNET alone is no longer sufficient for protected LAN operations.
Updating AndroidManifest.xml
The first step is to declare the new permission in AndroidManifest.xml.
Open the manifest file and add the following permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />The INTERNET permission is still required for ordinary network communication. ACCESS_LOCAL_NETWORK provides the additional authorization needed for protected local network traffic.
You do not need to remove existing permissions such as INTERNET or NEARBY_WIFI_DEVICES if your application still uses their functionality.
For example, a smart home application may use NEARBY_WIFI_DEVICES for Wi-Fi-related operations and ACCESS_LOCAL_NETWORK for direct communication with devices on the LAN.
The new permission is relevant when targeting API level 37 or higher. Applications targeting API level 36 or lower should not add or request it as part of the Android 17 migration.
Requesting Local Network Permission at Runtime
Declaring a permission in AndroidManifest.xml does not automatically grant it.
Applications targeting Android 17 must check the permission status and request it at runtime before accessing protected local network resources.
Android's Activity Result API provides a convenient way to request permissions.
Step 1: Add the Required Dependencies
Use AndroidX Activity and Core libraries in your application.
For example, the following dependencies provide the Activity Result API and ContextCompat utilities:
dependencies {
implementation("androidx.activity:activity-ktx:<version>")
implementation("androidx.core:core-ktx:<version>")
}Use compatible stable versions from your project's dependency management.
Step 2: Implement the Permission Request
The following example uses ActivityResultContracts.RequestPermission to request ACCESS_LOCAL_NETWORK.
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
class MainActivity : ComponentActivity() {
private val localNetworkPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
connectToLocalDevice()
} else {
Toast.makeText(
this,
"Local network access was denied",
Toast.LENGTH_LONG
).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkLocalNetworkPermission()
}
private fun checkLocalNetworkPermission() {
val permission = Manifest.permission.ACCESS_LOCAL_NETWORK
val isGranted = ContextCompat.checkSelfPermission(
this,
permission
) == PackageManager.PERMISSION_GRANTED
if (isGranted) {
connectToLocalDevice()
} else {
localNetworkPermissionLauncher.launch(permission)
}
}
private fun connectToLocalDevice() {
// Start local network communication.
}
}This implementation performs three important operations:
- Checks whether local network permission is already granted.
- Requests permission if it has not been granted.
- Starts the local network operation after permission is granted.
The application should not attempt the protected connection before the permission check has completed.
For an application with multiple screens, consider moving this logic into a dedicated permission manager or repository so that permission handling is not duplicated across activities.
Step 3: Handle Existing Permission Grants
ACCESS_LOCAL_NETWORK belongs to the NEARBY_DEVICES permission group.
If the user has already granted another permission in this group, such as a Bluetooth permission, Android may not display another permission prompt for local network access.
Therefore, do not assume that every permission request will display a dialog.
Always check the actual permission status using checkSelfPermission() rather than relying on whether a prompt appeared.
Connecting to a Local Device After Permission Is Granted
Once permission is granted, the application can perform its intended local network operation.
For example, consider an application that connects to a device running a local HTTP service.
The following example uses a background coroutine to avoid performing network operations on the main thread.
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.InetSocketAddress
import java.net.Socket
class MainActivity : ComponentActivity() {
private val permissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted ->
if (granted) {
connectToDevice()
} else {
showPermissionDeniedMessage()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkPermission()
}
private fun checkPermission() {
val permission = Manifest.permission.ACCESS_LOCAL_NETWORK
if (
ContextCompat.checkSelfPermission(
this,
permission
) == PackageManager.PERMISSION_GRANTED
) {
connectToDevice()
} else {
permissionLauncher.launch(permission)
}
}
private fun connectToDevice() {
lifecycleScope.launch {
val connected = withContext(Dispatchers.IO) {
try {
Socket().use { socket ->
socket.connect(
InetSocketAddress(
"192.168.1.100",
8080
),
5000
)
true
}
} catch (exception: Exception) {
false
}
}
if (connected) {
Toast.makeText(
this@MainActivity,
"Device connection successful",
Toast.LENGTH_SHORT
).show()
} else {
Toast.makeText(
this@MainActivity,
"Unable to connect to the device",
Toast.LENGTH_LONG
).show()
}
}
}
private fun showPermissionDeniedMessage() {
Toast.makeText(
this,
"Local network permission is required for this feature",
Toast.LENGTH_LONG
).show()
}
}The example connects to a local TCP service using a socket.
The connection is performed on Dispatchers.IO because network operations should not block the main thread.
In a production application, distinguish between permission failures, connection timeouts, unreachable devices, and other networking errors. A failed connection does not necessarily mean that the user denied permission.
Handling Permission Denial
Users may deny local network permission because they do not understand why an application needs access to nearby devices.
Applications should explain the feature before requesting permission.
For example, a smart home application can display the following message:
Connect to your home devices
This application needs local network access
to discover and communicate with your smart
lights, cameras, and home automation devices.
Allow access to continue.
Join the conversation! Your thoughts help the community grow.