Do we have any library similar to androidx.annotat...
# multiplatform
v
Do we have any library similar to androidx.annotation.RestrictTo in kotlin? I am trying to create modular kmp sdk with limited api surface. I want some of the apis to be accessible in my other sdks but restrict them in public. For similar case, androidx has androidx.annotation.RestrictTo. Do we have anything similar in kmp world?
m
androidx.annotation is kmp library despite being named "androidx" so you can use this annotation freely in your kmp project as long as you target the same or a subset of the platforms androidx.annotation targets. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:annotation/annotation/build.gradle;l=20
v
Thanks for the info
r
A lot of libraries define a custom opt-in annotation for this. Example from kotlinx.coroutines This doesn't prevent consumers from using the API, but it at least documents that they shouldn't and that there are no compatibility guarantees.
v
@russhwolf is there any possibility to restrict api?
r
What does restrict mean to you? It makes it a warning or error to use annotated declarations unless you explicitly opt into them. More details here
v
I’m looking for a way to hide api that are marked as public. So here’s context, my sdk consists of many kmp modules. In order to access one from another, I’d to mark as ‘public’. All those apis would be accessible in final sdk as well. Whoever using my sdk can access my internal apis. I want limit public api surface. AOSP has @hide annotation which will do exactly that. I’m looking for a similar solution in kmp.
@russhwolf any thoughts on this?
r
Using a custom opt-in annotation is the best solution to this that I'm aware of. APIs will technically be accessible but consumers will be required to add an annotation to use them and the annotation can make clear that it's non-public API. You may want to follow this issue which would add a hidden mode to suppress such API from auto-complete.
v
Thanks Russell
120 Views