Adding a Feature Module
Step-by-step guide for creating a new KMP feature module in the Meshtastic project.
Create the Module Directory
mkdir -p feature/my-feature/src/{commonMain,commonTest,androidMain,jvmMain,iosMain}/kotlin/org/meshtastic/feature/myfeature
Create build.gradle.kts
plugins {
alias(libs.plugins.meshtastic.kmp.feature)
}
kotlin {
@Suppress("UnstableApiUsage")
android {
namespace = "org.meshtastic.feature.myfeature"
withHostTest { isIncludeAndroidResources = true }
}
sourceSets {
commonMain.dependencies {
implementation(projects.core.common)
implementation(projects.core.navigation)
implementation(projects.core.resources)
implementation(projects.core.ui)
implementation(projects.core.di)
}
commonTest.dependencies {
implementation(libs.compose.multiplatform.ui.test)
}
jvmTest.dependencies {
implementation(compose.desktop.currentOs)
}
}
}
meshtastic.kmp.feature declares the jvm() target, and iosSimulatorArm64() on hosts that can run Kotlin/Native. It adds the Compose, Navigation 3, Lifecycle and metrox-viewmodel-compose dependencies, applies meshtastic.metro, and puts core:testing on commonTest. It also derives the Android namespace from the module path when none is set, which for a hyphenated path such as :feature:my-feature isn’t a valid package name, so set namespace yourself.
Register in settings.gradle.kts
Add your module to the main include() block:
include(
// ...existing modules...
":feature:my-feature",
)
Contribute bindings
There is no DI module class to write. Annotate each class where it lives and Metro’s AppScope aggregation picks it up:
@ViewModelKey
@ContributesIntoMap(AppScope::class, binding<ViewModel>())
class MyFeatureViewModel(private val nodeRepository: NodeRepository) : ViewModel()
@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class)
class MyFeatureRepositoryImpl(...) : MyFeatureRepository
A composable gets its dependencies through a ViewModel: inject them into the ViewModel’s constructor and take the ViewModel as a default parameter (viewModel: MyFeatureViewModel = metroViewModel()). It never reads the graph itself.
Make the graphs see the module
Each host’s @DependencyGraph (AndroidAppGraph, DesktopAppGraph, WebAppGraph) aggregates contributions from its classpath, so the module only has to be a dependency of the host:
androidApp/build.gradle.ktsanddesktopApp/build.gradle.kts(through:appShell, which every feature module belongs to)webApp/build.gradle.kts, only for a module that appliesmeshtastic.kmp.web: add it towasmJsMain.dependencies, and add its directory to thewebfilter in.github/workflows/pull-request.yml, whichscripts/check-changes-filter.pyenforces
Add Navigation Routes
In core/navigation/src/commonMain/kotlin/org/meshtastic/core/navigation/Routes.kt:
@Serializable
sealed interface MyFeatureRoute : Route {
@Serializable data object MyFeatureGraph : MyFeatureRoute, Graph
@Serializable data object MyFeatureHome : MyFeatureRoute
}
A new sealed route interface also has to be registered in MeshtasticNavSavedStateConfig (core/navigation/.../NavigationConfig.kt) with subclassesOfSealed<MyFeatureRoute>(), or saving the back stack fails to serialize its routes. Add an instance of each new route to allRouteInstances in NavigationConfigTest, which round-trips every route through saved state.
Create Navigation Entries
src/commonMain/kotlin/org/meshtastic/feature/myfeature/navigation/MyFeatureNavigation.kt:
package org.meshtastic.feature.myfeature.navigation
import androidx.navigation3.runtime.EntryProviderScope
import androidx.navigation3.runtime.NavBackStack
import androidx.navigation3.runtime.NavKey
import org.meshtastic.core.navigation.MyFeatureRoute
fun EntryProviderScope<NavKey>.myFeatureGraph(backStack: NavBackStack<NavKey>) {
entry<MyFeatureRoute.MyFeatureGraph> {
MyFeatureScreen(onNavigateUp = { backStack.removeLastOrNull() })
}
entry<MyFeatureRoute.MyFeatureHome> {
MyFeatureScreen(onNavigateUp = { backStack.removeLastOrNull() })
}
}
Both the graph sentinel (MyFeatureRoute.MyFeatureGraph) and the primary screen (MyFeatureRoute.MyFeatureHome) navigate to the same composable, so the feature is reachable via either a top-level push or a deep-link graph push — the same pattern feature:wifi-provision and feature:firmware use.
Then wire it up: add the module to appShell/build.gradle.kts and call myFeatureGraph(backStack) from sharedNavGraph in appShell’s MeshtasticMainScreen.kt, alongside the other features’ entries functions, so the Android, desktop and web hosts all show it. A module with no web target is called from appShell’s nonWebMain behind an expect, as mapGraph is. See Navigation Entry Registration.
Source Set Guidelines
| Source Set | Contains |
|---|---|
commonMain | Models, ViewModels, shared UI, DI module, navigation |
androidMain | Android-specific implementations (e.g. platform APIs) |
jvmMain | Desktop-specific implementations |
iosMain | iOS-specific implementations |
commonTest | Shared unit tests |
Testing Expectations
Every feature module should have:
- Unit tests in
commonTestfor business logic - UI tests using
compose-multiplatform-ui-testwhere appropriate - No test dependency on other feature modules
Checklist
- Module directory created
build.gradle.ktswith correct plugins and dependencies- Added to
settings.gradle.kts - Added to
ALL_MODULES_FULLinbuild-logic/convention/src/main/kotlin/RootConventionPlugin.kt;python3 scripts/check-module-list.pyfails when a non-exempt module is missing from it, when an entry is absent from thesettings.gradle.ktsincludes, or when an exempt module is re-added, and a non-exempt module missing from it is absent from Dokka and Kover aggregation andkmpSmokeCompile - Added to a test shard in
.github/workflows/reusable-check.yml(shard-featurefor a feature module): its:feature:my-feature:allTeststask intasksand itskoverXmlReportinkover;python3 scripts/check-test-shards.pyfails for a module with no test task in any shard, unless the module is listed in the script’sCOVERED_ELSEWHEREorNO_TESTS_YETsets - If the module is user-facing, a page under
docs/en/user/and aMODULE_TO_DOCSentry for it inscripts/check-doc-coverage.js; the check fails when a listed module’s page is missing, and a module with no entry needs no page - Classes annotated with
@SingleIn(AppScope::class) @ContributesBinding(AppScope::class)or@Inject, and ViewModels with@ViewModelKey @ContributesIntoMap(AppScope::class, binding<ViewModel>()); no per-module DI class - Module is a dependency of the hosts (
:appShellfor android and desktop,wasmJsMainfor web) - Routes added to
Routes.kt, the sealed interface registered inNavigationConfig.kt, and an instance of each route inNavigationConfigTest - Navigation entries registered
./gradlew kmpSmokeCompilepasses./gradlew :feature:my-feature:allTestspasses