# DnsOptions.Builder

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)

# Builder

*** ** * ** ***

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

```
class Builder
```

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

Builder for [DnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions).

## Summary

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

|                                                                                                                                                                                                                                   Public methods                                                                                                                                                                                                                                    ||
|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [DnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions)                  | [build](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#build())`()` Creates and returns the final [DnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions) instance, based on the values in this builder.                                                                                            |
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | [setPersistHostCache](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setPersistHostCache(kotlin.Int))`(`state:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Sets whether the DNS cache should be persisted to disk.                                                                               |
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | [setPersistHostCachePeriod](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setPersistHostCachePeriod(java.time.Duration))`(`persistHostCachePeriod:` `[Duration](https://developer.android.com/reference/kotlin/java/time/Duration.html#)`)` Sets the minimum period between subsequent writes to disk for DNS cache persistence.    |
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | [setPreestablishConnectionsToStaleDnsResults](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setPreestablishConnectionsToStaleDnsResults(kotlin.Int))`(`state:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Sets whether Cronet should use stale cached DNS records to pre-establish connections. |
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | [setStaleDns](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setStaleDns(kotlin.Int))`(`state:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Sets whether to use stale DNS results at all.                                                                                                         |
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | [setStaleDnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setStaleDnsOptions(android.net.http.DnsOptions.StaleDnsOptions))`(`staleDnsOptions:` `[DnsOptions.StaleDnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.StaleDnsOptions)`)` Sets detailed configuration for stale DNS.     |
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | [setUseHttpStackDnsResolver](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setUseHttpStackDnsResolver(kotlin.Int))`(`state:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Enables the use of the HTTP-stack-specific DNS resolver.                                                                |

## Public constructors

### Builder

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
Builder()
```

## Public methods

### build

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun build(): DnsOptions
```

Creates and returns the final [DnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions) instance, based on the values in this builder.

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

### setPersistHostCache

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun setPersistHostCache(state: Int): DnsOptions.Builder
```

Sets whether the DNS cache should be persisted to disk.

Only relevant if [HttpEngine.Builder.setStoragePath(String)](https://developer.android.com/reference/kotlin/android/net/http/HttpEngine.Builder#setStoragePath(kotlin.String)) is set.

|                                                                                                                                                                                                                                                                                                          Parameters                                                                                                                                                                                                                                                                                                           ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `state` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): one of the DNS_OPTION_\* values Value is [android.net.http.DnsOptions#DNS_OPTION_UNSPECIFIED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_UNSPECIFIED:kotlin.Int), [android.net.http.DnsOptions#DNS_OPTION_ENABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_ENABLED:kotlin.Int), or [android.net.http.DnsOptions#DNS_OPTION_DISABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_DISABLED:kotlin.Int) |

|                                                                              Return                                                                              ||
|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | the builder for chaining This value cannot be `null`. |

### setPersistHostCachePeriod

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun setPersistHostCachePeriod(persistHostCachePeriod: Duration): DnsOptions.Builder
```

Sets the minimum period between subsequent writes to disk for DNS cache persistence.

Only relevant if [setPersistHostCache(int)](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setPersistHostCache(kotlin.Int)) is set to [DNS_OPTION_ENABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_ENABLED:kotlin.Int).

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

|                                                                              Return                                                                              ||
|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | the builder for chaining This value cannot be `null`. |

### setPreestablishConnectionsToStaleDnsResults

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun setPreestablishConnectionsToStaleDnsResults(state: Int): DnsOptions.Builder
```

Sets whether Cronet should use stale cached DNS records to pre-establish connections.

If enabled, Cronet will optimistically pre-establish connections to servers that matched the hostname at some point in the past and were cached but the cache entry expired. Such connections won't be used further until a new DNS lookup confirms the cached record was up to date.

To use cached DNS records straight away, use [setStaleDns(int)](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setStaleDns(kotlin.Int)) and [StaleDnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.StaleDnsOptions) configuration options.

This option may not be available for all networking protocols.

|                                                                                                                                                                                                                                                                                                          Parameters                                                                                                                                                                                                                                                                                                           ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `state` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): one of the DNS_OPTION_\* values Value is [android.net.http.DnsOptions#DNS_OPTION_UNSPECIFIED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_UNSPECIFIED:kotlin.Int), [android.net.http.DnsOptions#DNS_OPTION_ENABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_ENABLED:kotlin.Int), or [android.net.http.DnsOptions#DNS_OPTION_DISABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_DISABLED:kotlin.Int) |

|                                                                              Return                                                                              ||
|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | the builder for chaining This value cannot be `null`. |

### setStaleDns

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun setStaleDns(state: Int): DnsOptions.Builder
```

Sets whether to use stale DNS results at all.

|                                                                                                                                                                                                                                                                                                          Parameters                                                                                                                                                                                                                                                                                                           ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `state` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): one of the DNS_OPTION_\* values Value is [android.net.http.DnsOptions#DNS_OPTION_UNSPECIFIED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_UNSPECIFIED:kotlin.Int), [android.net.http.DnsOptions#DNS_OPTION_ENABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_ENABLED:kotlin.Int), or [android.net.http.DnsOptions#DNS_OPTION_DISABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_DISABLED:kotlin.Int) |

|                                                                              Return                                                                              ||
|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | the builder for chaining This value cannot be `null`. |

### setStaleDnsOptions

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun setStaleDnsOptions(staleDnsOptions: DnsOptions.StaleDnsOptions): DnsOptions.Builder
```

Sets detailed configuration for stale DNS. Only relevant if [setStaleDns(int)](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#setStaleDns(kotlin.Int)) is set.

|                                                                                Parameters                                                                                 ||
|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| `staleDnsOptions` | [DnsOptions.StaleDnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.StaleDnsOptions): This value cannot be `null`. |

|                                                                               Return                                                                               ||
|-----------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | this builder for chaining. This value cannot be `null`. |

### setUseHttpStackDnsResolver

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in [S Extensions 7](https://developer.android.com/sdkExtensions)  

```
fun setUseHttpStackDnsResolver(state: Int): DnsOptions.Builder
```

Enables the use of the HTTP-stack-specific DNS resolver.

Setting this to [DNS_OPTION_ENABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_ENABLED:kotlin.Int) is necessary for other functionality of [DnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions) to work, unless specified otherwise. See the [DnsOptions](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions) documentation for more details.

|                                                                                                                                                                                                                                                                                                          Parameters                                                                                                                                                                                                                                                                                                           ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `state` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): one of the DNS_OPTION_\* values Value is [android.net.http.DnsOptions#DNS_OPTION_UNSPECIFIED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_UNSPECIFIED:kotlin.Int), [android.net.http.DnsOptions#DNS_OPTION_ENABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_ENABLED:kotlin.Int), or [android.net.http.DnsOptions#DNS_OPTION_DISABLED](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions#DNS_OPTION_DISABLED:kotlin.Int) |

|                                                                              Return                                                                              ||
|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [DnsOptions.Builder](https://developer.android.com/reference/kotlin/android/net/http/DnsOptions.Builder#) | the builder for chaining This value cannot be `null`. |