You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KMP项目同步报错:默认Kotlin层级模板未应用

解决Kotlin Multiplatform项目默认层级模板未应用报错

在M2芯片的Mac上同步构建KMP项目时,:core模块出现报错:默认Kotlin层级模板未应用,原因是为iOS相关源码集(如iosArm64Main、iosArm64Test等)配置了显式的.dependsOn()关联。

报错详情

The Default Kotlin Hierarchy Template was not applied to 'project ':core'':
Explicit .dependsOn() edges were configured for the following source sets:
[iosArm64Main, iosArm64Test, iosMain, iosSimulatorArm64Main, iosSimulatorArm64Test, iosTest, iosX64Main, iosX64Test]

Consider removing dependsOn-calls or disabling the default template by adding
    'kotlin.mpp.applyDefaultHierarchyTemplate=false'
to your gradle.properties

项目build.gradle.kts原内容

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("kotlinx-serialization")
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "11"
            }
        }
    }
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        version = "1.0"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "core"
            isStatic = true
        }
       pod("UICKeyChainStore")
       pod("FirebaseCore")
       pod("FirebaseInstanceID")
    }

    val coroutines = "1.8.0"
    val ktor = "2.3.9"

    sourceSets {
        all {
            languageSettings.optIn("kotlin.RequiresOptIn")
            languageSettings.optIn("kotlin.experimental.ExperimentalObjCName")
        }

        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")

                // Http
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor")
                implementation("io.ktor:ktor-client-core:$ktor")
                implementation("io.ktor:ktor-client-serialization:$ktor")
                implementation("io.ktor:ktor-client-logging:$ktor")
                implementation("io.ktor:ktor-client-content-negotiation:$ktor")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                api("io.ktor:ktor-client-okhttp:$ktor")
                implementation("io.ktor:ktor-client-serialization-jvm:$ktor")
                implementation("io.ktor:ktor-client-logging-jvm:$ktor")
                implementation("com.facebook.stetho:stetho-okhttp3:1.6.0")

                implementation("com.google.firebase:firebase-core:21.1.1")
                implementation("com.google.firebase:firebase-messaging:23.4.1")
            }
        }
        val androidUnitTest by getting
        val androidInstrumentedTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)

            dependencies {
                // Http
                //implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:1.0-M1-1.4.0-rc")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
                implementation("io.ktor:ktor-client-ios:$ktor")
                //implementation("io.ktor:ktor-client-serialization-native:1.3.2")
                //implementation("io.ktor:ktor-client-logging-native:1.3.2")
            }
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "com.****.****.core"
    compileSdk = 34
    defaultConfig {
        minSdk = 23
        testOptions.targetSdk = 34
    }
    java {
        toolchain.languageVersion.set(JavaLanguageVersion.of(11))
    }
    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs> {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
}

解决方法

方案一:移除手动依赖关联,使用默认层级模板

Kotlin 1.7及以上版本的KMP默认层级模板会自动建立iOS源码集的层级关系:iosMain自动关联所有iOS架构的Main源码集,iosTest自动关联所有iOS架构的Test源码集,无需手动创建和设置dependsOn。

修改后的sourceSets部分代码如下:

sourceSets {
    all {
        languageSettings.optIn("kotlin.RequiresOptIn")
        languageSettings.optIn("kotlin.experimental.ExperimentalObjCName")
    }

    val commonMain by getting {
        dependencies {
            implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")

            // Http
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
            implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor")
            implementation("io.ktor:ktor-client-core:$ktor")
            implementation("io.ktor:ktor-client-serialization:$ktor")
            implementation("io.ktor:ktor-client-logging:$ktor")
            implementation("io.ktor:ktor-client-content-negotiation:$ktor")
        }
    }
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test"))
        }
    }
    val androidMain by getting {
        dependencies {
            api("io.ktor:ktor-client-okhttp:$ktor")
            implementation("io.ktor:ktor-client-serialization-jvm:$ktor")
            implementation("io.ktor:ktor-client-logging-jvm:$ktor")
            implementation("com.facebook.stetho:stetho-okhttp3:1.6.0")

            implementation("com.google.firebase:firebase-core:21.1.1")
            implementation("com.google.firebase:firebase-messaging:23.4.1")
        }
    }
    val androidUnitTest by getting
    val androidInstrumentedTest by getting
    
    // 直接使用默认生成的iosMain,无需手动创建和设置dependsOn
    val iosMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-ios:$ktor")
        }
    }
    // 直接使用默认生成的iosTest
    val iosTest by getting
}

方案二:禁用默认层级模板(不推荐)

如果必须保留手动的dependsOn配置,在项目的gradle.properties文件中添加以下配置:

kotlin.mpp.applyDefaultHierarchyTemplate=false

内容的提问来源于stack exchange,提问作者Fabio Cirruto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:14:56