Junior — Middle
Can you explain the concept of Source Set in Kotlin Multiplatform and how it is used to organize code?
sobes.tech AI
Answer from AI
In Kotlin Multiplatform, Source Set is a logical group of source files combined by platform or common purpose. Source Set allows organizing code so that shared logic and platform-dependent code can be separated.
Main types of Source Set:
- commonMain — shared code for all platforms.
- commonTest — shared tests.
- platformMain (e.g., androidMain, iosMain) — code specific to a platform.
- platformTest — tests for a specific platform.
Using Source Set, most logic can be written once in commonMain, and platform-specific details can be implemented in platformMain. This simplifies maintenance and code reuse.
Example structure:
src/
commonMain/
kotlin/
com/example/common/
androidMain/
kotlin/
com/example/android/
iosMain/
kotlin/
com/example/ios/
In build.gradle.kts, dependencies can be configured, and dependencies between Source Sets can be specified, allowing flexible management of code for different platforms.