Skip to content

Instantly share code, notes, and snippets.

@danmaas
Last active May 3, 2025 15:08
Show Gist options
  • Select an option

  • Save danmaas/c60af5fed9f55d2bc616ce302696540d to your computer and use it in GitHub Desktop.

Select an option

Save danmaas/c60af5fed9f55d2bc616ce302696540d to your computer and use it in GitHub Desktop.
CorePlane OkHttp DNS IPv4 preference
// Customize OkHttp to add IPv4 DNS preference
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.OkHttpClientFactory;
import okhttp3.OkHttpClient;
public class CorePlaneOkHttpClientFactory implements OkHttpClientFactory {
public OkHttpClient createNewNetworkModuleClient() {
return OkHttpClientProvider.createClientBuilder()
.dns(new CorePlaneOkHttpDNSSelector(CorePlaneOkHttpDNSSelector.IPvMode.IPV4_FIRST))
.build();
}
}
import okhttp3.Dns
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import java.util.logging.Logger
class CorePlaneOkHttpDNSSelector(private val mode: IPvMode) : Dns {
enum class IPvMode(val code: String) {
SYSTEM("system"),
IPV6_FIRST("ipv6"),
IPV4_FIRST("ipv4"),
IPV6_ONLY("ipv6only"),
IPV4_ONLY("ipv4only");
companion object {
@JvmStatic
fun fromString(ipMode: String): IPvMode =
IPvMode.values().find { it.code == ipMode } ?: throw Exception("Unknown value $ipMode")
}
}
override fun lookup(hostname: String): List<InetAddress> {
var addresses = Dns.SYSTEM.lookup(hostname)
addresses = when (mode) {
IPvMode.IPV6_FIRST -> addresses.sortedBy { Inet4Address::class.java.isInstance(it) }
IPvMode.IPV4_FIRST -> addresses.sortedBy { Inet6Address::class.java.isInstance(it) }
IPvMode.IPV6_ONLY -> addresses.filter({ Inet6Address::class.java.isInstance(it) })
IPvMode.IPV4_ONLY -> addresses.filter({ Inet4Address::class.java.isInstance(it) })
IPvMode.SYSTEM -> addresses
}
//logger.fine("DJMOKHTTP ($hostname): " + addresses.joinToString(", ") { it.toString() })
return addresses
}
companion object {
private val logger = Logger.getLogger(CorePlaneOkHttpDNSSelector::class.java.name)
}
}
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
public void onCreate() {
super.onCreate();
OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory());
}
// ...
}
@andreialecu
Copy link
Copy Markdown

Thanks for this!

For anyone else landing here that isn't a Java developer, for this to work I also needed to add package my.package.name; to the top of both the .java and .kt files. (same package name as you have at the top of MainApplication.java

And also needed apply plugin: 'kotlin-android' in android/app/build.gradle.

Here's a related thread with further details:
facebook/react-native#32730

@vankhoa01
Copy link
Copy Markdown

Hi @andreialecu : Related to issue aws-amplify/amplify-js#5539
I tried to apply this solution, but it don't work now
Do you have any other solution to fix this ?

Thank you so much

@andreialecu
Copy link
Copy Markdown

This is the solution I'm using and it works for me.

@andac-ozcan
Copy link
Copy Markdown

This fixed my issue too. Now I'm able to make network calls right after app opens, no need to wait timeout or fallback to ipv4 for 3 minutes.

@benchakalaka
Copy link
Copy Markdown

Helped me as well! Thank you

@enlo314
Copy link
Copy Markdown

enlo314 commented Sep 11, 2022

Hi, this solution works on debug but not on the release, any workaround?

@HodayaGruz
Copy link
Copy Markdown

when i try to compile with this i get:
cannot find symbol
OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory());
^
symbol: variable OkHttpClientProvider
location: class MainApplication
what is the solution?

@JKKholmatov
Copy link
Copy Markdown

JKKholmatov commented Aug 30, 2023

How can I apply this to an Expo app?
@andreialecu @andac-ozcan @danmaas

@JKKholmatov
Copy link
Copy Markdown

JKKholmatov commented Aug 31, 2023

when i try to compile with this i get: cannot find symbol OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); ^ symbol: variable OkHttpClientProvider location: class MainApplication what is the solution?

@HodayaGruz I also got this error, did You find how to solve it?

@andac-ozcan
Copy link
Copy Markdown

when i try to compile with this i get: cannot find symbol OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); ^ symbol: variable OkHttpClientProvider location: class MainApplication what is the solution?

@HodayaGruz I also got this error, did You find how to solve it?

You should import this:
import com.facebook.react.modules.network.OkHttpClientProvider;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment