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

将现有Android应用集成到KMM时遇未解析引用问题求助

KMM迁移中调用Shared模块方法出现“未解析引用”问题

我正按照官方教程将现有应用迁移为Kotlin Multiplatform Mobile(KMM)应用,严格遵循教程步骤操作后,应用构建时没有报错,但调用shared模块中定义的方法时,出现未解析引用的问题。

注:我已在app模块的gradle中添加了implementation project(":shared1")依赖。

附报错截图:
未解析引用报错1
未解析引用报错2

我的app模块build.gradle文件:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    // hilt
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'
    // safeArgs
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.example.bestpracticesapplication"
        minSdk 31
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.2.0"
    }
}

dependencies {
    implementation project(":shared1")
    // google one-tap sign-in and sign-up
    implementation 'com.google.android.gms:play-services-auth:20.3.0'

    implementation 'androidx.activity:activity-ktx:1.6.0'
    implementation 'androidx.activity:activity:1.6.0'
    implementation 'androidx.activity:activity-compose:1.6.0'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation("io.coil-kt:coil:2.2.2")
    implementation("io.coil-kt:coil-compose:2.2.2")

    // Compose
    implementation 'androidx.compose.ui:ui:1.2.1'
    // Tooling support (Previews, etc.)
    implementation 'androidx.compose.ui:ui-tooling:1.2.1'
    // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
    implementation 'androidx.compose.foundation:foundation:1.2.1'
    // Material Design
    implementation 'androidx.compose.material:material:1.2.1'
    // Material design icons
    implementation 'androidx.compose.material:material-icons-core:1.2.1'
    implementation 'androidx.compose.material:material-icons-extended:1.2.1'
    // Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
    // Integration with observables
    implementation 'androidx.compose.runtime:runtime-livedata:1.2.1'
    implementation 'androidx.compose.runtime:runtime-rxjava2:1.2.1'

    def lifecycle_version = "2.5.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

    // hilt
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"
    implementation 'androidx.hilt:hilt-navigation-fragment:1.0.0'


    // Navigation Graph
    def nav_version = "2.5.2"
    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    // Jetpack Compose Integration
    implementation "androidx.navigation:navigation-compose:$nav_version"


    // retrofit
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    // GSON
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"


    // Room DB
    def room_version = "2.4.3"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    kapt "androidx.room:room-compiler:$room_version"


    // WorkManager
    implementation "androidx.work:work-runtime-ktx:2.7.1"
    implementation "androidx.startup:startup-runtime:1.1.1"
    implementation "androidx.hilt:hilt-work:1.0.0"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'

}

// Allow references to generated code
kapt {
    correctErrorTypes true
}

shared模块build.gradle文件:

plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

kotlin {
    android()
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared1"
        }
    }

    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest 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)
        }
        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.example.shared1"
    compileSdk = 32
    defaultConfig {
        minSdk = 31
        targetSdk = 32
    }
}

内容的提问来源于stack exchange,提问作者Ryan Chen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 16:55:30