链接 ktor 和 kotlinx.coroutines 会导致未定义的符号

Posted

技术标签:

【中文标题】链接 ktor 和 kotlinx.coroutines 会导致未定义的符号【英文标题】:Linking ktor and kotlinx.coroutines results in undefined symbols 【发布时间】:2019-06-10 20:19:35 【问题描述】:

我正在尝试将我的 Kotlin/Native 项目链接为 ios 框架。 Kotlin 代码编译得很好,但是当链接器运行时,我收到以下错误:

Undefined symbols for architecture x86_64:
  "_kfun:kotlinx.coroutines.AbstractContinuation.getResult$kotlinx-coroutines-core-native()kotlin.Any?", referenced from:
      _kfun:io.ktor.client.engine.ios.IosClientEngine.execute(io.ktor.client.call.HttpClientCall;io.ktor.client.request.HttpRequestData)io.ktor.client.call.HttpEngineCall in combined.o
ld: symbol(s) not found for architecture x86_64
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors

我尝试将平台从iosX64 更改为iosArm64,但会导致相同的错误。这是我的 gradle 文件以供参考,以防我缺少依赖项。

plugins 
    id 'kotlin-multiplatform' version '1.3.11'
    id 'kotlinx-serialization' version '1.3.0'


ext 
    ktor_version = '1.0.1'
    coroutines_version = '1.1.0'
    serialization_version = '0.9.1'


repositories 
    maven  url "https://kotlin.bintray.com/ktor" 
    maven  url "https://kotlin.bintray.com/kotlinx" 
    google()
    jcenter()
    mavenCentral()


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

configurations 
    compilerPlugin


android 
    compileSdkVersion 27
    defaultConfig 
        applicationId "org.jetbrains.kotlin.mpp_app_android"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
    buildTypes 
        release 
            minifyEnabled false
        
    


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'



kotlin 
    targets 
        fromPreset(presets.android, 'android')
        // This preset is for iPhone emulator
        // Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
        fromPreset(presets.iosX64, 'ios') 
            compilations.main.outputKinds('FRAMEWORK')
        
    
    sourceSets 
        commonMain 
            dependencies 
                implementation 'commons-codec:commons-codec:1.10'
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation("io.ktor:ktor-client:$ktor_version")
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
            
        
        commonTest 
            dependencies 
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            
        
        androidMain 
            dependencies 
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation("io.ktor:ktor-client-android:$ktor_version")
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
            
        
        androidTest 
            dependencies 
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            
        
        iosMain 
            dependencies 
                implementation "io.ktor:ktor-client-ios_debug_ios_x64:$ktor_version"

                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
            
        
        iosTest 
        
        configure([ios_x86_64Main, ios_arm64Main]) 
            dependsOn iosMain
        
    


// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework 
    def buildType = project.findProperty("kotlin.build.type") ?: "DEBUG"
    def target = project.findProperty("kotlin.target") ?: "ios"
    dependsOn "link$buildType.toLowerCase().capitalize()Framework$target.capitalize()"

    doLast 
        def srcFile = kotlin.targets."$target".compilations.main.getBinary("FRAMEWORK", buildType)
        def targetDir = getProperty("configuration.build.dir")
        copy 
            from srcFile.parent
            into targetDir
            include 'app.framework/**'
            include 'app.framework.dSYM'
        
    

任何帮助将不胜感激,谢谢!

【问题讨论】:

这看起来像一个链接器错误。你知道ktor 是静态库还是动态库/框架?如果是后者,您还需要在主应用中链接它。 这是一个链接到 ktor 并在 iOS 上运行的示例项目:github.com/adrianbukros/github-multiplatform-example 感谢您的链接。我实际上能够通过从ktor 的版本1.0.1 更新到1.1.1 来解决这个问题。看起来 kotlinx.coroutinesktor 中的 API 需要更新。 【参考方案1】:

您的iosMain Ktor 依赖项似乎有误。尝试这样设置:

implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-core-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-ios:$ktor_version"

【讨论】:

以上是关于链接 ktor 和 kotlinx.coroutines 会导致未定义的符号的主要内容,如果未能解决你的问题,请参考以下文章

将 ktor 应用程序部署到 Heroku 后出现日志错误

Ktor 启动示例错误

Ktor挖坑日记还在用Retrofit网络请求吗?试试Ktor吧

Ktor挖坑日记还在用Retrofit网络请求吗?试试Ktor吧

Ktor 中的表单身份验证

Ktor 与 Kmongo 和 kotlinx.serialization