https://kotlinlang.org logo
#library-development
Title
# library-development
m

Marcus Cvjeticanin

03/05/2023, 11:17 AM
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

Big Chungus

03/05/2023, 11:53 AM
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

Marcus Cvjeticanin

03/05/2023, 12:52 PM
Thanks for the input! Will consider that.
m

mbonnin

03/05/2023, 12:56 PM
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

Big Chungus

03/05/2023, 12:58 PM
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

Marcus Cvjeticanin

03/05/2023, 1:14 PM
@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

mbonnin

03/05/2023, 1:15 PM
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

Marcus Cvjeticanin

03/05/2023, 2:50 PM
Ah ok! It's actually public. So just use "regular" threads then I suppose?
m

mbonnin

03/05/2023, 2:52 PM
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

louiscad

03/05/2023, 9:11 PM
You can use coroutines and provide a callback/future/whatever adapter for Java callers.
m

mbonnin

03/06/2023, 11:08 AM
@louiscad do you have an example of a Library doing this successfully?
l

louiscad

03/06/2023, 11:08 AM
@mbonnin AndroidX Paging is one of those I believe.
m

mbonnin

03/06/2023, 11:12 AM
Is there any Android developer still using Java ? 😅
l

louiscad

03/06/2023, 11:14 AM
What would be your guess? blob thinking upside down
m

mbonnin

03/06/2023, 11:14 AM
😄
The documentation examples are quite nice though
l

louiscad

03/06/2023, 11:22 AM
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

mbonnin

03/06/2023, 11:25 AM
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.
11 Views