Daniele B
02/28/2023, 11:00 AMimplementation()
or api()
to import packages?
The IntellijIDEA wizard for a compose multiplatform project uses api()
, but the composejb examples use implementation()
e.g.
implementation(compose.foundation)
or
api(compose.foundation)
Big Chungus
02/28/2023, 11:02 AMBig Chungus
02/28/2023, 11:03 AMDaniele B
02/28/2023, 11:18 AMimplementation
or api
?
Can you provide examples of libraries where we should use one or the other?Adam S
02/28/2023, 11:20 AMimplementation()
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.Adam S
02/28/2023, 11:22 AMimplementation()
dependency in commonMain
, then it also be added as an implementation dependency in jvmMain
, jsMain
, etc
https://kotlinlang.org/docs/multiplatform-hierarchy.htmlRobert Jaros
02/28/2023, 11:27 AMapi
and implementation
is only relevant if you are creating a library.Hylke Bron
02/28/2023, 12:49 PMapi
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 appsDaniele B
02/28/2023, 3:53 PM