gpt4 book ai didi

android - KMM + 撰写 : Unresolved reference: drawable

转载 作者:行者123 更新时间:2023-12-05 00:09:20 29 4
gpt4 key购买 nike

我有一个 KMM 应用程序,其中 android 目标使用 Jetpack Compose。尝试使用可绘制资源时出现以下错误:

e: [...]/OnboardingScreen.kt: (33, 46): Unresolved reference: drawable


这是尝试通过 painterResource(id = R.drawable.ic_icon) 访问可绘制对象的结果。 .
我尝试了以下方法来解决此问题:
  • 清理并构建项目
  • 使缓存无效并重新启动
  • 修复执行 ./gradlew assembleDebug 时的所有警告
  • 静态 R 类正确导入

  • 以上都不能解决问题。
    这是我的 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
    提前致谢。

    最佳答案

    这就是您引用图像资源的方式:

    import com.your.package.R 

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

    关于android - KMM + 撰写 : Unresolved reference: drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70479168/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com