3.5 KiB
Build Convention: Test Dependencies for KMP Modules
Summary
We've centralized test dependency configuration for Kotlin Multiplatform (KMP) modules by creating a new build convention plugin function. This eliminates code duplication across all feature and core modules.
Changes Made
1. New Convention Function (build-logic/convention/src/main/kotlin/org/meshtastic/buildlogic/KotlinAndroid.kt)
Added configureKmpTestDependencies() function that automatically configures test dependencies for all KMP modules:
internal fun Project.configureKmpTestDependencies() {
extensions.configure<KotlinMultiplatformExtension> {
sourceSets.apply {
val commonTest = findByName("commonTest") ?: return@apply
commonTest.dependencies {
implementation(kotlin("test"))
}
// Configure androidHostTest if it exists
val androidHostTest = findByName("androidHostTest")
androidHostTest?.dependencies {
implementation(kotlin("test"))
}
}
}
}
Benefits:
- Single source of truth for test framework dependencies
- Automatically applied to all KMP modules using
meshtastic.kmp.library - Reduces build.gradle.kts boilerplate across 7+ feature modules
2. Plugin Integration (build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt)
Updated KmpLibraryConventionPlugin to call the new function:
configureKotlinMultiplatform()
configureKmpTestDependencies() // NEW
configureAndroidMarketplaceFallback()
3. Removed Duplicate Dependencies
Removed manual implementation(kotlin("test")) declarations from:
feature/messaging/build.gradle.ktsfeature/firmware/build.gradle.ktsfeature/intro/build.gradle.ktsfeature/map/build.gradle.ktsfeature/node/build.gradle.ktsfeature/settings/build.gradle.ktsfeature/connections/build.gradle.kts
Each module now only declares project-specific test dependencies:
commonTest.dependencies {
implementation(projects.core.testing)
// kotlin("test") is now added by convention!
}
Impact
Before
- 7+ feature modules each manually adding
implementation(kotlin("test"))tocommonTest.dependencies - 7+ feature modules each manually adding
implementation(kotlin("test"))toandroidHostTestsource sets - High risk of inconsistency or missing dependencies in new modules
After
- Single configuration in
build-logic/applies to all KMP modules - Guaranteed consistency across all feature modules
- Future modules automatically benefit from this convention
- Build.gradle.kts files are cleaner and more focused on module-specific dependencies
Testing
Verified with:
./gradlew :feature:node:testAndroidHostTest :feature:settings:testAndroidHostTest
# BUILD SUCCESSFUL
The convention plugin automatically provides kotlin("test") to all commonTest and androidHostTest source sets in KMP modules.
Future Considerations
If additional test framework dependencies are needed across all KMP modules (e.g., new assertion libraries, mocking frameworks), they can be added to configureKmpTestDependencies() in one place, automatically benefiting all KMP modules.
This follows the established pattern in the project for convention plugins, as seen with:
configureComposeCompiler()- centralizes Compose compiler configurationconfigureKotlinAndroid()- centralizes Kotlin/Android base configuration- Koin, Detekt, Spotless conventions - all follow this pattern