Foster
Foster

Reputation: 385

KMM Application Cannot Run In Simulator

I am new to KMM and I am trying to integrate KMM project as a pod to my sample Xcode Project. I can link the KMM Pod to my Xcode project. However if I try to call one of the function, below error yielded. If I do not call any of the KMM function, it can run the app in my simulator.

ViewController

import UIKit
import Multiplatform

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let a = Greeting()
        a.greeting()
    }
}
ld: warning: ignoring file /Users/avjiang/Developments/Multiplatform/SharedCode/build/cocoapods/framework/Multiplatform.framework/Multiplatform, building for iOS Simulator-arm64 but attempting to link with file built for iOS Simulator-x86_64
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_MultiplatformGreeting", referenced from:
      objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The KMM application is just a simple app. There is a Greeting class inside SharedCode -> commonMain -> kotlin -> Greeting

Below is my configuration for build.gradle.kts

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

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

version = "1.0"

kotlin {
    android()

    cocoapods {
        // Configure fields required by CocoaPods.
        summary = "Some description for a Kotlin/Native module"
        homepage = "Link to a Kotlin/Native module homepage"
        frameworkName = "Multiplatform"
        ios.deploymentTarget = "13.5"
        podfile = project.file("/Users/avjiang/Developments/TestKotlinMultiplatformPod/Podfile")
    }

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iosTarget("ios") { }

    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting
        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(30)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(19)
        targetSdkVersion(30)
    }
}

And this is my project build.gradle.kts

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20")
        classpath("com.android.tools.build:gradle:4.2.2")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

Basically I am just following the tutorial from https://kotlinlang.org/docs/mobile/create-first-app.html. Nothing complex yet. Is there anything I missed out?

My machine configuration: Mac mini M1 Xcode 13 beta 2 MacOS 11.4

Thank you

Upvotes: 3

Views: 3255

Answers (3)

Huy Tower
Huy Tower

Reputation: 7976

Issue might be happen at these lines:

val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
    if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
        ::iosArm64
    else
        ::iosX64

Trying to include all architecture in KMM project: iosArm64, iosX64, iosArm64Simulator

To avoid issue: ld:

symbol(s) not found for architecture arm64

Upvotes: 0

Danny
Danny

Reputation: 119

You need to import shared because KMM uses shared module.

Upvotes: 0

Kevin Galligan
Kevin Galligan

Reputation: 17342

The error:

ld: warning: ignoring file /Users/avjiang/Developments/Multiplatform/SharedCode/build/cocoapods/framework/Multiplatform.framework/Multiplatform, building for iOS Simulator-arm64 but attempting to link with file built for iOS Simulator-x86_64

You're trying to build for the M1 chip simulator, Simulator-arm64, but KMM does not support that yet. Your KMM build is for Intel, Simulator-x86_64.

For M1 Macs, you'll need to run this on your actual iPhone device or run Xcode under Rosetta (thanks to Philip in the comments).

Upvotes: 8

Related Questions