Hey guys, I am making a library for Android which...
# rx
d
Hey guys, I am making a library for Android which would later be made available via maven. I want to make an optional library which can be added to the project and would work with the original main library to give
Rx support
as I do not want to increase the original library’s method count. This is similar to how you can add optional
Rx support
to
Retrofit
and Retrofit can work with
Rxjava
. I tried finding ways to do it but without results. Can anyone who has worked on it help me out on how to set it up? Maybe some code examples or websites on how to do it?
g
It really depends on your API. Retrofit provides way to extend library, so additional library for Rx support is pluggable. What exactly do you want to know?
m
Iirc some time ago Retrofit did also a trick - they checked if okhttp is on classpath and if it was then it was used otherwise they used httpurlconnection or sth. Now it’s okhttp only I guess but you can easily find it in commit history on github.
g
It very depends on your case and hard to recommend specific solution
Hooks for plugins sounds like a better and more clear solution, but it’s not always possible
u
Whats the issue here? Dont you just create new library which depends on your general library and rxjava2 also?
d
Thanks for the suggestion guys. I have gone through the retrofit code base and have noticed the way they are making use of
rxjava
. Im trying to implement the same. Right now by library has few methods which will return
Call<Object>
, without any change in the current library, I want to implement another library which works as an adapter to return
Observable<Object>
when using the main library methods.
g
But you should notice, that Retrofit uses reflection, but not just to support different return types
If you library just returns
Call
for async work, I don’t think that you can easily convert it to retrofit, only if you use extension functions that wrap Call to Observable. For Kotlin library, if the only thing that you want is different abstraction for callbacks, I would choose suspend functions and coroutines instead of Call/Observable etc