簡體   English   中英

KMM + Compose:未解決的參考:可繪制

[英]KMM + Compose: Unresolved reference: drawable

我有一個 KMM 應用程序,其中 android 目標使用 Jetpack Compose。 嘗試使用可繪制資源時出現以下錯誤:

e: [...]/OnboardingScreen.kt: (33, 46): 未解決的參考:drawable

這是嘗試通過painterResource(id = R.drawable.ic_icon)訪問drawable 的結果。

我嘗試了以下方法來解決此問題:

  • 清理並構建項目
  • 使緩存無效並重新啟動
  • 修復執行./gradlew assembleDebug時的所有警告
  • 正確導入 static R class

以上都不能解決問題。

這是我的 android 模塊的 build.gradle.kts:

plugins {
    id("com.android.application")
    kotlin("android")
}

val composeVersion = findProperty("version.compose") as String
val composeNavigationVersion = findProperty("version.composeNavigation") as String
val koinVersion = findProperty("version.koin") as String

android {

    compileSdk = (findProperty("android.compileSdk") as String).toInt()

    defaultConfig {
        applicationId = "com.app.app.android"
        minSdk = (findProperty("android.minSdk") as String).toInt()
        targetSdk = (findProperty("android.targetSdk") as String).toInt()
        versionCode = 1
        versionName = "1.0"
    }

    buildFeatures {
        compose = true
    }

    // Set both the Java and Kotlin compilers to target Java 8.
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    composeOptions {
        kotlinCompilerExtensionVersion = composeVersion
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}

dependencies {

    implementation(project(":shared"))

    // Android
    implementation("com.google.android.material:material:1.4.0")

    // Jetpack Compose
    implementation("androidx.compose.ui:ui:$composeVersion")
    implementation("androidx.compose.ui:ui-tooling:$composeVersion")
    implementation("androidx.compose.foundation:foundation:$composeVersion")
    implementation("androidx.compose.material:material:$composeVersion")
    implementation("androidx.compose.material:material-icons-core:$composeVersion")
    implementation("androidx.compose.material:material-icons-extended:$composeVersion")
    implementation("androidx.activity:activity-compose:$composeVersion")
    implementation("androidx.navigation:navigation-compose:$composeNavigationVersion")

    // Koin
    implementation("io.insert-koin:koin-android:$koinVersion")
}

configurations.all {
    resolutionStrategy {
        force("org.jetbrains.kotlin:kotlin-stdlib:1.5.31")
    }
}

我的共享模塊的build.gradle.kts:

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

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64() // sure all ios dependencies support this target

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../ios/Podfile")
        framework {
            baseName = "shared"
        }
    }

    val multiplatformVersion = findProperty("version.multiplatformSettings") as String
    val koinVersion = findProperty("version.koin") as String
    val coroutinesVersion = findProperty("version.coroutines") as String
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("com.russhwolf:multiplatform-settings-no-arg:$multiplatformVersion")
                implementation("io.insert-koin:koin-core:$koinVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        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 {
    compileSdk = 31
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 22
        targetSdk = 31
    }
}

最后,我的 gradle.properties:

# Gradle
org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M"

# Kotlin
kotlin.code.style=official

# Android
android.useAndroidX=true
android.compileSdk=31
android.targetSdk=31
android.minSdk=22

# MPP
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.mpp.enableCInteropCommonization=true
kotlin.native.enableDependencyPropagation=false
kotlin.native.ignoreDisabledTargets=true

# Common versions
version.multiplatformSettings=0.8.1
version.koin=3.1.4
version.coroutines=1.5.2-native-mt

# Android versions
version.compose=1.0.5
version.composeNavigation=2.4.0-rc01

提前致謝。

Denny Kurniawan 上面的評論為我指明了正確的方向。 更改項目名稱后,我遇到了類似的問題。 在“OnboardingScreen.kt”文件的頂部,檢查完整的“包”名稱是否與您的實際項目名稱匹配,例如“包 com.android.application”。

If the package name listed in 'OnboardingScreen.kt' is not the same as your actual project's name, then the static R class is probably not being imported correctly and, hence, this error.

換句話說,“OnboardingScreen.kt”中的完整 package 名稱應與“app->src->main->java”下顯示的文件夾名稱匹配。 希望這對我有幫助。

這就是您引用圖像資源的方式:

import com.your.package.R 

val img = R.drawable.img_name

或者

val img = com.your.package.R.drawable.img_name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM