icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.12k stars 124 forks source link

Resources not generating in Compose Multiplatform project #498

Open hitherejoe opened 1 year ago

hitherejoe commented 1 year ago

Hello!

I'm trying to setup shared string resources, but am running into some issues. I have checked with the sample projects and I can't spot any differences to how I have implemented things, so opening an issue incase this is a bug.

My module structure is the following, with the strings.xml located in the resources/mr/base directory:

Screenshot 2023-05-26 at 12 44 30

Generation happens, but there are no resources being generated. I can access MR.strings in my common main directory, but there are no strings present.

Screenshot 2023-05-26 at 12 45 47

This might be a separate issue related to my project, but I'm seeing this error within the generated file (sharing here incase it is helpful!).

Screenshot 2023-05-26 at 12 46 03

My build.gradle.kts file for this module looks like the following:

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("org.jetbrains.compose")
    id("dev.icerock.mobile.multiplatform-resources")
}

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        ...
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(project(":common:data"))
                api(compose.runtime)
                api(compose.material)
                api(compose.foundation)
                @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                implementation(compose.components.resources)
                api("dev.icerock.moko:resources-compose:0.22.3")
                implementation("org.jetbrains.kotlinx:atomicfu:0.20.2")
            }
        }
        val androidMain 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)
        }
    }
}

multiplatformResources {
    multiplatformResourcesPackage = "academy.droid.jrnl.design"
}

android {
    compileSdk = (findProperty("android.compileSdk") as String).toInt()
    namespace = "academy.droid.jrnl.design"

    defaultConfig {
        minSdk = (findProperty("android.minSdk") as String).toInt()
        targetSdk = (findProperty("android.targetSdk") as String).toInt()
    }
}

Let me know if I can provide any extra information

Alex009 commented 1 year ago

hi. please show strings.xml content. and also check is path to this file is /src/commonMain/resources/MR/base/strings.xml not /src/commonMain/resources/MR.base/strings.xml

jcaiqueoliveira commented 1 year ago

I've got the same problem @Alex009 trying to access a custom font

image image

my configuration to my current module:

plugins {
    id(libs.plugins.multiplatform.get().pluginId)
    alias(libs.plugins.compose)
    id(libs.plugins.android.library.get().pluginId)
    alias(libs.plugins.moko)
}
setupKMP()
kotlin {

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material)
                implementation(libs.composeIcons.featherIcons)
                implementation("dev.icerock.moko:resources-compose:0.23.0")
            }
        }

        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation("dev.icerock.moko:resources-test:0.23.0")
            }
        }

        val androidMain by getting
        val androidUnitTest by getting
        val iosMain by getting

        val iosSimulatorArm64Test by getting
        val iosTest by getting {
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "co.moendy.platform.design"
}

multiplatformResources {
    multiplatformResourcesPackage = "co.moendy.platform.design"
    multiplatformResourcesClassName = "MoendyRes"
}

my setupKmp function:

fun Project.setupKMP() {
    plugins.withType<org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper> {
        extensions.configure<KotlinMultiplatformExtension> {
            explicitApi()

            // app target
            android()

            ios()
            iosSimulatorArm64()

            sourceSets {
                val commonMain by getting
                val commonTest by getting
                val jvmMain by creating {
                    dependsOn(commonMain)
                }
                val jvmTest by creating {
                    dependsOn(commonTest)
                }

                val androidMain by getting {
                    dependsOn(jvmMain)
                }
                val iosMain by getting
                val iosSimulatorArm64Main by getting {
                    dependsOn(iosMain)
                }
            }
        }

        findAndroidExtension().apply {
            setupAndroid()
            sourceSets["main"].apply {
                res.srcDirs("src/androidMain/resources")
                manifest.srcFile("src/androidMain/AndroidManifest.xml")
            }
        }

        tasks.withType<KotlinCompile> {
            kotlinOptions.configureKotlinJvmOptions()
        }
    }
}

Kotlin 1.8.20 Gradle 8 And also the error message:

e: file:...platform/design/build/generated/moko/commonMain/src/co/moendy/platform/design/MoendyRes.kt:12:22 Expected object 'MoendyRes' has no actual declaration in module for JVM

jcaiqueoliveira commented 1 year ago

Removing android.defaults.buildfeatures.resvalues=false from my gradle.properties solves to build on Android.

If I fix that on iOS I'll update here

schachi5000 commented 1 year ago

@jcaiqueoliveira I'm having the same Issue with iOS. Did you get it to work?

shalva97 commented 6 months ago

Any update here? I have similar issue on a project at work. tried to create separate project to repoduce the issue but it does not generate anything except empty folders