kmp-feature-module
Use when adding a new feature or library module to a Kotlin Multiplatform project - "create a module", "modularize this feature", a :feature module, settings.gradle.kts include lines, a logic/ui module split so SwiftUI can consume shared logic, an umbrella shared iOS framework, e
Install
npx skills add https://github.com/Kushpawan/kmp-mobile-skills/tree/main/skills/kmp-feature-module
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install kushpawan-kmp-mobile-skills@llmmart
git clone https://github.com/Kushpawan/kmp-mobile-skills.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole kushpawan/kmp-mobile-skills collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
KMP feature module
A feature module is a Gradle project with one owner, a narrow public API, and exactly two ways in: the app's DI graph and the app's navigation host. Pick the module shape the project already uses. The most expensive mistake is a module that builds and runs on Android while the iOS framework silently does not contain it.
When to use
- Creating
:feature:<name>or a reusable library module in a KMP build. - Splitting a feature out of a monolithic
:sharedor:composeApp. - Adding a
:logic+:uipair for an app whose iOS UI is SwiftUI. - A new module compiles for Android but its types are missing in Swift.
When not to use
- Adding a platform API inside an existing module: use
kmp-expect-actual. - Adding or upgrading a dependency or Gradle plugin: use
kmp-gradle-version-catalog. - Auditing an existing module: use
kmp-module-health. - Migrating existing modules to AGP 9 (
androidTarget()to the Android-KMP library plugin, splitting the app entry point out): use JetBrains'kotlin-tooling-agp9-migration. - Turning an Android-only app into KMP: use
android-to-kmp-migration.
Step 0 — Read the project first
Do not write a build file from memory. Read the build, record the answers, then copy the closest existing sibling module.
cat settings.gradle.kts # include(...), includeBuild, catalog path
find . -name build.gradle.kts -not -path '*/build/*' | sort # existing modules
grep -nE '^(agp|kotlin|composeMultiplatform|android-?[Gg]radle[A-Za-z-]*) *=' gradle/libs.versions.toml
grep -rnE 'androidTarget\(|androidLibrary *\{|kotlin\.multiplatform\.library|com\.android\.library' \
--include=build.gradle.kts . # which Android-target style siblings use
grep -rln "binaries.framework" --include=build.gradle.kts . # who produces the iOS framework
grep -rn "export(" --include=build.gradle.kts . # what Swift can see today (Obj-C framework)
grep -rnE "swift *\{|swiftExport" --include=build.gradle.kts . # Swift export instead of a framework?
grep -rlE "composeMultiplatform|org.jetbrains.compose" --include=build.gradle.kts .
ls build-logic buildSrc 2>/dev/null # convention plugins
grep -rl "ComposeUIViewController" --include='*.kt' . | head -3 # iOS renders Compose?
| Record | Where it shows up |
|---|---|
Module naming (:feature:notes, :notes, :features:notes-impl) |
include(...) lines |
| Kotlin and AGP versions | gradle/libs.versions.toml |
Android target style: com.android.kotlin.multiplatform.library + kotlin { android { } } (current), kotlin { androidLibrary { } } (deprecated since AGP 9.1.0-alpha09), or com.android.library + androidTarget() (legacy) |
sibling build files |
Swift interop: Objective-C framework (binaries.framework + export(...)) or Swift export (export { swift { } }) |
the module iOS links |
iOS targets: iosArm64, iosSimulatorArm64, maybe iosX64 |
sibling build files — a new module declares the same set as its consumers |
Convention plugins (id("com.example.kmp.library")) |
build-logic/, buildSrc/ |
| DI: Koin, kotlin-inject, Metro, manual | startKoin, @Component |
| Navigation: Navigation Compose, Navigation 3, Decompose, Voyager | the NavHost or root component |
Presentation: ViewModel + StateFlow, or an MVI base such as MviViewModel<State, Effect> |
existing ViewModels |
| Package root, test naming, fakes vs mocks | an existing module's commonTest |
Then choose the shape:
| What you find | Shape |
|---|---|
The framework module applies Compose and iOS shows ComposeUIViewController |
A — shared Compose UI. Screens live in commonMain of the feature module. |
iOS UI is SwiftUI; the framework module has no Compose; features come as :logic/:ui (or api/impl, domain/presentation) pairs |
B — logic/ui split. :logic is KMP without Compose; :ui is Compose for Android. |
One monolithic :shared with a package per feature and no feature modules |
Ask. Default to a new package inside :shared. Do not modularize as a side effect. |
| Greenfield | A if iOS will render Compose Multiplatform; B if iOS will be native SwiftUI. |
Write down the choice. Every later step follows it.
Workflow
1. Name the module
| Shape A | Shape B | |
|---|---|---|
| Gradle path | :feature:notes |
:feature:notes:logic, :feature:notes:ui |
| Directory | feature/notes/ |
feature/notes/logic/, feature/notes/ui/ |
| Package | com.example.app.feature.notes |
com.example.app.feature.notes in both |
Android namespace |
com.example.app.feature.notes |
…feature.notes.logic, …feature.notes.ui |
Every module needs a unique namespace. Duplicates collide in generated R classes, and AGP 9 fails
the build on them by default. Done when grep -rn 'namespace = "com.example.app.feature.notes' .
finds nothing yet.
2. Register it in settings.gradle.kts
include(":feature:notes") // Shape A
include(":feature:notes:logic", ":feature:notes:ui") // Shape B
Gradle maps :feature:notes:logic to feature/notes/logic/ by default. If the build enables
TYPESAFE_PROJECT_ACCESSORS, other modules may write projects.feature.notes.logic; follow what
siblings use. Done when ./gradlew projects lists the new path.
3. Write the build file
If a convention plugin exists, the build file is a few lines: apply it, set namespace, add
dependencies. Otherwise copy the matching template from
references/gradle-templates.md. Rules that apply either way:
Android target (default). Apply
com.android.kotlin.multiplatform.libraryand put all Android config inkotlin { android { } }; there is no top-levelandroid { }block. On AGP 8.12+ the block isandroid { };androidLibrary { }is deprecated since AGP 9.1.0-alpha09, so only an AGP 8.10–8.11 build uses it. Kotlin 2.5 deprecatesandroidTarget()withcom.android.libraryon every AGP version, and AGP 9 accepts it only behind legacy opt-outs. Use it in a new module only when every sibling still does (legacy, follow the project); migrate separately.Opt-ins on the Android-KMP plugin. Everything below is off by default, so a module that needs it must ask for it inside
kotlin { android { } }:Need DSL Without it Host (unit) tests in src/androidHostTestwithHostTest {}androidHostTestnever compiles or runsDevice tests in src/androidDeviceTestwithDeviceTest { instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }no instrumented test task Android res/(insrc/androidMain/res) or Compose resourcesandroidResources { enable = true }Compose resources fail at runtime Java sources in androidMainwithJava()Java sources are not compiled Consumer R8 keep rules optimization { consumerKeepRules.apply { publish = true; file("consumer-rules.pro") } }rules are not shipped to the app No variants. The plugin has one variant: no build types, flavours or
BuildConfig. Pass configuration in from the app through DI. Compose tooling for previews goes on"androidRuntimeClasspath"(...)in the top-leveldependencies { }, notdebugImplementation(AGP 9.0.0-beta01+).iOS targets. Declare at least every target of each module that will depend on this one. A consumer with
iosX64()cannot depend on a module without it.iosX64is only for Intel-Mac simulators and is a Tier 3 target in current Kotlin (see the target support page).Frameworks. Feature modules declare no
binaries.framework. Only the module iOS links (the umbrella in Shape B,:sharedor:composeAppin Shape A) produces one.Shape B
:logichas no Compose. No Compose plugins, no Compose artifacts, not even through anapidependency. Compose there drags the Compose runtime into the iOS framework and puts types in its API that SwiftUI cannot use.Versions. Only
libs.*accessors. New entries go throughkmp-gradle-version-catalog.
Done when ./gradlew :feature:notes:help (or :feature:notes:logic:help) configures without error.
4. Lay out the sources
Shape A feature/notes/src/
commonMain/kotlin/com/example/app/feature/notes/
data/ domain/ ui/ (NotesListScreen.kt, NotesListViewModel.kt) navigation/ di/NotesModule.kt
commonTest/kotlin/com/example/app/feature/notes/
androidMain/ iosMain/ # only when an actual or platform binding is needed
Shape B feature/notes/logic/src/commonMain/kotlin/com/example/app/feature/notes/
data/ domain/ presentation/ (NotesListViewModel.kt, NotesListState.kt) di/NotesModule.kt
feature/notes/logic/src/commonTest/...
feature/notes/ui/src/commonMain/kotlin/com/example/app/feature/notes/
ui/ (NotesListScreen.kt, NotesListTestTags.kt) navigation/NotesNavigation.kt di/NotesUiModule.kt
Mirror the package layout of the nearest existing feature. Add data/ and domain/ only when the
feature has them; a screen that only navigates needs neither.
5. Add the DI module
Each feature exposes one DI module and registers nothing globally. Koin example:
// feature/notes/logic/src/commonMain/kotlin/com/example/app/feature/notes/di/NotesModule.kt
val notesModule = module {
singleOf(::NotesRepositoryImpl) bind NotesRepository::class
factoryOf(::ObserveNotesUseCase)
viewModelOf(::NotesListViewModel) // org.koin.core.module.dsl, from koin-core-viewmodel
}
Register it where the app builds its graph — startKoin { modules(appModule, notesModule) } or an
aggregating module { includes(notesModule) }. In Shape B the iOS app starts the same graph through
the umbrella; see references/ios-umbrella-framework.md.
Done when the app module lists notesModule. A missing registration is a runtime
NoDefinitionFoundException, not a compile error, so check it now.
6. Register navigation
The feature owns its destinations; the host adds one line per feature. Cross-feature navigation goes through callbacks that the app wires, never through another feature's route type.
// Shape A: feature module. Shape B: the :ui module.
@Serializable data object NotesListRoute
@Serializable data class NoteDetailRoute(val noteId: String)
fun NavGraphBuilder.notesGraph(navController: NavController, onOpenProfile: () -> Unit) {
composable<NotesListRoute> {
NotesListScreen(
onNoteClick = { noteId -> navController.navigate(NoteDetailRoute(noteId)) },
onProfileClick = onOpenProfile,
)
}
composable<NoteDetailRoute> { NoteDetailScreen(onBack = { navController.popBackStack() }) }
}
// App NavHost — the only place that knows two features exist.
NavHost(navController = navController, startDestination = NotesListRoute) {
notesGraph(navController = navController, onOpenProfile = { navController.navigate(ProfileRoute) })
profileGraph(navController = navController)
}
If the project uses Navigation 3, Decompose or Voyager, follow its existing registration pattern instead; the callback rule still holds. Done when the host compiles with the new line.
7. Wire it into its consumers
Shape A. The module that owns the NavHost and the DI graph adds
implementation(project(":feature:notes")). If iOS only reaches the feature through Compose, no
export is needed; export only what Swift calls directly.
Shape B. Two consumers, and the umbrella needs both lines:
// shared/build.gradle.kts — the umbrella that produces the iOS framework
kotlin {
listOf(iosArm64(), iosSimulatorArm64()).forEach { target ->
target.binaries.framework {
baseName = "Shared"
export(project(":feature:notes:logic")) // puts its public API in the Swift header
}
}
sourceSets {
commonMain.dependencies {
api(project(":feature:notes:logic")) // export() is legal only for api dependencies
}
}
}
// androidApp/build.gradle.kts
dependencies { implementation(project(":feature:notes:ui")) }
export without api fails the build. api without export is the silent failure: Android builds
and runs, Gradle is green, and Swift later reports Cannot find 'NotesListViewModel' in scope.
export is not transitive: if NotesListViewModel exposes Note from :core:model, export
:core:model too. Done when the header check in Verify finds the class.
If the project uses Swift export (export { swift { } }, Beta in Kotlin 2.5.0, iOS 18+), there
is no Objective-C header and no export(...) call. Each exported Gradle module becomes its own Swift
module, and api dependencies of the linked module are exported automatically. A new :logic
module is therefore a new Swift module: give it a stable name with export { swift { moduleName = "NotesLogic" } } in its own build file, and expect Swift call sites to import it by that name. Never
link a Swift export build and an Objective-C framework into the same Xcode target (two Kotlin
runtimes). Setup and mappings: kmp-swiftui-interop and
Swift export.
8. Add the first test
Add one ViewModel or use-case test in commonTest using the project's test style (default
kotlin.test + a hand-written fake + Turbine). A template is in
references/gradle-templates.md. Done when the test
runs on both an Android and an iOS test task.
Rule: no feature depends on another feature
A feature-to-feature dependency couples release cycles, invites cycles, and lets one feature reach into another's internals. Allowed edges:
| From | May depend on |
|---|---|
:feature:notes:ui |
:feature:notes:logic, :core:*, design system |
:feature:notes / :feature:notes:logic |
:core:* (model, network, database, common) |
:core:* |
other :core:* only — never a feature, never the app |
| App / umbrella | every feature and :core:* |
When two features must cooperate:
- Navigation — callbacks wired by the app (step 6).
- Shared data — move the type to a
:core:module once a second feature needs it, not before. - Shared behaviour — declare an interface in a
:core:module, implement it in the owning feature, and bind it in the app's DI graph. The consuming feature sees only the interface.
Check it from the root (expect no output; assumes modules under feature/<name>/):
for f in $(find feature -name build.gradle.kts -not -path '*/build/*'); do
own=$(echo "$f" | cut -d/ -f2)
grep -HnoE 'project\(":feature:[A-Za-z0-9_-]+|projects\.feature\.[A-Za-z0-9_]+' "$f" \
| grep -vE "(:feature:|projects\.feature\.)$own\$" || true
done
Quick reference
| Task | Shape A | Shape B |
|---|---|---|
| Plugins | KMP, Android KMP library, Compose MP, Compose compiler | :logic: KMP, Android KMP library. :ui: plus Compose |
| Android block | kotlin { android { namespace; compileSdk; minSdk } } + opt-ins it needs |
same in each module |
| Preview tooling | "androidRuntimeClasspath"(...) |
same, in :ui |
| iOS targets | same as consumers | :logic only; :ui is Android-only |
binaries.framework |
no (unless iOS links this module) | no; the umbrella has it |
| Umbrella edits | none, unless Swift calls it | api(project(...)) and export(project(...)) (Swift export: api only) |
| Android app edit | via the module that hosts the NavHost | implementation(project(":feature:<name>:ui")) |
| DI | val <name>Module registered in the app graph |
same, in :logic; UI-only bindings in :ui |
| Navigation | NavGraphBuilder.<name>Graph(...) |
same, in :ui |
Verify
Run what applies and report each line as PASS, FAIL or NOT RUN (with the reason, e.g. "no
macOS host"). Never report a check as passed that you did not run. If a task name differs, list the
real ones with ./gradlew :feature:notes:logic:tasks --all | grep -iE 'compile|test|link'.
./gradlew :feature:notes:logic:compileCommonMainKotlinMetadata # shared code alone
./gradlew :feature:notes:logic:compileAndroidMain # AGP KMP plugin (legacy: compileDebugKotlinAndroid)
./gradlew :feature:notes:logic:compileKotlinIosSimulatorArm64 # macOS only
./gradlew :feature:notes:logic:testAndroidHostTest # legacy plugin: testDebugUnitTest
./gradlew :feature:notes:logic:iosSimulatorArm64Test # macOS only
./gradlew :shared:linkDebugFrameworkIosSimulatorArm64 # Shape B umbrella, macOS only
grep -c 'swift_name("NotesListViewModel")' \
shared/build/bin/iosSimulatorArm64/debugFramework/Shared.framework/Headers/Shared.h # expect >= 1
# (Objective-C framework only; with Swift export, build the Xcode scheme and call the type from Swift)
./gradlew :androidApp:assembleDebug # or :composeApp:assembleDebug
Then run the feature-boundary check above, and build the iOS app in Xcode if a scheme exists.
Common mistakes
| Mistake | Why it bites | Fix |
|---|---|---|
api(...) in the umbrella but no export(...) |
Android is fine; Swift cannot see the module, found late and far from the cause | Add export(project(...)) to every framework binary; run the header grep |
| Exporting a module whose public types come from an un-exported module | export is not transitive; those types appear in Swift with module-prefixed names or not at all |
Export the type's own module too; avoid transitiveExport = true (size and link time) |
Compose dependency in a :logic module |
The SwiftUI-facing framework drags in Compose, or exposes Compose types Swift cannot use | Keep Compose in :ui; put state the UI needs on the state class |
| Feature imports another feature | Coupled builds, cycles, internal APIs leak | Callbacks, :core: types, or an interface bound by the app |
| New module declares different iOS targets than its consumer | No matching variant / could not resolve for iosX64 or similar |
Copy the target list from the consuming module |
binaries.framework in every feature module |
Extra link tasks and framework files nobody ships | Frameworks only where iOS links |
| Two Kotlin frameworks linked into one iOS app | Each carries its own Kotlin runtime; shared types are not the same type in Swift | One umbrella framework per app |
New module on androidTarget() + com.android.library when siblings use the Android-KMP plugin |
Deprecated in Kotlin 2.5 on every AGP version; AGP 9 needs legacy opt-outs that AGP 10 removes | com.android.kotlin.multiplatform.library + kotlin { android { } } |
androidLibrary { } copied from an older guide |
Deprecated since AGP 9.1.0-alpha09; scheduled for removal | Rename the block to android { } (AGP 8.12+) |
Forgot withHostTest {} with the AGP KMP plugin |
androidHostTest sources silently never compile or run |
Enable host tests in kotlin { android { } } |
Compose resources without androidResources { enable = true } |
Builds, then fails at runtime on Android | Enable Android resources in that module |
Reading BuildConfig in a library module |
The AGP KMP library plugin generates none | Inject config from the app module |
debugImplementation(ui-tooling) in a KMP module |
The plugin has no build types, so there is no debug configuration | "androidRuntimeClasspath"(libs.compose.ui.tooling) |
consumer-rules.pro present but never published |
The new plugin ships consumer keep rules only on request; the app's R8 strips reflected classes | optimization { consumerKeepRules.apply { publish = true; file(...) } } |
| Swift export and an Objective-C framework linked into one Xcode target | Two Kotlin runtimes in one app | One interop mode per Xcode target |
| DI module never registered | Runtime NoDefinitionFoundException on first screen open |
Add it to the app graph in step 5 |
Related skills
kmp-expect-actual— platform code inside the new module.kmp-gradle-version-catalog— catalog entries, convention plugins, composite builds.kmp-module-health— audit the module once it exists.compose-multiplatform-navigation,kmp-swiftui-interop,kmp-xcframework-distributionin this repo, for navigation depth, the Swift side of the umbrella, and shipping it as an XCFramework.android-to-kmp-migration— when the project is not KMP yet.kotlin-tooling-agp9-migration(JetBrains) — move existingandroidTarget()modules to the Android-KMP plugin; the same repo has Kotlin/Native build-performance skills.- Kotlin Multiplatform docs, Android KMP library plugin and Swift export.
Files (kmp-mobile-skills)
-
references
-
gradle-templates.md 17 KB
Loading… -
ios-umbrella-framework.md 8 KB
Loading…
-
-
SKILL.md 21.8 KB
--- name: kmp-feature-module description: Use when adding a new feature or library module to a Kotlin Multiplatform project - "create a module", "modularize this feature", a :feature module, settings.gradle.kts include lines, a logic/ui module split so SwiftUI can consume shared logic, an umbrella shared iOS framework, export() to Swift, com.android.kotlin.multiplatform.library and its android target block (withHostTest, androidResources), or a new module that builds on Android while Swift reports "Cannot find 'X' in scope". Not for moving existing modules to AGP 9. license: Apache-2.0 compatibility: Kotlin Multiplatform Gradle project. Default templates target AGP 9.x with the Android-KMP library plugin and Kotlin 2.5; older layouts (AGP 8.10+, androidTarget) are followed when the project uses them. iOS framework checks need macOS with Xcode. Checked December 2026. metadata: category: build platforms: android, ios --- # KMP feature module A feature module is a Gradle project with one owner, a narrow public API, and exactly two ways in: the app's DI graph and the app's navigation host. Pick the module shape the project already uses. The most expensive mistake is a module that builds and runs on Android while the iOS framework silently does not contain it. ## When to use - Creating `:feature:<name>` or a reusable library module in a KMP build. - Splitting a feature out of a monolithic `:shared` or `:composeApp`. - Adding a `:logic` + `:ui` pair for an app whose iOS UI is SwiftUI. - A new module compiles for Android but its types are missing in Swift. ## When not to use - Adding a platform API inside an existing module: use `kmp-expect-actual`. - Adding or upgrading a dependency or Gradle plugin: use `kmp-gradle-version-catalog`. - Auditing an existing module: use `kmp-module-health`. - Migrating existing modules to AGP 9 (`androidTarget()` to the Android-KMP library plugin, splitting the app entry point out): use JetBrains' [`kotlin-tooling-agp9-migration`](https://github.com/Kotlin/kotlin-agent-skills/tree/main/skills/kotlin-tooling-agp9-migration). - Turning an Android-only app into KMP: use `android-to-kmp-migration`. ## Step 0 — Read the project first Do not write a build file from memory. Read the build, record the answers, then copy the closest existing sibling module. ```bash cat settings.gradle.kts # include(...), includeBuild, catalog path find . -name build.gradle.kts -not -path '*/build/*' | sort # existing modules grep -nE '^(agp|kotlin|composeMultiplatform|android-?[Gg]radle[A-Za-z-]*) *=' gradle/libs.versions.toml grep -rnE 'androidTarget\(|androidLibrary *\{|kotlin\.multiplatform\.library|com\.android\.library' \ --include=build.gradle.kts . # which Android-target style siblings use grep -rln "binaries.framework" --include=build.gradle.kts . # who produces the iOS framework grep -rn "export(" --include=build.gradle.kts . # what Swift can see today (Obj-C framework) grep -rnE "swift *\{|swiftExport" --include=build.gradle.kts . # Swift export instead of a framework? grep -rlE "composeMultiplatform|org.jetbrains.compose" --include=build.gradle.kts . ls build-logic buildSrc 2>/dev/null # convention plugins grep -rl "ComposeUIViewController" --include='*.kt' . | head -3 # iOS renders Compose? ``` | Record | Where it shows up | |---|---| | Module naming (`:feature:notes`, `:notes`, `:features:notes-impl`) | `include(...)` lines | | Kotlin and AGP versions | `gradle/libs.versions.toml` | | Android target style: `com.android.kotlin.multiplatform.library` + `kotlin { android { } }` (current), `kotlin { androidLibrary { } }` (deprecated since AGP 9.1.0-alpha09), or `com.android.library` + `androidTarget()` (legacy) | sibling build files | | Swift interop: Objective-C framework (`binaries.framework` + `export(...)`) or Swift export (`export { swift { } }`) | the module iOS links | | iOS targets: `iosArm64`, `iosSimulatorArm64`, maybe `iosX64` | sibling build files — a new module declares the same set as its consumers | | Convention plugins (`id("com.example.kmp.library")`) | `build-logic/`, `buildSrc/` | | DI: Koin, kotlin-inject, Metro, manual | `startKoin`, `@Component` | | Navigation: Navigation Compose, Navigation 3, Decompose, Voyager | the `NavHost` or root component | | Presentation: `ViewModel` + `StateFlow`, or an MVI base such as `MviViewModel<State, Effect>` | existing ViewModels | | Package root, test naming, fakes vs mocks | an existing module's `commonTest` | Then choose the shape: | What you find | Shape | |---|---| | The framework module applies Compose and iOS shows `ComposeUIViewController` | **A — shared Compose UI.** Screens live in `commonMain` of the feature module. | | iOS UI is SwiftUI; the framework module has no Compose; features come as `:logic`/`:ui` (or `api`/`impl`, `domain`/`presentation`) pairs | **B — logic/ui split.** `:logic` is KMP without Compose; `:ui` is Compose for Android. | | One monolithic `:shared` with a package per feature and no feature modules | **Ask.** Default to a new package inside `:shared`. Do not modularize as a side effect. | | Greenfield | A if iOS will render Compose Multiplatform; B if iOS will be native SwiftUI. | Write down the choice. Every later step follows it. ## Workflow ### 1. Name the module | | Shape A | Shape B | |---|---|---| | Gradle path | `:feature:notes` | `:feature:notes:logic`, `:feature:notes:ui` | | Directory | `feature/notes/` | `feature/notes/logic/`, `feature/notes/ui/` | | Package | `com.example.app.feature.notes` | `com.example.app.feature.notes` in both | | Android `namespace` | `com.example.app.feature.notes` | `…feature.notes.logic`, `…feature.notes.ui` | Every module needs a unique `namespace`. Duplicates collide in generated `R` classes, and AGP 9 fails the build on them by default. Done when `grep -rn 'namespace = "com.example.app.feature.notes' .` finds nothing yet. ### 2. Register it in `settings.gradle.kts` ```kotlin include(":feature:notes") // Shape A include(":feature:notes:logic", ":feature:notes:ui") // Shape B ``` Gradle maps `:feature:notes:logic` to `feature/notes/logic/` by default. If the build enables `TYPESAFE_PROJECT_ACCESSORS`, other modules may write `projects.feature.notes.logic`; follow what siblings use. Done when `./gradlew projects` lists the new path. ### 3. Write the build file If a convention plugin exists, the build file is a few lines: apply it, set `namespace`, add dependencies. Otherwise copy the matching template from [references/gradle-templates.md](references/gradle-templates.md). Rules that apply either way: - **Android target (default).** Apply `com.android.kotlin.multiplatform.library` and put all Android config in `kotlin { android { } }`; there is no top-level `android { }` block. On AGP 8.12+ the block is `android { }`; `androidLibrary { }` is deprecated since AGP 9.1.0-alpha09, so only an AGP 8.10–8.11 build uses it. Kotlin 2.5 deprecates `androidTarget()` with `com.android.library` on every AGP version, and AGP 9 accepts it only behind legacy opt-outs. Use it in a new module **only** when every sibling still does (legacy, follow the project); migrate separately. - **Opt-ins on the Android-KMP plugin.** Everything below is off by default, so a module that needs it must ask for it inside `kotlin { android { } }`: | Need | DSL | Without it | |---|---|---| | Host (unit) tests in `src/androidHostTest` | `withHostTest {}` | `androidHostTest` never compiles or runs | | Device tests in `src/androidDeviceTest` | `withDeviceTest { instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }` | no instrumented test task | | Android `res/` (in `src/androidMain/res`) or Compose resources | `androidResources { enable = true }` | Compose resources fail at runtime | | Java sources in `androidMain` | `withJava()` | Java sources are not compiled | | Consumer R8 keep rules | `optimization { consumerKeepRules.apply { publish = true; file("consumer-rules.pro") } }` | rules are not shipped to the app | - **No variants.** The plugin has one variant: no build types, flavours or `BuildConfig`. Pass configuration in from the app through DI. Compose tooling for previews goes on `"androidRuntimeClasspath"(...)` in the top-level `dependencies { }`, not `debugImplementation` (AGP 9.0.0-beta01+). - **iOS targets.** Declare at least every target of each module that will depend on this one. A consumer with `iosX64()` cannot depend on a module without it. `iosX64` is only for Intel-Mac simulators and is a Tier 3 target in current Kotlin (see the [target support page](https://kotlinlang.org/docs/native-target-support.html)). - **Frameworks.** Feature modules declare no `binaries.framework`. Only the module iOS links (the umbrella in Shape B, `:shared` or `:composeApp` in Shape A) produces one. - **Shape B `:logic` has no Compose.** No Compose plugins, no Compose artifacts, not even through an `api` dependency. Compose there drags the Compose runtime into the iOS framework and puts types in its API that SwiftUI cannot use. - **Versions.** Only `libs.*` accessors. New entries go through `kmp-gradle-version-catalog`. Done when `./gradlew :feature:notes:help` (or `:feature:notes:logic:help`) configures without error. ### 4. Lay out the sources ```text Shape A feature/notes/src/ commonMain/kotlin/com/example/app/feature/notes/ data/ domain/ ui/ (NotesListScreen.kt, NotesListViewModel.kt) navigation/ di/NotesModule.kt commonTest/kotlin/com/example/app/feature/notes/ androidMain/ iosMain/ # only when an actual or platform binding is needed Shape B feature/notes/logic/src/commonMain/kotlin/com/example/app/feature/notes/ data/ domain/ presentation/ (NotesListViewModel.kt, NotesListState.kt) di/NotesModule.kt feature/notes/logic/src/commonTest/... feature/notes/ui/src/commonMain/kotlin/com/example/app/feature/notes/ ui/ (NotesListScreen.kt, NotesListTestTags.kt) navigation/NotesNavigation.kt di/NotesUiModule.kt ``` Mirror the package layout of the nearest existing feature. Add `data/` and `domain/` only when the feature has them; a screen that only navigates needs neither. ### 5. Add the DI module Each feature exposes one DI module and registers nothing globally. Koin example: ```kotlin // feature/notes/logic/src/commonMain/kotlin/com/example/app/feature/notes/di/NotesModule.kt val notesModule = module { singleOf(::NotesRepositoryImpl) bind NotesRepository::class factoryOf(::ObserveNotesUseCase) viewModelOf(::NotesListViewModel) // org.koin.core.module.dsl, from koin-core-viewmodel } ``` Register it where the app builds its graph — `startKoin { modules(appModule, notesModule) }` or an aggregating `module { includes(notesModule) }`. In Shape B the iOS app starts the same graph through the umbrella; see [references/ios-umbrella-framework.md](references/ios-umbrella-framework.md). Done when the app module lists `notesModule`. A missing registration is a runtime `NoDefinitionFoundException`, not a compile error, so check it now. ### 6. Register navigation The feature owns its destinations; the host adds one line per feature. Cross-feature navigation goes through callbacks that the app wires, never through another feature's route type. ```kotlin // Shape A: feature module. Shape B: the :ui module. @Serializable data object NotesListRoute @Serializable data class NoteDetailRoute(val noteId: String) fun NavGraphBuilder.notesGraph(navController: NavController, onOpenProfile: () -> Unit) { composable<NotesListRoute> { NotesListScreen( onNoteClick = { noteId -> navController.navigate(NoteDetailRoute(noteId)) }, onProfileClick = onOpenProfile, ) } composable<NoteDetailRoute> { NoteDetailScreen(onBack = { navController.popBackStack() }) } } // App NavHost — the only place that knows two features exist. NavHost(navController = navController, startDestination = NotesListRoute) { notesGraph(navController = navController, onOpenProfile = { navController.navigate(ProfileRoute) }) profileGraph(navController = navController) } ``` If the project uses Navigation 3, Decompose or Voyager, follow its existing registration pattern instead; the callback rule still holds. Done when the host compiles with the new line. ### 7. Wire it into its consumers **Shape A.** The module that owns the `NavHost` and the DI graph adds `implementation(project(":feature:notes"))`. If iOS only reaches the feature through Compose, no `export` is needed; export only what Swift calls directly. **Shape B.** Two consumers, and the umbrella needs **both** lines: ```kotlin // shared/build.gradle.kts — the umbrella that produces the iOS framework kotlin { listOf(iosArm64(), iosSimulatorArm64()).forEach { target -> target.binaries.framework { baseName = "Shared" export(project(":feature:notes:logic")) // puts its public API in the Swift header } } sourceSets { commonMain.dependencies { api(project(":feature:notes:logic")) // export() is legal only for api dependencies } } } ``` ```kotlin // androidApp/build.gradle.kts dependencies { implementation(project(":feature:notes:ui")) } ``` `export` without `api` fails the build. `api` without `export` is the silent failure: Android builds and runs, Gradle is green, and Swift later reports `Cannot find 'NotesListViewModel' in scope`. `export` is not transitive: if `NotesListViewModel` exposes `Note` from `:core:model`, export `:core:model` too. Done when the header check in **Verify** finds the class. **If the project uses Swift export** (`export { swift { } }`, Beta in Kotlin 2.5.0, iOS 18+), there is no Objective-C header and no `export(...)` call. Each exported Gradle module becomes its own Swift module, and `api` dependencies of the linked module are exported automatically. A new `:logic` module is therefore a new Swift module: give it a stable name with `export { swift { moduleName = "NotesLogic" } }` in its own build file, and expect Swift call sites to import it by that name. Never link a Swift export build and an Objective-C framework into the same Xcode target (two Kotlin runtimes). Setup and mappings: `kmp-swiftui-interop` and [Swift export](https://kotlinlang.org/docs/native-swift-export.html). ### 8. Add the first test Add one ViewModel or use-case test in `commonTest` using the project's test style (default `kotlin.test` + a hand-written fake + Turbine). A template is in [references/gradle-templates.md](references/gradle-templates.md#8-first-test). Done when the test runs on both an Android and an iOS test task. ## Rule: no feature depends on another feature A feature-to-feature dependency couples release cycles, invites cycles, and lets one feature reach into another's internals. Allowed edges: | From | May depend on | |---|---| | `:feature:notes:ui` | `:feature:notes:logic`, `:core:*`, design system | | `:feature:notes` / `:feature:notes:logic` | `:core:*` (model, network, database, common) | | `:core:*` | other `:core:*` only — never a feature, never the app | | App / umbrella | every feature and `:core:*` | When two features must cooperate: 1. **Navigation** — callbacks wired by the app (step 6). 2. **Shared data** — move the type to a `:core:` module once a *second* feature needs it, not before. 3. **Shared behaviour** — declare an interface in a `:core:` module, implement it in the owning feature, and bind it in the app's DI graph. The consuming feature sees only the interface. Check it from the root (expect no output; assumes modules under `feature/<name>/`): ```bash for f in $(find feature -name build.gradle.kts -not -path '*/build/*'); do own=$(echo "$f" | cut -d/ -f2) grep -HnoE 'project\(":feature:[A-Za-z0-9_-]+|projects\.feature\.[A-Za-z0-9_]+' "$f" \ | grep -vE "(:feature:|projects\.feature\.)$own\$" || true done ``` ## Quick reference | Task | Shape A | Shape B | |---|---|---| | Plugins | KMP, Android KMP library, Compose MP, Compose compiler | `:logic`: KMP, Android KMP library. `:ui`: plus Compose | | Android block | `kotlin { android { namespace; compileSdk; minSdk } }` + opt-ins it needs | same in each module | | Preview tooling | `"androidRuntimeClasspath"(...)` | same, in `:ui` | | iOS targets | same as consumers | `:logic` only; `:ui` is Android-only | | `binaries.framework` | no (unless iOS links this module) | no; the umbrella has it | | Umbrella edits | none, unless Swift calls it | `api(project(...))` **and** `export(project(...))` (Swift export: `api` only) | | Android app edit | via the module that hosts the NavHost | `implementation(project(":feature:<name>:ui"))` | | DI | `val <name>Module` registered in the app graph | same, in `:logic`; UI-only bindings in `:ui` | | Navigation | `NavGraphBuilder.<name>Graph(...)` | same, in `:ui` | ## Verify Run what applies and report each line as `PASS`, `FAIL` or `NOT RUN` (with the reason, e.g. "no macOS host"). Never report a check as passed that you did not run. If a task name differs, list the real ones with `./gradlew :feature:notes:logic:tasks --all | grep -iE 'compile|test|link'`. ```bash ./gradlew :feature:notes:logic:compileCommonMainKotlinMetadata # shared code alone ./gradlew :feature:notes:logic:compileAndroidMain # AGP KMP plugin (legacy: compileDebugKotlinAndroid) ./gradlew :feature:notes:logic:compileKotlinIosSimulatorArm64 # macOS only ./gradlew :feature:notes:logic:testAndroidHostTest # legacy plugin: testDebugUnitTest ./gradlew :feature:notes:logic:iosSimulatorArm64Test # macOS only ./gradlew :shared:linkDebugFrameworkIosSimulatorArm64 # Shape B umbrella, macOS only grep -c 'swift_name("NotesListViewModel")' \ shared/build/bin/iosSimulatorArm64/debugFramework/Shared.framework/Headers/Shared.h # expect >= 1 # (Objective-C framework only; with Swift export, build the Xcode scheme and call the type from Swift) ./gradlew :androidApp:assembleDebug # or :composeApp:assembleDebug ``` Then run the feature-boundary check above, and build the iOS app in Xcode if a scheme exists. ## Common mistakes | Mistake | Why it bites | Fix | |---|---|---| | `api(...)` in the umbrella but no `export(...)` | Android is fine; Swift cannot see the module, found late and far from the cause | Add `export(project(...))` to every framework binary; run the header grep | | Exporting a module whose public types come from an un-exported module | `export` is not transitive; those types appear in Swift with module-prefixed names or not at all | Export the type's own module too; avoid `transitiveExport = true` (size and link time) | | Compose dependency in a `:logic` module | The SwiftUI-facing framework drags in Compose, or exposes Compose types Swift cannot use | Keep Compose in `:ui`; put state the UI needs on the state class | | Feature imports another feature | Coupled builds, cycles, internal APIs leak | Callbacks, `:core:` types, or an interface bound by the app | | New module declares different iOS targets than its consumer | `No matching variant` / could not resolve for `iosX64` or similar | Copy the target list from the consuming module | | `binaries.framework` in every feature module | Extra link tasks and framework files nobody ships | Frameworks only where iOS links | | Two Kotlin frameworks linked into one iOS app | Each carries its own Kotlin runtime; shared types are not the same type in Swift | One umbrella framework per app | | New module on `androidTarget()` + `com.android.library` when siblings use the Android-KMP plugin | Deprecated in Kotlin 2.5 on every AGP version; AGP 9 needs legacy opt-outs that AGP 10 removes | `com.android.kotlin.multiplatform.library` + `kotlin { android { } }` | | `androidLibrary { }` copied from an older guide | Deprecated since AGP 9.1.0-alpha09; scheduled for removal | Rename the block to `android { }` (AGP 8.12+) | | Forgot `withHostTest {}` with the AGP KMP plugin | `androidHostTest` sources silently never compile or run | Enable host tests in `kotlin { android { } }` | | Compose resources without `androidResources { enable = true }` | Builds, then fails at runtime on Android | Enable Android resources in that module | | Reading `BuildConfig` in a library module | The AGP KMP library plugin generates none | Inject config from the app module | | `debugImplementation(ui-tooling)` in a KMP module | The plugin has no build types, so there is no debug configuration | `"androidRuntimeClasspath"(libs.compose.ui.tooling)` | | `consumer-rules.pro` present but never published | The new plugin ships consumer keep rules only on request; the app's R8 strips reflected classes | `optimization { consumerKeepRules.apply { publish = true; file(...) } }` | | Swift export and an Objective-C framework linked into one Xcode target | Two Kotlin runtimes in one app | One interop mode per Xcode target | | DI module never registered | Runtime `NoDefinitionFoundException` on first screen open | Add it to the app graph in step 5 | ## Related skills - `kmp-expect-actual` — platform code inside the new module. - `kmp-gradle-version-catalog` — catalog entries, convention plugins, composite builds. - `kmp-module-health` — audit the module once it exists. - `compose-multiplatform-navigation`, `kmp-swiftui-interop`, `kmp-xcframework-distribution` in this repo, for navigation depth, the Swift side of the umbrella, and shipping it as an XCFramework. - `android-to-kmp-migration` — when the project is not KMP yet. - [`kotlin-tooling-agp9-migration`](https://github.com/Kotlin/kotlin-agent-skills/tree/main/skills/kotlin-tooling-agp9-migration) (JetBrains) — move existing `androidTarget()` modules to the Android-KMP plugin; the same repo has Kotlin/Native build-performance skills. - [Kotlin Multiplatform docs](https://kotlinlang.org/docs/multiplatform/), [Android KMP library plugin](https://developer.android.com/kotlin/multiplatform/plugin) and [Swift export](https://kotlinlang.org/docs/native-swift-export.html).
Reviews (0)
No reviews yet.
Comments
Sign in to join the conversation.
No comments yet. Start the conversation.