【问题标题】:iOS dependences not resolving in Kotlin MultiPlatform Project在 Kotlin MultiPlatform 项目中未解决 iOS 依赖项
【发布时间】:2020-12-08 08:10:14
【问题描述】:

我正在尝试开发一个小型库来将问题发布到我公司的 Jira 服务器,我认为带有 KTOR 的 Kotlin MPP 将只是门票。

起初,在学习了一些教程后,我做了一个共享项目,iOS 的导入工作正常,但 Android 的 Ktor 实现无法解决。然后我意识到我需要重新创建项目并创建一个库而不是共享应用程序,因为我已经为每个移动客户端拥有了现有的代码库,并且我需要发布 MPP 库以供他们使用。

将项目重新创建为库并开始添加 KTOR 1.3.2 的依赖项后,iOS 依赖项无法解析。这不仅仅是 KTOR,它是任何 iOS 依赖项,所以我的项目设置中显然有一些不正确的地方,但我无法发现它。

这里是 gradle 文件:

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}

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

group 'com.example.issuereporter'
version '0.0.1'

apply plugin: 'maven-publish'

kotlin {

    targets {
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'ios') {
            binaries {
                framework('IssueReporter')
            }
        }


        fromPreset(presets.jvm, 'android')
    }

    def ktor_version = "1.3.2"

    sourceSets["commonMain"].dependencies {
        implementation kotlin('stdlib-common')

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

    sourceSets["commonTest"].dependencies {
        implementation kotlin('test-common')
        implementation kotlin('test-annotations-common')
    }

    sourceSets["androidMain"].dependencies {
        implementation kotlin('stdlib')

        implementation "io.ktor:ktor-client-core-jvm:$ktor_version"
        implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
        implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
        implementation "io.ktor:ktor-client-auth-jvm:$ktor_version"

    }

    sourceSets["androidTest"].dependencies {
        implementation kotlin('test')
        implementation kotlin('test-junit')
    }

    sourceSets["iosMain"].dependencies {
        implementation "io.ktor:ktor-client-ios:$ktor_version"
        implementation "io.ktor:ktor-client-core-native:$ktor_version"
        implementation "io.ktor:ktor-client-json-native:$ktor_version"
        implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
    }


}

configurations {
    compileClasspath
}


task packForXcode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
    final def framework = kotlin.targets.ios.binaries.getFramework("IssueReporter", mode)

    inputs.property "mode", mode
    dependsOn framework.linkTask

    from { framework.outputFile.parentFile }
    into frameworkDir

    doLast {
        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}

tasks.build.dependsOn packForXcode

控制台输出是

Could not resolve io.ktor:ktor-client-ios:1.3.2.
Could not resolve io.ktor:ktor-client-core-native:1.3.2.
Could not resolve io.ktor:ktor-client-json-native:1.3.2.
Could not resolve io.ktor:ktor-client-serialization-native:1.3.2.

我在这里缺少什么明显的东西吗?

更新

我废弃并重新创建了项目,并安装了更新版本的 IntelliJ(IntelliJ IDEA 2019.3.5(社区版))和 Kotlin 1.4.0 插件。这给了我一个稍微不同的创建向导,以及使用 Kotlin 作为 Gradle 语法的选项。

更新build.gradle.kts文件:

plugins {
    kotlin("multiplatform") version "1.4.0"
    kotlin("plugin.serialization") version "1.4.0"
    id("com.android.library")
    id("kotlin-android-extensions")
}
group = "com.example.issuereporter"
version = "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
    maven(url = "https://kotlin.bintray.com/kotlinx")
    maven(url = "https://dl.bintray.com/kotlin/ktor")
    maven(url = "https://repo1.maven.org/maven2/")
    maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
    maven(url = "https://plugins.gradle.org/m2/")
}
kotlin {
    android()
    iosX64("ios") {
        binaries {
            framework {
                baseName = "library"
            }
        }
    }

    val ktor_version = "1.3.2"
    val serialization_version = "0.20.0"

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:$ktor_version")
                implementation("io.ktor:ktor-client-json:$ktor_version")
                implementation("io.ktor:ktor-client-serialization:$ktor_version")
                implementation("io.ktor:ktor-client-auth:$ktor_version")
                implementation("io.ktor:ktor-client-apache:$ktor_version")

                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("androidx.core:core-ktx:1.2.0")

                implementation("io.ktor:ktor-client-android:$ktor_version")
                implementation("io.ktor:ktor-client-auth-jvm:$ktor_version")
                implementation("io.ktor:ktor-client-json-jvm:$ktor_version")
            }
        }
        val androidTest by getting
        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:$ktor_version")
                implementation ("io.ktor:ktor-client-core-native:$ktor_version")
                implementation("io.ktor:ktor-client-json-native:$ktor_version")
                implementation("io.ktor:ktor-client-auth-native:$ktor_version")
            }
        }
        val iosTest by getting
    }
}
android {
    compileSdkVersion(29)
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}

当我将ktor_version 设置为1.3.2 而不是1.4.0 时,iOS 的 gradle 依赖项成功同步(假设镜像尚未针对本机文件更新??)...但导入没有当我尝试使用该类时,t 编译...见附图:

【问题讨论】:

  • 只保留commonMain 中的那些,并在其他特定平台上使用...-core-xyz 删除其他所有内容;例如删除io.ktor:ktor-client-core-native
  • 对于 kotlin 1.4.0,请按照 Dr.jacky 所说的进行。 kotlinlang.org/docs/reference/…
  • 它是如何工作的?原生实现不会失败吗?

标签: android ios kotlin build.gradle kotlin-multiplatform


【解决方案1】:

我猜你在settings.gradle 中没有enableFeaturePreview("GRADLE_METADATA")

settings.gradle

查看我们的启动项目KaMPKit 以获取 1.3.72 上的运行示例。我们可能会在本周将它升级到 1.4.0,但现在它应该是一个很好的参考。

【讨论】:

  • 啊哈!我忘了提这个,但是我提! `rootProject.name = 'IssueReporter' enableFeaturePreview('GRADLE_METADATA') `
  • (但我会检查入门工具包,谢谢!- 并且谢谢你迄今为止在 Kotlin MPP 上的所有工作和宣传!)
  • 感觉ktor-client-core-native 1.4.0 还没有发布。至少在公共存储库mvnrepository.com/artifact/io.ktor/ktor-client-core-native 中缺少它。顺便说一句,我现在正在尝试在 1.4 上迁移 KaMPKit,并期待完全相同的问题。
  • KaMPKit for 1.4.0 依赖于许多库。我目前正在与 sqldelight 搏斗。 1.4.0 是一个重大更新,并且有许多与“原生”无关的配置问题,因此图书馆生态系统将需要比平时更多的时间来整理。不过,正在取得进展。祝我好运...
  • @KevinGalligan 更新到 Gradle 6.0 后,您可以从项目的 settings.gradle 文件中删除 enableFeaturePreview("GRADLE_METADATA")kotlinlang.org/docs/reference/…
【解决方案2】:

这是我的依赖项(我有单独的 iOS 目标,但它仍然可以帮助你):

        iosEmulatorMain.dependencies {
            implementation "io.ktor:ktor-client-ios:$ktorVersion"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutinesVersion"
            implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serializationVersion"
            implementation "com.soywiz.korlibs.klock:klock-iosx64:$klockVersion"
            implementation "com.github.aakira:napier-iosX64:$napierVersion"
        }
        iosDeviceMain.dependencies {
            implementation "io.ktor:ktor-client-ios:$ktorVersion"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutinesVersion"
            implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serializationVersion"
            implementation "com.soywiz.korlibs.klock:klock-iosarm64:$klockVersion"
            implementation "com.github.aakira:napier-iosArm64:$napierVersion"
        }

版本:

ktorVersion=1.3.2

存储库:

    maven(url = "https://kotlin.bintray.com/kotlinx")
    maven(url = "https://dl.bintray.com/kotlin/ktor")
    maven(url = "https://repo1.maven.org/maven2/")
    maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
    maven(url = "https://plugins.gradle.org/m2/")

要配置客户端序列化程序,请尝试这样编写:

install(JsonFeature) {
    serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
        ignoreUnknownKeys = true 
    })
}

【讨论】:

  • 谢谢...这仍然不起作用,我看不出您的依赖声明有任何区别,只有您的存储库...
  • 我也觉得奇怪的是我需要其他存储库 - ktor.io/quickstart/quickstart/gradle.html 的指南只提到 jcenter()ktor.io/clients/http-client/multiplatform.html 的指南没有提及任何内容,所以我认为这没有改变。 ..
  • 谢谢。你在添加序列化插件吗?
  • 是的,像这样的插件 { kotlin("multiplatform") kotlin("plugin.serialization") }
  • 我可以暂时让导入工作,但它只是放弃并且在几次 gradle 同步后无法解决......这感觉没有希望。我非常有信心这与我当地的项目结构有关......
猜你喜欢
  • 2021-09-08
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 2021-06-22
  • 2017-05-14
相关资源
最近更新 更多