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

Cannot access 'dev.icerock.moko.resources.ResourceContainer' #737

Closed Egi10 closed 4 months ago

Egi10 commented 5 months ago

I encountered an error in my multimodule project after updating to Moko Resources version 0.24.1. The error occurs when using strings in the actual class on the iOS platform (iosMain).

Cannot access 'dev.icerock.moko.resources.ResourceContainer' which is a supertype of 
'id.example.common.resources.FeederRes.strings'. 
Check your module classpath for missing or conflicting dependencies

Project Structure: -shared --common ---resource --core

Example Code:

Common (expect):

expect class CSVGenerate {
fun generateBody()
}
fun getMyFormatDesc(input: String): StringDesc {
    return StringDesc.ResourceFormatted(MR.strings.my_string_formatted, input)
}

iOS (actual):

actual class CSVGenerate {
actual fun generateBody() {
val string = getMyFormatDesc(input: "hello").localized()
}
}

Android (actual):

actual class CSVGenerate {
actual fun generateBody() {
val string = getMyFormatDesc("hello").toString(context = this)
}

Note: This error was encountered during a rebuild in Android Studio, but the project still runs when executed on Android and iOS.

Alex009 commented 5 months ago

hi. can you create small reproducer project?

Egi10 commented 5 months ago

I've tried replicating the issue by creating a sample project, but the same error doesn't occur.

Previously, I was using version 0.23.0 and everything was working smoothly. When the latest version came out and I wanted to upgrade Kotlin, I tried to update. However, I encountered the error.

Egi10 commented 5 months ago

I haven't encountered this issue before, but my project is not working properly. For now, I have downgraded to version 0.23.0 and implemented the solution from https://github.com/icerockdev/moko-resources/issues.

Egi10 commented 4 months ago

After further investigation, I discovered that the root cause of the issue lies in the module naming convention within my project:

-shared --common ---resources --core

This naming structure triggers an error during the execution of compileIosMainKotlinMetadata. However, if I change the module name to anything other than resources, the error disappears.

Example project: https://github.com/Egi10/resources

cc @Alex009

Egi10 commented 4 months ago

I'm now encountering an issue when running the app on iOS with the error: kotlin.IllegalArgumentException: bundle with identifier ***.main not found. I've also added a new configuration, but the problem persists :

"$SRCROOT/../gradlew" -p "$SRCROOT/../" :yourframeworkproject:copy`YourFrameworkName`FrameworkResourcesToApp \
    -Pmoko.resources.BUILT_PRODUCTS_DIR="$BUILT_PRODUCTS_DIR" \
    -Pmoko.resources.CONTENTS_FOLDER_PATH="$CONTENTS_FOLDER_PATH" \
    -Pkotlin.native.cocoapods.platform="$PLATFORM_NAME" \
    -Pkotlin.native.cocoapods.archs="$ARCHS" \
    -Pkotlin.native.cocoapods.configuration="$CONFIGURATION" 
Alex009 commented 4 months ago

This naming structure triggers an error during the execution of compileIosMainKotlinMetadata. However, if I change the module name to anything other than resources, the error disappears.

i think it's kotlin bug with module names clash. https://youtrack.jetbrains.com/issue/KT-57914

Alex009 commented 4 months ago

I've also added a new configuration, but the problem persists

please show full code of your xcode resources build phase and also logs from build process

Egi10 commented 4 months ago

I added the following script to the Build Phase:

"$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:core:copyPodFrameworkResourcesToApp \
  -Pmoko.resources.BUILT_PRODUCTS_DIR="$BUILT_PRODUCTS_DIR" \
  -Pmoko.resources.CONTENTS_FOLDER_PATH="$CONTENTS_FOLDER_PATH" \
  -Pkotlin.native.cocoapods.platform="$PLATFORM_NAME" \
  -Pkotlin.native.cocoapods.archs="$ARCHS" \
  -Pkotlin.native.cocoapods.configuration="$CONFIGURATION"

However, I encountered this error at runtime when trying to access the resources.

Screenshot 2024-07-22 at 13 33 25

I got the copyPodFrameworkResourcesToApp from a warning.

linkPodDebugFrameworkIosArm64 is found.
If you use a static framework, Xcode should have Build Phase with copyPodFrameworkResourcesToApp gradle task call. 
Please read readme on https://github.com/icerockdev/moko-resources
-
To hide this message, add 'moko.resources.disableStaticFrameworkWarning=true' to the Gradle properties.
Egi10 commented 4 months ago

Issue resolved, it turned out there was a problem with my custom plugin.