简体   繁体   中英

iOS dependences not resolving in Kotlin MultiPlatform Project

I am trying to develop a small library to post issues to my company's Jira server, and I thought that a Kotlin MPP w/ KTOR would be just the ticket.

At first, following a few tutorials, I made a shared project, and the imports for iOS were working fine but Android's Ktor implementation would not resolve. Then I realized that I needed to recreate the project and create a library instead of a shared application, as I have existing codebases for each mobile client already, and I need to publish the MPP library to be used by them.

Upon recreating the project as a library, and simply starting to add the dependencies for KTOR 1.3.2 , the iOS dependencies are failing to resolve. This is not just KTOR, it is any iOS dependency, so there's obviously something incorrect in my project setup, but I am unable to spot it.

Here is the gradle file:

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

The console output is

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.

Anything obvious that I am missing here?

UPDATE

I scrapped and recreated the project w/ an updated version of IntelliJ (IntelliJ IDEA 2019.3.5 (Community Edition)) & the Kotlin 1.4.0 Plugin installed. This gave me a slightly different creation wizard, and the option to use Kotlin as the Gradle syntax.

Updated build.gradle.kts file:

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
        }
    }
}

The gradle dependencies for iOS successfully sync when I set the ktor_version to 1.3.2 but not for 1.4.0 (assuming the mirrors haven't updated for the native files??)... but the imports don't compile when I attempt to utilize the class at all... see attached image:

在此处输入图像描述

I would guess you don't have enableFeaturePreview("GRADLE_METADATA") in settings.gradle .

settings.gradle

Check our starter project KaMPKit for a running example on 1.3.72. We'll probably bump that to 1.4.0 this week, but for now it should be a good reference.

Here are my dependencies (I have separate iOS targets, but it should still help you):

        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"
        }

Versions:

ktorVersion=1.3.2

Repositories:

    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/")

To configure client serializer try to write it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM