android KMM+编写:未解析的引用:可拉伸的

ebdffaop  于 2023-02-06  发布在  Android
关注(0)|答案(3)|浏览(157)

我有一个KMM应用程序,其中的android目标使用Jetpack合成。当我尝试使用可绘制资源时,我得到以下错误:
e:[... ]/车载屏幕kt:(33,46):未解析的引用:可拉伸的
这是尝试通过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

先谢了。

uwopmtnx

uwopmtnx1#

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

import com.your.package.R 

val img = R.drawable.img_name

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

6yt4nkrj2#

Denny Kurniawan上面的评论给我指出了正确的方向。我在更改项目名称后遇到了类似的问题。在"OnboardingScreen.kt“文件的顶部,检查完整的”package“名称是否与实际项目的名称匹配,例如“package com.android.application”。
如果在'OnboardingScreen.kt'中列出的包名与实际项目的名称不同,则静态R类可能没有正确导入,因此出现此错误。
换句话说,“OnboardingScreen.kt”中的完整包名应该与“app-〉src-〉main-〉java”下显示的文件夹名相匹配。希望这对其他人有帮助,就像我一样。

ny6fqffe

ny6fqffe3#

对我来说,这个错误发生,因为我拖我的图像到资源管理器,我解决它的导入图像手动与“导入可绘制”按钮

相关问题