moko-resources icon indicating copy to clipboard operation
moko-resources copied to clipboard

MacOS executable target copy resources logic

Open zacharee opened this issue 3 years ago • 5 comments

I'm trying to create a macOS target for a Kotlin/Compose multiplatform project using moko-resources. If I use runDebugExecutableMacosArm64 or createDistributableNativeDebugMacosArm64 I get an error:

kotlin.IllegalArgumentException: bundle with identifier tk.zwander.samloaderkotlin.resources.MR not found

Looking through the readme, I see mentions of exporting frameworks for native Swift and iOS, but I'm not sure how to apply this to a purely Kotlin project. I'm not directly building with Xcode (just IntelliJ), so I'm not sure where I would put the extra build commands. I also looked through the sample, but I don't see a Kotlin Native macOS module there.

Is it currently possible to use moko-resources in with a Kotlin Native macOS target, and if so, how should I go about doing it?

Here's a link to my project: https://github.com/zacharee/SamloaderKotlin.

zacharee avatar Jul 22 '22 23:07 zacharee

you can use this gradle config:


tasks.withType<KotlinNativeLink>()
    .matching { linkTask -> linkTask.binary is AbstractExecutable }
    .configureEach {
        val task: KotlinNativeLink = this

        doLast {
            val binary: NativeBinary = task.binary
            val outputDir: File = task.outputFile.get().parentFile
            task.libraries
                .filter { library -> library.extension == "klib" }
                .filter(File::exists)
                .forEach { inputFile ->
                    val klibKonan = KonanFile(inputFile.path)
                    val klib = KotlinLibraryLayoutImpl(
                        klib = klibKonan,
                        component = "default"
                    )
                    val layout = klib.extractingToTemp

                    // extracting bundles
                    layout
                        .resourcesDir
                        .absolutePath
                        .let(::File)
                        .listFiles(FileFilter { it.extension == "bundle" })
                        // copying bundles to app
                        ?.forEach { bundleFile ->
                            logger.info("${bundleFile.absolutePath} copying to $outputDir")
                            bundleFile.copyRecursively(
                                target = File(outputDir, bundleFile.name),
                                overwrite = true
                            )
                        }
                }
        }
    }

tasks.withType<AbstractNativeMacApplicationPackageAppDirTask> {
    val task: AbstractNativeMacApplicationPackageAppDirTask = this

    doLast {
        val execFile: File = task.executable.get().asFile
        val execDir: File = execFile.parentFile
        val destDir: File = task.destinationDir.asFile.get()
        val bundleID: String = task.bundleID.get()

        val outputDir = File(destDir, "$bundleID.app/Contents/Resources")
        outputDir.mkdirs()

        execDir.listFiles().orEmpty()
            .filter { it.extension == "bundle" }
            .forEach { bundleFile ->
                logger.info("${bundleFile.absolutePath} copying to $outputDir")
                bundleFile.copyRecursively(
                    target = File(outputDir, bundleFile.name),
                    overwrite = true
                )
            }
    }
}

later we add this inside plugin to copy resources automatically.

https://github.com/zacharee/SamloaderKotlin/pull/75/files here request with required changes in your project

Alex009 avatar Jul 24 '22 08:07 Alex009

Thanks for the code and PR!

zacharee avatar Jul 24 '22 13:07 zacharee

I'm also facing the same issue. I'm trying to use moko-resources for a project that can run on Android, iOS, and macOS. Android and iOS are fine, but when I try to run on macOS I get the same error:

bundle with identifier xxxx.resources.MR not found

Unfortunately it's a closed-source project for my employer, so I can't share the code here.

Will the macOS resources copy logic enhancement be added to a future release of moko-resources?

joshgrossnz avatar Sep 07 '22 19:09 joshgrossnz

One should probably add this line

import org.jetbrains.kotlin.konan.file.File as KonanFile

as a hint how to resolve the KonanFile in the code above.

mipastgt avatar Nov 04 '22 13:11 mipastgt

Hi @Alex009, Do you have any workaround for ios about this problem?

JamshedAlamQaderi avatar Aug 30 '23 09:08 JamshedAlamQaderi