Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add Interceptor/EventListener to all OkhttpClient #4228

Closed
Leaking opened this issue Aug 22, 2018 · 8 comments
Closed

How to add Interceptor/EventListener to all OkhttpClient #4228

Leaking opened this issue Aug 22, 2018 · 8 comments

Comments

@Leaking
Copy link

@Leaking Leaking commented Aug 22, 2018

Maybe your project have serveral OkhttpClients,you need to add your custom Interceptor/EventListener to every OkhttpClients one by one. But some OkhttpClients come from 3rd library, and you can't add your custom Interceptor/EventListener to them.

Is there any way to add add Interceptor/EventListener to all OkhttpClients conveniently.

@yschimke
Copy link
Collaborator

@yschimke yschimke commented Aug 23, 2018

Unfortunately not, you should file an issue with those libraries to allow you to share clients. As well common interceptors, it should provide benefits of sharing connections on the device.

@yschimke yschimke closed this Aug 23, 2018
@wuairc
Copy link

@wuairc wuairc commented Oct 10, 2018

We also need this feature to do accounting and monitoring logic. Add interceptors case by case is error prone, not future proof, and sometimes not doable.

@swankjesse
Copy link
Member

@swankjesse swankjesse commented Oct 11, 2018

Consider using dependency injection? It's my favorite way to make configuration uniform within an application.

@Leaking
Copy link
Author

@Leaking Leaking commented Oct 12, 2018

We also need this feature to do accounting and monitoring logic. Add interceptors case by case is error prone, not future proof, and sometimes not doable.

Hi, recently, I develop a plugin based on asm and gradle transform api to insert global interceptor for every okhttpclient, you can find it here https://github.com/Leaking/Hunter#okhttp-plugin

You can have a try.

@wuairc
Copy link

@wuairc wuairc commented Oct 15, 2018

Consider using dependency injection? It's my favorite way to make configuration uniform within an application.

Is that possible when the third party library that use OkHttpClient do not use DI and we can not change that situation ?

@wuairc
Copy link

@wuairc wuairc commented Oct 15, 2018

We also need this feature to do accounting and monitoring logic. Add interceptors case by case is error prone, not future proof, and sometimes not doable.

Hi, recently, I develop a plugin based on asm and gradle transform api to insert global interceptor for every okhttpclient, you can find it here https://github.com/Leaking/Hunter#okhttp-plugin

You can have a try.

Yes, this is the ideal way to do it, but a little overkill. Will try it

@SamB
Copy link

@SamB SamB commented Nov 30, 2019

Doesn't work quite as well when the code creating the OkHttpClint is in another APK does it? (I.e, Tachiyomi.)

In particular, suppose you're want to use something like Stetho that needs to be set up centrally but also needs an interceptor on each client; for this to work, you'd have to add something to the main APK's ABI, and then use that in extension APKs to set up the interceptor(s), which would make those APKs depend on the new main APK, which can be a pain when testing on a phone you use external builds of Tachiyomi on because then all of a sudden that extension can't load in the release build and you have to switch back to a release build of the extension to read your manga :-(. (Or, you know, migrate your state to a self-built "release" APK with the new ABI also.)

I was really hoping for a dynamic instrumentation option. (Sure would be nice if most of the work on dynamic instrumentation for Android didn't assume you wanted to instrument someone else's APK.)

I guess the simplest approach would be a way to set a list of interceptors to add to all new clients (and then remove when creating a builder from those clients, I guess, to avoid extra registrations). Then you could just add Stetho's interceptor to the list during app startup and be good to go.

@SamB
Copy link

@SamB SamB commented Nov 30, 2019

(Obviously, it could just as well be any other diagnostic facility with a central component and an interceptor.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants