# VpnService.Builder

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# Builder

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/net/VpnService.Builder "View this page in Java")  

```
open class Builder
```

|---|------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                   ||
| ↳ | [android.net.VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) |

Helper class to create a VPN interface. This class should be always used within the scope of the outer[VpnService](https://developer.android.com/reference/kotlin/android/net/VpnService).

## Summary

|                                               Public constructors                                                ||
|---------------------------------------------------------------------------------------------------------------|---|
| [Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#Builder())`()` <br /> |

|                                                                                                                                                                                                                                                                     Public methods                                                                                                                                                                                                                                                                     ||
|-------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addAddress](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addAddress(kotlin.String,%20kotlin.Int))`(`address:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `prefixLength:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Convenience method to add a network address to the VPN interface using a numeric address string. |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addAddress](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addAddress(java.net.InetAddress,%20kotlin.Int))`(`address:` `[InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#)`, `prefixLength:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Add a network address to the VPN interface.                                       |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addAllowedApplication](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addAllowedApplication(kotlin.String))`(`packageName:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Adds an application that's allowed to access the VPN connection.                                                                                                                   |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addDisallowedApplication](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addDisallowedApplication(kotlin.String))`(`packageName:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Adds an application that's denied access to the VPN connection.                                                                                                              |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addDnsServer](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addDnsServer(kotlin.String))`(`address:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Convenience method to add a DNS server to the VPN connection using a numeric address string.                                                                                                             |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addDnsServer](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addDnsServer(java.net.InetAddress))`(`address:` `[InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#)`)` Add a DNS server to the VPN connection.                                                                                                                                                   |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addRoute(android.net.IpPrefix))`(`prefix:` `[IpPrefix](https://developer.android.com/reference/kotlin/android/net/IpPrefix)`)` Add a network route to the VPN interface.                                                                                                                                                                   |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addRoute(kotlin.String,%20kotlin.Int))`(`address:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `prefixLength:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Convenience method to add a network route to the VPN interface using a numeric address string.       |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addRoute(java.net.InetAddress,%20kotlin.Int))`(`address:` `[InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#)`, `prefixLength:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Add a network route to the VPN interface.                                             |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [addSearchDomain](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addSearchDomain(kotlin.String))`(`domain:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Add a search domain to the DNS resolver.                                                                                                                                                            |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [allowBypass](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#allowBypass())`()` Allows all apps to bypass this VPN connection.                                                                                                                                                                                                                                                                       |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [allowFamily](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#allowFamily(kotlin.Int))`(`family:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Allows traffic from the specified address family.                                                                                                                                                                    |
| open[ParcelFileDescriptor](https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.html#)? | [establish](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#establish())`()` Create a VPN interface using the parameters supplied to this builder.                                                                                                                                                                                                                                                    |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [excludeRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#excludeRoute(android.net.IpPrefix))`(`prefix:` `[IpPrefix](https://developer.android.com/reference/kotlin/android/net/IpPrefix)`)` Exclude a network route from the VPN interface.                                                                                                                                                     |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setBlocking](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setBlocking(kotlin.Boolean))`(`blocking:` `[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`)` Sets the VPN interface's file descriptor to be in blocking/non-blocking mode.                                                                                                                          |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setConfigureIntent](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setConfigureIntent(android.app.PendingIntent))`(`intent:` `[PendingIntent](https://developer.android.com/reference/kotlin/android/app/PendingIntent.html#)`)` Set the[PendingIntent](https://developer.android.com/reference/kotlin/android/app/PendingIntent.html#)to an activity for users to configure the VPN connection.    |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setHttpProxy](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setHttpProxy(android.net.ProxyInfo))`(`proxyInfo:` `[ProxyInfo](https://developer.android.com/reference/kotlin/android/net/ProxyInfo)`)` Sets an HTTP proxy for the VPN network.                                                                                                                                                       |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setMetered](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setMetered(kotlin.Boolean))`(`isMetered:` `[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`)` Marks the VPN network as metered.                                                                                                                                                                       |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setMtu](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setMtu(kotlin.Int))`(`mtu:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Set the maximum transmission unit (MTU) of the VPN interface.                                                                                                                                                                     |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setSession](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setSession(kotlin.String))`(`session:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Set the name of this session.                                                                                                                                                                                |
| open[VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)          | [setUnderlyingNetworks](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setUnderlyingNetworks(kotlin.Array))`(`networks:` `[Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)<[Network](https://developer.android.com/reference/kotlin/android/net/Network)!>?`)` Sets the underlying networks used by the VPN for its upstream connections.                              |

## Public constructors

### Builder

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
Builder()
```

## Public methods

### addAddress

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addAddress(
    address: String, 
    prefixLength: Int
): VpnService.Builder
```

Convenience method to add a network address to the VPN interface using a numeric address string. See[InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#)for the definitions of numeric address formats. Adding an address implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily

|                                                       Parameters                                                        ||
|-----------|--------------------------------------------------------------------------------------------------------------|
| `address` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                            Exceptions                            ||
|--------------------------------------|----------------------------|
| `java.lang.IllegalArgumentException` | if the address is invalid. |

**See Also**

- [#addAddress(InetAddress, int)](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addAddress(java.net.InetAddress,%20kotlin.Int))  

### addAddress

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addAddress(
    address: InetAddress, 
    prefixLength: Int
): VpnService.Builder
```

Add a network address to the VPN interface. Both IPv4 and IPv6 addresses are supported. At least one address must be set before calling[establish](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#establish()). Adding an address implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily

|                                                           Parameters                                                            ||
|-----------|----------------------------------------------------------------------------------------------------------------------|
| `address` | [InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                            Exceptions                            ||
|--------------------------------------|----------------------------|
| `java.lang.IllegalArgumentException` | if the address is invalid. |

### addAllowedApplication

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addAllowedApplication(packageName: String): VpnService.Builder
```

Adds an application that's allowed to access the VPN connection. If this method is called at least once, only applications added through this method (and no others) are allowed access. Else (if this method is never called), all applications are allowed by default. If some applications are added, other, un-added applications will use networking as if the VPN wasn't running. A[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)may have only a set of allowed applications OR a set of disallowed ones, but not both. Calling this method after[addDisallowedApplication](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addDisallowedApplication(kotlin.String))has already been called, or vice versa, will throw an[UnsupportedOperationException](https://developer.android.com/reference/kotlin/java/lang/UnsupportedOperationException.html#).`packageName`must be the canonical name of a currently installed application.[PackageManager.NameNotFoundException](https://developer.android.com/reference/kotlin/android/content/pm/PackageManager.NameNotFoundException.html#)is thrown if there's no such application.

|                                                                                           Parameters                                                                                            ||
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `packageName` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):The full name (e.g.: "com.google.apps.contacts") of an application. This value cannot be`null`. |

|                                                                                                                                   Return                                                                                                                                   ||
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining method calls. This value cannot be`null`. |

|                                           Exceptions                                           ||
|-----------------------------------------------------------|-------------------------------------|
| `android.content.pm.PackageManager.NameNotFoundException` | If the application isn't installed. |

### addDisallowedApplication

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addDisallowedApplication(packageName: String): VpnService.Builder
```

Adds an application that's denied access to the VPN connection. By default, all applications are allowed access, except for those denied through this method. Denied applications will use networking as if the VPN wasn't running. A[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)may have only a set of allowed applications OR a set of disallowed ones, but not both. Calling this method after[addAllowedApplication](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addAllowedApplication(kotlin.String))has already been called, or vice versa, will throw an[UnsupportedOperationException](https://developer.android.com/reference/kotlin/java/lang/UnsupportedOperationException.html#).`packageName`must be the canonical name of a currently installed application.[PackageManager.NameNotFoundException](https://developer.android.com/reference/kotlin/android/content/pm/PackageManager.NameNotFoundException.html#)is thrown if there's no such application.

|                                                                                           Parameters                                                                                            ||
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `packageName` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):The full name (e.g.: "com.google.apps.contacts") of an application. This value cannot be`null`. |

|                                                                                                                                   Return                                                                                                                                   ||
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining method calls. This value cannot be`null`. |

|                                           Exceptions                                           ||
|-----------------------------------------------------------|-------------------------------------|
| `android.content.pm.PackageManager.NameNotFoundException` | If the application isn't installed. |

### addDnsServer

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addDnsServer(address: String): VpnService.Builder
```

Convenience method to add a DNS server to the VPN connection using a numeric address string. See[InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#)for the definitions of numeric address formats. Adding a server implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily

|                                                       Parameters                                                        ||
|-----------|--------------------------------------------------------------------------------------------------------------|
| `address` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                            Exceptions                            ||
|--------------------------------------|----------------------------|
| `java.lang.IllegalArgumentException` | if the address is invalid. |

**See Also**

- [#addDnsServer(InetAddress)](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addDnsServer(java.net.InetAddress))  

### addDnsServer

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addDnsServer(address: InetAddress): VpnService.Builder
```

Add a DNS server to the VPN connection. Both IPv4 and IPv6 addresses are supported. If none is set, the DNS servers of the default network will be used. Adding a server implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily

|                                                           Parameters                                                            ||
|-----------|----------------------------------------------------------------------------------------------------------------------|
| `address` | [InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                            Exceptions                            ||
|--------------------------------------|----------------------------|
| `java.lang.IllegalArgumentException` | if the address is invalid. |

### addRoute

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addRoute(prefix: IpPrefix): VpnService.Builder
```

Add a network route to the VPN interface. Both IPv4 and IPv6 routes are supported. Adding a route implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily Calling this method overrides previous calls to[excludeRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#excludeRoute(android.net.IpPrefix))for the same destination. If multiple routes match the packet destination, route with the longest prefix takes precedence.

|                                                      Parameters                                                       ||
|----------|-------------------------------------------------------------------------------------------------------------|
| `prefix` | [IpPrefix](https://developer.android.com/reference/kotlin/android/net/IpPrefix):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                           Exceptions                           ||
|--------------------------------------|--------------------------|
| `java.lang.IllegalArgumentException` | if the route is invalid. |

### addRoute

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addRoute(
    address: String, 
    prefixLength: Int
): VpnService.Builder
```

Convenience method to add a network route to the VPN interface using a numeric address string. See[InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#)for the definitions of numeric address formats. Adding a route implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily Calling this method overrides previous calls to[excludeRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#excludeRoute(android.net.IpPrefix))for the same destination. If multiple routes match the packet destination, route with the longest prefix takes precedence.

|                                                       Parameters                                                        ||
|-----------|--------------------------------------------------------------------------------------------------------------|
| `address` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                           Exceptions                           ||
|--------------------------------------|--------------------------|
| `java.lang.IllegalArgumentException` | if the route is invalid. |

**See Also**

- [#addRoute(InetAddress, int)](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#addRoute(java.net.InetAddress,%20kotlin.Int))  

### addRoute

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addRoute(
    address: InetAddress, 
    prefixLength: Int
): VpnService.Builder
```

Add a network route to the VPN interface. Both IPv4 and IPv6 routes are supported. Adding a route implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily Calling this method overrides previous calls to[excludeRoute](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#excludeRoute(android.net.IpPrefix))for the same destination. If multiple routes match the packet destination, route with the longest prefix takes precedence.

|                                                           Parameters                                                            ||
|-----------|----------------------------------------------------------------------------------------------------------------------|
| `address` | [InetAddress](https://developer.android.com/reference/kotlin/java/net/InetAddress.html#):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                           Exceptions                           ||
|--------------------------------------|--------------------------|
| `java.lang.IllegalArgumentException` | if the route is invalid. |

### addSearchDomain

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun addSearchDomain(domain: String): VpnService.Builder
```

Add a search domain to the DNS resolver.

|                                                       Parameters                                                       ||
|----------|--------------------------------------------------------------------------------------------------------------|
| `domain` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

### allowBypass

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun allowBypass(): VpnService.Builder
```

Allows all apps to bypass this VPN connection. By default, all traffic from apps is forwarded through the VPN interface and it is not possible for apps to side-step the VPN. If this method is called, apps may use methods such as[ConnectivityManager.bindProcessToNetwork](https://developer.android.com/reference/kotlin/android/net/ConnectivityManager#bindProcessToNetwork(android.net.Network))to instead send/receive directly over the underlying network or any other network they have permissions for.

|                                                                                                                                    Return                                                                                                                                     ||
|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining of method calls. This value cannot be`null`. |

### allowFamily

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun allowFamily(family: Int): VpnService.Builder
```

Allows traffic from the specified address family. By default, if no address, route or DNS server of a specific family (IPv4 or IPv6) is added to this VPN, then all outgoing traffic of that family is blocked. If any address, route or DNS server is added, that family is allowed. This method allows an address family to be unblocked even without adding an address, route or DNS server of that family. Traffic of that family will then typically fall-through to the underlying network if it's supported.`family`must be either`AF_INET`(for IPv4) or`AF_INET6`(for IPv6).[IllegalArgumentException](https://developer.android.com/reference/kotlin/java/lang/IllegalArgumentException.html#)is thrown if it's neither.

|                                                                Parameters                                                                 ||
|----------|---------------------------------------------------------------------------------------------------------------------------------|
| `family` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):The address family (`AF_INET`or`AF_INET6`) to allow. |

|                                                                                                                                    Return                                                                                                                                     ||
|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining of method calls. This value cannot be`null`. |

### establish

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun establish(): ParcelFileDescriptor?
```

Create a VPN interface using the parameters supplied to this builder. The interface works on IP packets, and a file descriptor is returned for the application to access them. Each read retrieves an outgoing packet which was routed to the interface. Each write injects an incoming packet just like it was received from the interface. The file descriptor is put into non-blocking mode by default to avoid blocking Java threads. To use the file descriptor completely in native space, see[ParcelFileDescriptor.detachFd()](https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.html#detachFd()). The application MUST close the file descriptor when the VPN connection is terminated. The VPN interface will be removed and the network will be restored by the system automatically.

To avoid conflicts, there can be only one active VPN interface at the same time. Usually network parameters are never changed during the lifetime of a VPN connection. It is also common for an application to create a new file descriptor after closing the previous one. However, it is rare but not impossible to have two interfaces while performing a seamless handover. In this case, the old interface will be deactivated when the new one is created successfully. Both file descriptors are valid but now outgoing packets will be routed to the new interface. Therefore, after draining the old file descriptor, the application MUST close it and start using the new file descriptor. If the new interface cannot be created, the existing interface and its file descriptor remain untouched.

An exception will be thrown if the interface cannot be created for any reason. However, this method returns`null`if the application is not prepared or is revoked. This helps solve possible race conditions between other VPN applications.

|                                                                                                                                            Return                                                                                                                                            ||
|---------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ParcelFileDescriptor](https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.html#)? | [ParcelFileDescriptor](https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.html#)of the VPN interface, or`null`if the application is not prepared. |

|                                               Exceptions                                               ||
|--------------------------------------|------------------------------------------------------------------|
| `java.lang.IllegalArgumentException` | if a parameter is not accepted by the operating system.          |
| `java.lang.IllegalStateException`    | if a parameter cannot be applied by the operating system.        |
| `java.lang.SecurityException`        | if the service is not properly declared in`AndroidManifest.xml`. |

**See Also**

- [android.net.VpnService](https://developer.android.com/reference/kotlin/android/net/VpnService)  

### excludeRoute

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun excludeRoute(prefix: IpPrefix): VpnService.Builder
```

Exclude a network route from the VPN interface. Both IPv4 and IPv6 routes are supported. Calling this method overrides previous calls to #addRoute for the same destination. If multiple routes match the packet destination, route with the longest prefix takes precedence.

|                                                      Parameters                                                       ||
|----------|-------------------------------------------------------------------------------------------------------------|
| `prefix` | [IpPrefix](https://developer.android.com/reference/kotlin/android/net/IpPrefix):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                           Exceptions                           ||
|--------------------------------------|--------------------------|
| `java.lang.IllegalArgumentException` | if the route is invalid. |

### setBlocking

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setBlocking(blocking: Boolean): VpnService.Builder
```

Sets the VPN interface's file descriptor to be in blocking/non-blocking mode. By default, the file descriptor returned by[establish](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#establish())is non-blocking.

|                                                                              Parameters                                                                               ||
|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `blocking` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html):True to put the descriptor into blocking mode; false for non-blocking. |

|                                                                                                                                   Return                                                                                                                                   ||
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining method calls. This value cannot be`null`. |

### setConfigureIntent

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setConfigureIntent(intent: PendingIntent): VpnService.Builder
```

Set the[PendingIntent](https://developer.android.com/reference/kotlin/android/app/PendingIntent.html#)to an activity for users to configure the VPN connection. If it is not set, the button to configure will not be shown in system-managed dialogs.

|                                                              Parameters                                                               ||
|----------|-----------------------------------------------------------------------------------------------------------------------------|
| `intent` | [PendingIntent](https://developer.android.com/reference/kotlin/android/app/PendingIntent.html#):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

### setHttpProxy

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setHttpProxy(proxyInfo: ProxyInfo): VpnService.Builder
```

Sets an HTTP proxy for the VPN network.

This proxy is only a recommendation and it is possible that some apps will ignore it.

PAC proxies are not supported over VPNs.

Apps that do use the proxy cannot distinguish between routes handled and not handled by the VPN and will try to access HTTP resources over the proxy regardless of the destination. In practice this means using a proxy with a split tunnel generally won't work as expected, because HTTP accesses on routes not handled by the VPN will not reach as the proxy won't be available outside of the VPN network.

|                                                         Parameters                                                         ||
|-------------|---------------------------------------------------------------------------------------------------------------|
| `proxyInfo` | [ProxyInfo](https://developer.android.com/reference/kotlin/android/net/ProxyInfo):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

### setMetered

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setMetered(isMetered: Boolean): VpnService.Builder
```

Marks the VPN network as metered. A VPN network is classified as metered when the user is sensitive to heavy data usage due to monetary costs and/or data limitations. In such cases, you should set this to`true`so that apps on the system can avoid doing large data transfers. Otherwise, set this to`false`. Doing so would cause VPN network to inherit its meteredness from its underlying networks.

VPN apps targeting[android.os.Build.VERSION_CODES#Q](https://developer.android.com/reference/kotlin/android/os/Build.VERSION_CODES.html#Q:kotlin.Int)or above will be considered metered by default.

|                                                                                           Parameters                                                                                           ||
|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `isMetered` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html):`true`if VPN network should be treated as metered regardless of underlying network meteredness |

|                                                                                                                                  Return                                                                                                                                   ||
|------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining method calls This value cannot be`null`. |

**See Also**

- [#setUnderlyingNetworks(Network[])](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#setUnderlyingNetworks(kotlin.Array))
- [android.net.ConnectivityManager#isActiveNetworkMetered()](https://developer.android.com/reference/kotlin/android/net/ConnectivityManager#isActiveNetworkMetered())  

### setMtu

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setMtu(mtu: Int): VpnService.Builder
```

Set the maximum transmission unit (MTU) of the VPN interface. If it is not set, the default value in the operating system will be used.

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

|                             Exceptions                              ||
|--------------------------------------|-------------------------------|
| `java.lang.IllegalArgumentException` | if the value is not positive. |

### setSession

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setSession(session: String): VpnService.Builder
```

Set the name of this session. It will be displayed in system-managed dialogs and notifications. This is recommended not required.

|                                                       Parameters                                                        ||
|-----------|--------------------------------------------------------------------------------------------------------------|
| `session` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):This value cannot be`null`. |

|                                                              Return                                                               ||
|------------------------------------------------------------------------------------------------------|-----------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | This value cannot be`null`. |

### setUnderlyingNetworks

Added in[API level 22](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun setUnderlyingNetworks(networks: Array<Network!>?): VpnService.Builder
```

Sets the underlying networks used by the VPN for its upstream connections.

|                                                                                                                                   Parameters                                                                                                                                    ||
|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `networks` | [Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)\<[Network](https://developer.android.com/reference/kotlin/android/net/Network)!\>?:An array of networks the VPN uses to tunnel traffic to/from its servers. This value may be`null`. |

|                                                                                                                                   Return                                                                                                                                   ||
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [VpnService.Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#) | this[Builder](https://developer.android.com/reference/kotlin/android/net/VpnService.Builder#)object to facilitate chaining method calls. This value cannot be`null`. |

**See Also**

- [android.net.VpnService#setUnderlyingNetworks](https://developer.android.com/reference/kotlin/android/net/VpnService#setUnderlyingNetworks(kotlin.Array))