rickclephas / KMP-ObservableViewModel

Library to use AndroidX/Kotlin ViewModels with SwiftUI
MIT License
591 stars 28 forks source link

Could not find "org.jetbrains.kotlin.native.platform.CoreFoundationBase" #28

Closed pabloski0000 closed 1 year ago

pabloski0000 commented 1 year ago

Hi @rickclephas, here is a bit of context about the problem: Version: 1.0.0-ALPHA-8 I've followed the README and for android everything is working OK. However, I added the package to Xcode and when I built the project it thrown an exception. Here is an extract of it: The following Kotlin source sets were configured but not added to any Kotlin compilation: androidAndroidTestRelease androidTestFixtures androidTestFixturesDebug androidTestFixturesRelease You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'. See https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets Task :shared:compileKotlinIosSimulatorArm64 FAILED e: Could not find "org.jetbrains.kotlin.native.platform.CoreFoundationBase" in [/Users/pabloski/projects/ShoppingListClientMobile, /Users/pabloski/.konan/klib, /Users/pabloski/.konan/kotlin-native-prebuilt-macos-aarch64-1.7.20/klib/common, /Users/pabloski/.konan/kotlin-native-prebuilt-macos-aarch64-1.7.20/klib/platform/ios_simulator_arm64]

I then tried adding extension Kmm_viewmodel_coreKMMViewModel: KMMViewModel { } but I receive this error in compile time Cannot find type 'Kmm_viewmodel_coreKMMViewModel' in scope

Thank you in advance.

rickclephas commented 1 year ago

It seems you are using Kotlin 1.7.20 with 1.0.0-ALPHA-8 which uses Kotlin 1.8.21. Unfortunately there is no KMM-ViewModel version that uses Kotlin 1.7.20. However you could try 1.0.0-ALPHA-2 which uses Kotlin 1.7.21.

pabloski0000 commented 1 year ago

Thank you for answering so quickly.

I've updated my project to 1.8.21 and now it compiles in IOS but in android it shows me this error: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class main.shoppilientmobile.shoppingList.infrastructure.sharedViewModels.TestViewModel, unresolved supertypes: com.rickclephas.kmm.viewmodel.KMMViewModel Adding -Xextended-compiler-checks argument might provide additional information.

rickclephas commented 1 year ago

Could you try to add the following dependency to your Android module?:

androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1

I'll fix this dependency issue in the next release.

pabloski0000 commented 1 year ago

It'd been already added:

dependencies {
    implementation(project(":shared"))
    .
    .
    .
    //KMM-ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
}

I also tried adding the dependency to the shared module in androidMain:

val androidMain by getting {
            dependencies {
                //KMM-ViewModel
                implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
            }
        }

So you know, I'm now using 1.0.0-ALPHA-8 version.

rickclephas commented 1 year ago

Alright could you possibly share your project structure? Is TestViewModel declared in your shared module?

pabloski0000 commented 1 year ago

Yes, of corse. Here is TestViewModel It is in commonMainmodule

package main.shoppilientmobile.shoppingList.infrastructure.sharedViewModels

import com.rickclephas.kmm.viewmodel.KMMViewModel
import com.rickclephas.kmm.viewmodel.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class TestViewModel : KMMViewModel() {
    private val _message = MutableStateFlow(viewModelScope, "This a test to share viewmodels")
    val message = _message.asStateFlow()
}

And I use it in a composable to test it

LaunchedEffect(Unit) {
            val testViewModel = TestViewModel()
            val message = testViewModel.message
        }

The error is due to val message = testViewModel.message

rickclephas commented 1 year ago

Thanks! Please add the KMM-ViewModel dependency to your Android module as well. That should do the trick.

pabloski0000 commented 1 year ago

Yes, it worked. Thank you very much!!! Let's move the test to a real project!