KMM + Compose:未解决的参考:可绘制
KMM + Compose: Unresolved reference: drawable
我有一个 KMM 应用程序,其中 android 目标使用 Jetpack Compose。当我尝试使用可绘制资源时出现以下错误:
e: [...]/OnboardingScreen.kt: (33, 46): Unresolved reference: drawable
这是尝试通过 painterResource(id = R.drawable.ic_icon)
访问可绘制对象的结果。
我已经尝试了以下方法来解决这个问题:
- 清理并构建项目
- 使缓存无效并重新启动
- 修复执行时的所有警告
./gradlew assembleDebug
- 静态 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' 文件的顶部,检查完整的 'package' 名称是否与您的实际项目名称匹配,例如“包 com.android.application”。
如果 'OnboardingScreen.kt' 中列出的包名称与您的实际项目名称不同,则静态 R class 可能未正确导入,因此会出现此错误。
换句话说,'OnboardingScreen.kt' 中的完整包名称应与 'app->src->main->java' 下显示的文件夹名称相匹配。希望这对我有所帮助。
你就是这样引用图片资源的:
import com.your.package.R
val img = R.drawable.img_name
或
val img = com.your.package.R.drawable.img_name
我有一个 KMM 应用程序,其中 android 目标使用 Jetpack Compose。当我尝试使用可绘制资源时出现以下错误:
e: [...]/OnboardingScreen.kt: (33, 46): Unresolved reference: drawable
这是尝试通过 painterResource(id = R.drawable.ic_icon)
访问可绘制对象的结果。
我已经尝试了以下方法来解决这个问题:
- 清理并构建项目
- 使缓存无效并重新启动
- 修复执行时的所有警告
./gradlew assembleDebug
- 静态 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' 文件的顶部,检查完整的 'package' 名称是否与您的实际项目名称匹配,例如“包 com.android.application”。
如果 'OnboardingScreen.kt' 中列出的包名称与您的实际项目名称不同,则静态 R class 可能未正确导入,因此会出现此错误。
换句话说,'OnboardingScreen.kt' 中的完整包名称应与 'app->src->main->java' 下显示的文件夹名称相匹配。希望这对我有所帮助。
你就是这样引用图片资源的:
import com.your.package.R
val img = R.drawable.img_name
或
val img = com.your.package.R.drawable.img_name