构建 iOS 框架时未找到 Ktor 和 kotlinx 依赖项

Posted

技术标签:

【中文标题】构建 iOS 框架时未找到 Ktor 和 kotlinx 依赖项【英文标题】:Ktor and kotlinx dependencies not found when building iOS framework 【发布时间】:2019-06-10 18:59:56 【问题描述】:

我正在尝试在我的 Kotlin/Native 项目中包含对 ktorkotlinx(准确地说是serializationcoroutines)的支持。所有库都可以在项目的 android 版本上运行,但是当我尝试编译 Xcode 项目时,我收到错误消息,指出找不到 kotlinx.serializationkotlinx.coroutines 库:

src/commonMain/kotlin/.../.kt:4:16: error: unresolved reference: coroutines
import kotlinx.coroutines.CoroutineDispatcher
               ^
src/commonMain/kotlin/.../.kt:5:16: error: unresolved reference: serialization
import kotlinx.serialization.json.JSON

这是我的项目的 gradle 文件:

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


ext 
    ktor_version = '1.0.1'
    kotlinx_version = '1.1.0'


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:$kotlinx_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.9.1"
            
        
        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:$kotlinx_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1"
            
        
        androidTest 
            dependencies 
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            
        
        iosMain 
            dependencies 
                implementation("io.ktor:ktor-client-ios:$ktor_version")
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlinx_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1"
            
        
        iosTest 
        
    


// 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'
        
    

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

【问题讨论】:

【参考方案1】:

确保enableFeaturePreview("GRADLE_METADATA")settings.gradle 文件中。一旦我添加了这一行,我就可以从 Xcode 编译了。

【讨论】:

【参考方案2】:

我也遇到了这个问题。它突然出现在一个我已经工作了几个月的项目上。我确实在settings.gradle 中设置了enableFeaturePreview('GRADLE_METADATA')。会发生什么?这真是令人沮丧;我的项目突然无缘无故停止了。

【讨论】:

以上是关于构建 iOS 框架时未找到 Ktor 和 kotlinx 依赖项的主要内容,如果未能解决你的问题,请参考以下文章

“未找到转换:类 io.ktor.utils.io.ByteChannelNative”错误使用 Ktor

Ktor App 部署到 AppEngine 时未调用 Main 方法

未找到“响应”类的 Ktor 序列化程序

Ktor 1.0 发布:Kotlin Web 框架;GoLand 2018.3 正式版发布!| 更新

构建发布和调试 apk 时未找到 Firebase Auth AndroidX 依赖项

构建新的 Azure Fabric 服务时未找到清单文件