Maybe your project have serveral OkhttpClients,you need to add your custom Interceptor/EventListener/Dns to every OkhttpClients one by one. But some OkhttpClients come from 3rd library, and you can't add your custom Interceptor/EventListener/Dns to them. I have filed a issue about this problem to Okhttp team.
OkHttp-Plugin can help you to achieve it, you can set a global Interceptor/EventListener/Dns to your all OkhttpClients.
Add some lines to your build.gradle
dependencies {
implementation 'com.quinn.hunter:hunter-okhttp-library:1.2.0'
}
repositories {
maven {
name = "GithubPackages"
url = uri("https://maven.pkg.github.com/Leaking/Hunter")
credentials {
username = 'Leaking'
password = '\u0067\u0068\u0070\u005f\u0058\u006d\u0038\u006e\u0062\u0057\u0031\u0053\u0053\u0042\u006a\u004a\u0064\u006f\u0071\u0048\u0064\u006b\u0036\u0034\u0077\u0031\u0054\u0066\u0074\u0071\u0052\u0046\u0068\u0042\u0032\u0047\u0057\u0037\u0046\u0070'
}
}
}
buildscript {
repositories {
maven {
name = "GithubPackages"
url = uri("https://maven.pkg.github.com/Leaking/Hunter")
credentials {
username = 'Leaking'
password = '\u0067\u0068\u0070\u005f\u0058\u006d\u0038\u006e\u0062\u0057\u0031\u0053\u0053\u0042\u006a\u004a\u0064\u006f\u0071\u0048\u0064\u006b\u0036\u0034\u0077\u0031\u0054\u0066\u0074\u0071\u0052\u0046\u0068\u0042\u0032\u0047\u0057\u0037\u0046\u0070'
}
}
google()
}
dependencies {
classpath 'com.quinn.hunter:hunter-okhttp-plugin:1.2.0'
classpath 'com.quinn.hunter:hunter-transform:1.2.0'
}
}
apply plugin: 'hunter-okhttp'
OkHttpHooker.installEventListenerFactory(CustomGlobalEventListener.FACTORY);
OkHttpHooker.installDns(new CustomGlobalDns());
OkHttpHooker.installInterceptor(new CustomGlobalInterceptor());
I recommend you to upgrade your okhttp to 3.11 or above, it almost costs nothing. If you want to use this plugin for okhttp below 3.11, please read this wiki page: wiki for okhttp-below-3.11