3

我是 KMM 的新手,我正在尝试将 KMM 项目作为 pod 集成到我的示例 Xcode 项目中。我可以将 KMM Pod 链接到我的 Xcode 项目。但是,如果我尝试调用其中一个函数,则会产生以下错误。如果我不调用任何 KMM 函数,它可以在我的模拟器中运行应用程序。

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)

KMM 应用程序只是一个简单的应用程序。SharedCode -> commonMain -> kotlin -> Greeting 里面有一个 Greeting 类

下面是我的配置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)
    }
}

这是我的项目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)
}

基本上我只是按照https://kotlinlang.org/docs/mobile/create-first-app.html的教程。还没有什么复杂的。有什么我错过了吗?

我的机器配置: Mac mini M1 Xcode 13 beta 2 MacOS 11.4

谢谢

4

2 回答 2

6

错误:

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

您正在尝试为 M1 芯片模拟器构建Simulator-arm64,但 KMM 尚不支持。您的 KMM 构建适用于英特尔,Simulator-x86_64.

对于 M1 Mac,您需要在您的实际 iPhone 设备上运行它或在 Rosetta 下运行 Xcode(感谢评论中的 Philip)。

于 2021-07-08T12:28:00.133 回答
0

您需要导入 shared因为 KMM 使用共享模块。

于 2021-11-10T12:39:10.737 回答