In the gradle file, should we use `implementation(...
# multiplatform
d
In the gradle file, should we use
implementation()
or
api()
to import packages? The IntellijIDEA wizard for a compose multiplatform project uses
api()
, but the composejb examples use
implementation()
e.g.
Copy code
implementation(compose.foundation)
or
Copy code
api(compose.foundation)
b
Implementation for stuff you don't expose in your api surface. Api if you do.
The difference is that implmentation transitive dependencies won't show up for autocompletion on your lib consumer side.
d
I haven't really understood what you mean. So, in case of Compose, should we use
implementation
or
api
? Can you provide examples of libraries where we should use one or the other?
a
I try and use
implementation()
as much as possible. It’s more defensive, and protects against spreading dependencies far and wide. Use
api()
if you have a subproject/library that will always require consumers to also have that library - because then it saves the consumer copy+pasting a dependency, and risking getting out of sync. So, for example, imagine you have a
:test-utils
subproject that adds some helper functions for the kotlin.test library. It doesn’t make sense to use the
:test-utils
subproject without kotlin.test, so it makes sense for the
:test-utils
project to expose kotlin.test as an
api()
dependency. When other subprojects depend on your
:test-utils
project, use
implementation(project(":test-utils"))
. Note that they don’t also need to add
implementation(kotlin("test"))
, because it was exposed as an
api()
dependency - Gradle understands that kotlin.test is required by
:test-utils
. However, it doesn’t make sense for those subprojects to expose
:test-utils
as an
api()
dependency. The subprojects need test-utils for their own implementation for tests, but because tests are internal to a project, they don’t need to share that detail with anyone else.
since you’re asking in #multiplatform be aware that Kotlin source sets are hierarchical - if you define an
implementation()
dependency in
commonMain
, then it also be added as an implementation dependency in
jvmMain
,
jsMain
, etc https://kotlinlang.org/docs/multiplatform-hierarchy.html
r
The difference between
api
and
implementation
is only relevant if you are creating a library.
h
The difference between
api
and
implementation
is also relevant when modularizing your app. You could say that each module is a library on its own. So yes the difference is only relevant for library modules, which are also present in modularized apps
d
thanks everyone for the clarification