如何使用登录 kotlin 多平台 Moblie

Viv*_*odi 5 android kotlin ktor kotlin-multiplatform kotlin-multiplatform-mobile

嘿,我在 Ktor 工作。我找到了这个答案来显示特定平台 iOS 和 Android 的日志。但是当我通过 gradle 构建它时,在终端上运行此命令时出现错误。正常构建成功,没有任何错误,但是当我使用以下命令时出现错误。

./gradlew build --warning-mode all 
Run Code Online (Sandbox Code Playgroud)

错误

* What went wrong:
Could not determine the dependencies of task ':kotlinmultiplatformsharedmodule:compileIosMainKotlinMetadata'.
> Could not resolve all artifacts for configuration ':kotlinmultiplatformsharedmodule:allSourceSetsCompileDependenciesMetadata'.
   > Could not resolve io.ktor:ktor-client-logging-native:2.0.1.
     Required by:
         project :kotlinmultiplatformsharedmodule
      > Could not resolve io.ktor:ktor-client-logging-native:2.0.1.
         > Could not get resource 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'.
            > Could not GET 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'. Received status code 403 from server: Forbidden
      > Could not resolve io.ktor:ktor-client-logging-native:2.0.1.
         > Could not get resource 'https://mobile-sdk.jumio.com/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'.
            > Could not GET 'https://mobile-sdk.jumio.com/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'. Received status code 403 from server: Forbidden

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Run Code Online (Sandbox Code Playgroud)

构建.gradle.kts

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

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "13.0"
        framework {
            baseName = "kotlinmultiplatformsharedmodule"
        }
    }

    sourceSets {
        val ktorVersion = "2.0.1"
        val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-logging:$ktorVersion")
                implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
                implementation("io.ktor:ktor-client-auth:$ktorVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
                implementation("io.insert-koin:koin-core:3.2.0-beta-1")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
            }
        }
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
            dependencies {
                implementation("io.ktor:ktor-client-darwin:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-native:$ktorVersion")
            }
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    compileSdk = 32
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以知道我如何使用 iOS/darwin 端的登录吗?谢谢

Nag*_*obi 4

如果您检查Maven 存储库上的 ktor-client-logging-native 工件,您会发现最新版本是 1.3.1。

因此,他们很可能从 v2 开始就以该名称删除了该工件。如果您进行类似搜索ktor-client-logging,您将找到具有-iosx64,-iosarm64架构的工件,其具有v2.0.1.

我找不到合适的 Ktor 文档,但我相信,如果您只io.ktor:ktor-client-logging在其中添加依赖项commonMain,就会找出要引入哪个平台特定的依赖项。

长话短说

您可以删除两者

implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
Run Code Online (Sandbox Code Playgroud)

implementation("io.ktor:ktor-client-logging-native:$ktorVersion")
Run Code Online (Sandbox Code Playgroud)

因为它们将通过指定commonMain依赖关系自动拉入

val commonMain by getting {
    implementation("io.ktor:ktor-client-logging:$ktorVersion")
}
Run Code Online (Sandbox Code Playgroud)

  • #RobertNagy 如果我常用的话。日志未在控制台中打印 (5认同)