icerockdev / moko-resources

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

SharedRes cannot be found in XCode Project #767

Closed haqim007 closed 2 months ago

haqim007 commented 2 months ago

Background

I create KMM project as external library then export it as XCFramework. I use the exported XCFramework in my XCode Project via cocoapods. I have been stuck on this issue for days.

Gradle KMM Side

`

import co.touchlab.skie.configuration.DefaultArgumentInterop
import co.touchlab.skie.configuration.EnumInterop
import co.touchlab.skie.configuration.SealedInterop
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.kotlinCocoapods)
    alias(libs.plugins.androidLibrary)
    alias(libs.plugins.kotlin.serialization)
    alias(libs.plugins.skie)
    alias(libs.plugins.ksp)
    alias(libs.plugins.sqldelight)
    alias(libs.plugins.mokoresources)
} 

skie{
    features{
        group {
            SealedInterop.Enabled(true)
            DefaultArgumentInterop.Enabled(true)
            EnumInterop.Enabled(true)
        }
    }
}

kotlin {
    androidTarget {
        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_11)
        }
    }
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Scrumdinger KMM Library"
        homepage = "https://github.com/haqim007"
        version = "1.1.0"
        ios.deploymentTarget = "15.0"
        framework {
            baseName = "ScrumdingerKMMLib"
            isStatic = true
            binaryOptions["bundleId"] = "dev.haqim.scrumdingerKMMLib"
            export(libs.mokoresources)
            freeCompilerArgs += listOf("-Xoverride-konan-properties=minVersion.ios=15.0")
        }
        specRepos {
            name = "ScrumdingerKMMLib"
        }
    }

    sourceSets {
        commonMain.dependencies {
            implementation(libs.kotlinx.datetime)
            implementation(libs.kotlinx.coroutines.core)
            implementation(libs.ktor.client.core)
            implementation(libs.ktor.client.content.negotiation)
            implementation(libs.ktor.serialization.kotlinx.json)
            implementation(libs.skie.configuration.annotations)

            implementation(libs.sqldelight.runtime)
            implementation(libs.kotlinx.datetime)
            implementation(libs.koin.core)
            implementation(libs.sqldelight.coroutines.extensions)
            api(libs.mokoresources)

        }
        androidMain.dependencies {
            implementation(libs.ktor.client.android)
            implementation(libs.sqldelight.android.driver)
        }
        iosMain.dependencies {
            implementation(libs.ktor.client.darwin)
            implementation(libs.sqldelight.native.driver)
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "dev.haqim.scrumdingerkmmlib"
    compileSdk = 34
    defaultConfig {
        minSdk = 24
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
}

sqldelight {
    databases {
        create("MyScrumdingerDB") {
            packageName.set("dev.haqim.scrumdingerkmmlib.data.cache")
        }
    }
}

multiplatformResources {
    resourcesPackage.set("dev.haqim.scrumdingerKMMLib.shared") // required
    resourcesClassName.set("SharedRes") // optional, default MR
}

tasks.register<Exec>("buildAndExport") {
    group = "Custom"
    description = "Build and export XCFramework locally"

    commandLine("sh", "../scripts/local_build.sh")
}

`

Cocoapods XCode project side

`

# Uncomment the next line to define a global platform for your project
platform :ios, '15.0'

target 'Scrumdinger' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!

# Pods for Scrumdinger
pod 'ScrumdingerKMMLib', :path => '../../kmm/ScrumdingerKMMLib/shared/build/cocoapods/publish/release'

target 'ScrumdingerTests' do
    inherit! :search_paths
    # Pods for testing
end

target 'ScrumdingerUITests' do
    # Pods for testing
end

end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "ScrumdingerKMMLib"
            puts "Updating #{target.name} COCOAPODS_SKIP_KOTLIN_BUILD"
            target.build_configurations.each do |config|
                config.build_settings['COCOAPODS_SKIP_KOTLIN_BUILD'] = '$ENABLE_PREVIEWS'
            end
        end
    end
end

`

you can see the full project here

KMM: https://github.com/haqim007/ScrumdingerKMMLib iOS App: https://github.com/haqim007/MyScrumdinger/tree/KMM

Alex009 commented 2 months ago

hi. you use static framework, so you should add xcode build phase - https://github.com/icerockdev/moko-resources?tab=readme-ov-file#iosmacos-static-kotlin-frameworks-support