Does anyone have a take on what are good practices...
# library-development
m
Does anyone have a take on what are good practices while writing an SDK in Kotlin? My SDK will need to work great with Java and this is my first time writing one so just if you have some points that I need to consider while writing it. Have someone done that from this channel?
b
Best tip for that I've ever heard is writing your tests in java. Helps a ton with spotting UX issues!
Other than that, you'll just need to be aware of @JvmField @JvmName @JvmStatic and similar annotations.
m
Thanks for the input! Will consider that.
m
A few advices: • No constructors with default params and/or builder DSLs -> use
Builders
, • No extension functions • No coroutines And everything @Big Chungus said
b
Extension functions are fine as long as they're not top-level. Just name the receiver argument via @receicer:JvmName("") and it will simply become first method argument from java
m
@mbonnin Very interested in hearing more about not using coroutines. I was actually considering using that since it will process huge amount of data.
m
I mean as long as it's not in your public API you're maybe fine but: • it's still a non-trivial transitive dependency • if you want to give your users some control over scheduling, you'd still have to bridge Dispatchers to Executors/Threads, whatever Java is using
m
Ah ok! It's actually public. So just use "regular" threads then I suppose?
m
Yea coroutines are hell to call from Java. You want to expose Threads/Executors instead
Unless you want to be multiplatform as well in which case, you'll certainly have to expose your own interface
l
You can use coroutines and provide a callback/future/whatever adapter for Java callers.
m
@louiscad do you have an example of a Library doing this successfully?
l
@mbonnin AndroidX Paging is one of those I believe.
m
Is there any Android developer still using Java ? 😅
l
What would be your guess? blob thinking upside down
m
😄
The documentation examples are quite nice though
l
The truth is ugly: Java is still around to some extent, though its usage in greenfield Android projects seems to be limited to the Flutter community.
m
I think it's a question whether you want to 1. support with existing codebases that haven't migrated to Kotlin yet or 2. or support java in new projects (Spring Boot, other backend stuff)
I don't see many Java enthousiasts adopting a lib that wraps coroutines for 2.