icerockdev / moko-mvvm

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

Installation with default KMM template Cocoapods #164

Open IlyasNN opened 2 years ago

IlyasNN commented 2 years ago

Hello! @Alex009 I have created KMM project from default template with cocoapods. I'm IOS developer and Gradle is hard for understanding. There is tutorial in MOKO MVVM REAMDE, but Gradle files are different. All my attempts end with working android app and crashing IOS app. I have no idea why it is crashing.

This is empty project created from the template: https://github.com/IlyasNN/MultiplatformMokoExample

Can you show how dependencies should be written to work great. Thanks!

IlyasNN commented 2 years ago

I use MacBook Air M1

My Gradle file

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins { kotlin("multiplatform") kotlin("native.cocoapods") id("com.android.library") }

version = "1.0"

kotlin { android()

val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
    System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
    System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
    else -> ::iosX64
}

iosTarget("ios") {}

cocoapods {
    summary = "Some description for the Shared Module"
    homepage = "Link to the Shared Module homepage"
    ios.deploymentTarget = "14.1"
    frameworkName = "MultiPlatformLibrary"
    podfile = project.file("../iosApp/Podfile")
}

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("dev.icerock.moko:mvvm-core:0.11.0") // only ViewModel, EventsDispatcher, Dispatchers.UI
            implementation("dev.icerock.moko:mvvm-livedata:0.11.0") // api mvvm-core, LiveData and extensions
            implementation("dev.icerock.moko:mvvm-state:0.11.0") // api mvvm-livedata, ResourceState class and extensions
        }
    }
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))
        }
    }
    val androidMain by getting {
        dependencies {
            implementation("dev.icerock.moko:mvvm-livedata-material:0.11.0") // api mvvm-livedata, Material library android extensions
            implementation("dev.icerock.moko:mvvm-livedata-glide:0.11.0") // api mvvm-livedata, Glide library android extensions
            implementation("dev.icerock.moko:mvvm-livedata-swiperefresh:0.11.0") // api mvvm-livedata, SwipeRefreshLayout library android extensions
            implementation("dev.icerock.moko:mvvm-databinding:0.11.0") // api mvvm-livedata, DataBinding support for Android
            implementation("dev.icerock.moko:mvvm-viewbinding:0.11.0") // api mvvm-livedata, ViewBinding support for Android
        }
    }
    val androidTest by getting {
        dependencies {
            implementation(kotlin("test-junit"))
            implementation("junit:junit:4.13.2")
        }
    }
    val iosMain by getting
    val iosTest by getting
}

}

android { compileSdkVersion(31) sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") defaultConfig { minSdkVersion(21) targetSdkVersion(31) } }

And errors:

Alex009 commented 2 years ago

at this moment none of moko libs not support iosSimulatorArm64 target. now supported only iosX64, iosArm64. disable iosSimulatorArm64 target and all will be fixed. in future updates we will add iosSimulatorArm64 target

IlyasNN commented 2 years ago

How should it be disabled? @Alex009

Alex009 commented 2 years ago

change this:

val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
    System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
    System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
    else -> ::iosX64
}

to

val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
    System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
    else -> ::iosX64
}
IlyasNN commented 2 years ago

@Alex009 That doesn't solve the problem.

Even if I add arm64 in excluded architectures in Xcode project I face with the next error:

Alex009 commented 2 years ago

did you try to run application on ios simulator? you can't do this without target that was removed by code from last my comment. you should try to run on ios device

terrakok commented 2 years ago

try to run ./gradlew :MultiPlatformLibrary:compileKotlinIos --info from terminal in root. and show result