icerockdev / moko-mvvm

Model-View-ViewModel architecture components for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
997 stars 95 forks source link

Cannot find type LiveData in scope #78

Closed skajake closed 3 years ago

skajake commented 3 years ago

I am trying to set up an initial project. However, in iOS, I get errors in MultiPlatformLibraryMVVM files:

Cannot find type LiveData in scope Cannot find type UITextFieldBindingKt in scope

It is not clear to me where iOS is supposed to pull in the LiveData from. It does not seem to come in from the MultiPlatformLibrary framework, despite me referencing LiveData in a SimpleViewModel class.

Thanks!

Alex009 commented 3 years ago

hi, @skajake ! livedata should come from MultiPlatformLibrary.framework. MultiPlatformLibrary.framework is framework compiled from Kotlin, with connected moko-mvvm and configured exports. https://kotlinlang.org/docs/reference/mpp-build-native-binaries.html#export-dependencies-to-binaries

in sample it's configured here: enable framework binary with name MultiPlatformLibrary - https://github.com/icerockdev/moko-mvvm/blob/master/sample/mpp-library/build.gradle.kts#L5 depend on moko-mvvm by api - https://github.com/icerockdev/moko-mvvm/blob/master/sample/mpp-library/build.gradle.kts#L14 add moko-mvvm to export - https://github.com/icerockdev/moko-mvvm/blob/master/sample/mpp-library/build.gradle.kts#L18

skajake commented 3 years ago

Thanks Alex,

I am using the cocoapods plugin and I am not sure how to export a dependency that way. If I add a framework attribute to ios in addition to using the cocoapods plugin I get an error: "Cannot create binary debugFramework: binary with such a name already exists"

Alex009 commented 3 years ago

hi! sorry for late reply. when you use cocoapods plugin you can add export this way:

kotlin {
    // export correct artifact to use all classes of moko-resources directly from Swift
    targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java).all {
        val arch = when (this.konanTarget) {    
            org.jetbrains.kotlin.konan.target.KonanTarget.IOS_ARM64 -> "iosarm64"   
            org.jetbrains.kotlin.konan.target.KonanTarget.IOS_X64 -> "iosx64"   
            else -> throw IllegalArgumentException()    
        }
        binaries.withType(org.jetbrains.kotlin.gradle.plugin.mpp.Framework::class.java).all {
            export("dev.icerock.moko:mvvm-$arch:0.8.1")
        }
    }
}