sorry for my dumb questions, but i just got back i...
# kotlin-native
t
sorry for my dumb questions, but i just got back into KN/multiplatform and things have changed a bit. I'm trying to figure out why the latest ktor doesnt have have support for the iOS client:
Could not resolve io.ktor:ktor-client-ios:1.2.0-alpha-2
there's a thread on ktor about it, but it hints to just dropping support for iOS. Also, my versions of kotlin and gradle seem to be out of date. What are the latest ones you're using?
Copy code
kotlin.code.style=official

# gradle version
gradle_version = 3.2.1

# kotlin versions
kotlin_version = 1.3.21
kotlin_native_version = 1.3.11
ktor_version = 1.2.0-alpha-2
kotlinx_coroutines_version = 1.1.0
thanks
o
@e5l ^^
k
Probably newer examples out there, but here’s ktor 1.1.2 in our app: https://github.com/touchlab/DroidconKotlin
r
Do you have the following in your
settings.gradle
?
enableFeaturePreview("GRADLE_METADATA")
Do you have
<http://dl.bintray.com/kotlin/ktor|dl.bintray.com/kotlin/ktor>
in your
repositories
section (also check that it's project's, not buildscript's one)?
t
yes to settings.gradle:
Copy code
pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "kotlin-multiplatform") {
                useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
            }
        }
    }
}
rootProject.name = 'PossibleDuplicates'


enableFeaturePreview('GRADLE_METADATA')
yes to bintray:
Copy code
repositories {
    mavenCentral()
    maven { url '<https://plugins.gradle.org/m2/>' }
    maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }
    maven { url "<https://kotlin.bintray.com/ktor>" }
    maven { url "<https://dl.bintray.com/kotlin/kotlinx/>" }
    maven { url "<https://kotlin.bintray.com/kotlinx>" }
    jcenter()
    google()
}
@kpgalligan when I tried your app settings, coroutines are not visible on iOS. i think i have a mess in here
r
And just for the sake of completeness, could you please show relevant part of
build.gradle
with configuration of common and native sourceSet dependencies? Everything you've shown seems correct.
k
Yeah, it can be tricky to set up. I’d be more help but I’m mid-crisis at the moment…
t
full build.gradle:
Copy code
buildscript {
    repositories {
        mavenLocal()
        maven { url '<https://plugins.gradle.org/m2/>' }
        maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }
        jcenter()
        google()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

repositories {
    mavenCentral()
    maven { url '<https://plugins.gradle.org/m2/>' }
    maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }
    maven { url "<https://kotlin.bintray.com/ktor>" }
    maven { url "<https://dl.bintray.com/kotlin/kotlinx/>" }
    maven { url "<https://kotlin.bintray.com/kotlinx>" }
    jcenter()
    google()
}
group 'com.mysample.newproject'
version '0.0.2'

apply plugin: 'maven-publish'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'


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

        fromPreset(iOSTarget, 'ios') {
            compilations.main.outputKinds('FRAMEWORK')

//            compilations.main.embedBitcode BitcodeEmbeddingMode.BITCODE // for release binaries
//            // or BitcodeEmbeddingMode.MARKER for debug binaries
        }

        fromPreset(presets.jvm, 'android')

    }

    sourceSets {
        commonMain {
            dependencies {
                api 'org.jetbrains.kotlin:kotlin-stdlib-common'
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        androidMain {
            dependencies {
                api 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation("io.ktor:ktor-client-android:1.1.1")
            }
        }
        androidTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            }
        }
        iosMain {
            dependencies {
                api 'org.jetbrains.kotlin:kotlin-stdlib'
            }
        }
        iosTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
            }
        }
    }
}

configurations {
    compileClasspath
}

dependencies {
    commonMainImplementation ("io.ktor:ktor-client-core:$ktor_version")
    androidMainImplementation("io.ktor:ktor-client-android:$ktor_version")
    iosMainImplementation("io.ktor:ktor-client-ios:$ktor_version")

    commonMainImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    commonMainImplementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.10.0"
}

task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'

    inputs.property "mode", mode
    dependsOn kotlin.targets.ios.compilations.main.linkTaskName("FRAMEWORK", mode)

    from { kotlin.targets.ios.compilations.main.getBinary("FRAMEWORK", mode).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
no worries Kevin. Thanks for your help anyways
r
iOS doesn't need
kotlin-stdlib
, but I'm not sure it would cause your issue here. I'd try to move everything from root
dependencies
block to relevant sourceSet dependencies, but the way it is also shouldn't lead to any troubles to my knowledge.
Sorry, I'm out of ideas :(
t
that build.gradle works, with this `gradle.properties`:
Copy code
kotlin.code.style=official

# gradle version
gradle_version = 3.2.1

# kotlin versions
kotlin_version = 1.3.11
kotlin_native_version = 1.3.11
ktor_version = 1.1.1
kotlinx_coroutines_version = 1.1.0
however, the serialization with kotlinx fails. here's my model:
Copy code
package model

import kotlinx.serialization.Serializable

@Serializable
data class PossibleDuplicate(val v: String)
here's the console, when I try to build:
Copy code
src/commonMain/kotlin/model/PossibleDuplicate.kt:3:16: error: unresolved reference: serialization
import kotlinx.serialization.Serializable
               ^
r
Oh, that's an interesting twist. I don't recall when they switched Gradle version, which one did you try to use with latest versions in
gradle.properties
?
t
i haven't changed the
gradle_version
from
3.2.1
in any case
r
3.2.1 is most probably Android Gradle Plugin version, not Gradle itself. Latter could be found at
gradle/wrapper/gradle-wrapper.properties
(or something along those lines, not near my machine right now).
t
gradle-wrapper.properties:
Copy code
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
r
4.7 uses old metadata version, please try to upgradle to 4.10. Ktor 1.1.2 seems to be built for it, but I'm not sure about fresher ones.
t
ok, so change gradle to 4.10. What should these ones be then?
Copy code
# gradle version
gradle_version = 3.2.1

# kotlin versions
kotlin_version = 1.3.11
kotlin_native_version = 1.3.11
ktor_version = 1.1.1
kotlinx_coroutines_version = 1.1.0
i dont think im using
kotlin_native_version
though
r
Ktor 1.1.2 mentions the following: Kotlin 1.3.20, kotlinx.coroutines 1.1.1, kotlinx.serialization 0.10.0 I'd try this known configuration, and as soon as it works try to go for fresher ones if needed. kotlin_native_version was most probably used for separate native plugin, kotlin-multiplatform has it embedded, so you can safely remove this line.
t
my versions are now this:
Copy code
# gradle version
gradle_version = 3.2.1

# kotlin versions
kotlin_version = 1.3.20
kotlin_native_version = 1.3.11
ktor_version = 1.1.2
kotlinx_coroutines_version = 1.1.1
kotlinx_serialization_runtime = 0.10.0
it is still complaining about the serialization:
Copy code
src/commonMain/kotlin/model/DuplicatesResponse.kt:3:16: error: unresolved reference: serialization
import kotlinx.serialization.Serializable
               ^
by the way, thanks a lot for your help
oh, i got it working!
1
🎉 1
r
Sorry, unexpectedly gone offline. Congrats on working configuration! 💪
b
@toomanyeduardos Could you post how you got it working please?
2
t
sure
the issue was with having this line, in the main
dependencies
block:
Copy code
commonMainImplementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.10.0"
so, now in my
sourceSets
group, I have this for common main:
Copy code
commonMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlinx_serialization_runtime"
            }
        }
this for android main:
Copy code
androidMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlinx_serialization_runtime"
            }
        }
and this for iOS main:
Copy code
iosMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$kotlinx_serialization_runtime"
            }
        }
so that makes my
build.gradle
file be like this:
Copy code
buildscript {
    repositories {
        mavenLocal()
        maven { url '<https://plugins.gradle.org/m2/>' }
        maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }
        jcenter()
        google()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

repositories {
    mavenCentral()
    maven { url '<https://plugins.gradle.org/m2/>' }
    maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }
    maven { url "<https://kotlin.bintray.com/ktor>" }
    maven { url "<https://dl.bintray.com/kotlin/kotlinx/>" }
    maven { url "<https://kotlin.bintray.com/kotlinx>" }
    jcenter()
    google()
}
group 'com.ancestry.possibleduplicates'
version '0.0.2'

apply plugin: 'maven-publish'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'


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

        fromPreset(iOSTarget, 'ios') {
            compilations.main.outputKinds('FRAMEWORK')

//            compilations.main.embedBitcode BitcodeEmbeddingMode.BITCODE // for release binaries
//            // or BitcodeEmbeddingMode.MARKER for debug binaries
        }

        fromPreset(presets.jvm, 'android')

    }

    sourceSets {
        commonMain {
            dependencies {
                api 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlinx_serialization_runtime"
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        androidMain {
            dependencies {
                api 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation("io.ktor:ktor-client-android:$ktor_version")
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlinx_serialization_runtime"
            }
        }
        androidTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            }
        }
        iosMain {
            dependencies {
                api 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$kotlinx_serialization_runtime"
            }
        }
        iosTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
            }
        }
    }
}

configurations {
    compileClasspath
}

dependencies {
    commonMainImplementation ("io.ktor:ktor-client-core:$ktor_version")
    androidMainImplementation("io.ktor:ktor-client-android:$ktor_version")
    iosMainImplementation("io.ktor:ktor-client-ios:$ktor_version")

    commonMainImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
let me know if you have any questions (which i doubt I can answer, but we'll see...)
j
So, perhaps you’re recent battle with this could help me figure out a similar issue. I’m working on a shared library and I’m looking to introduce Ktor to it. My project structure lives inside of an android app like so:
Copy code
MainFolder/
-.gradle/
-app/
-gradle/
-SharedCode/
    -.gradle/
    -build/
    -gradle/
    -src/
    -build.gradle
    -settings.gradle
-build.gradle
-settings.gradle
Inside of the
SharedCode
directory lies all of the code that gets compiled to frameworks and libraries. I’m having trouble figuring out how to use gradle for Ktor.
Copy code
SharedCode - build.gradle

plugins {
    id 'kotlin-multiplatform' version '1.3.10'
}
repositories {
    mavenCentral()

}
group 'com.example'
version '0.0.1'

apply plugin: 'maven-publish'


kotlin {
    
    targets {
        fromPreset(presets.jvm, 'jvm')
        fromPreset(presets.js, 'js')
        // For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
        // For Linux, preset should be changed to e.g. presets.linuxX64
        // For MacOS, preset should be changed to e.g. presets.macosX64
        fromPreset(presets.macosX64, 'macos')

        fromPreset(System.getenv('SDK_NAME')?.startsWith("iphoneos") ? presets.iosArm64 : presets.iosX64, 'ios') {
            compilations.main.outputKinds('FRAMEWORK')
            compilations.main.extraOpts '-module-name', 'Core'
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
//                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlinx_serialization_runtime"
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        jvmMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
//                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlinx_serialization_runtime"
            }
        }
        jvmTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            }
        }
        jsMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
            }
        }
        jsTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-js'
            }
        }
        macosMain {
        }
        macosTest {
        }
        iosMain{
//            implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$kotlinx_serialization_runtime"
        }
    }
}

// From here: <http://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html>
// Apache 2 License

task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'

    inputs.property "mode", mode
    dependsOn kotlin.targets.ios.compilations.main.linkTaskName("FRAMEWORK", mode)

    from { kotlin.targets.ios.compilations.main.getBinary("FRAMEWORK", mode).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
Copy code
SharedCode settings.gradle

//ext.ktor_version = '1.1.2'
//ext.kotlinx_coroutines_version = '1.1.1'
//ext.kotlinx_serialization_runtime = '0.10.0'
//ext.serialization_version = '0.10.0'

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "kotlin-multiplatform") {
                useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
            }
        }
    }

    repositories {
        maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }

        mavenCentral()

        maven { url '<https://plugins.gradle.org/m2/>' }
//        jcenter()
//        google()
//
//        maven { url "<https://kotlin.bintray.com/ktor>" }
//        maven { url "<https://dl.bintray.com/kotlin/kotlinx/>" }
//        maven { url "<https://kotlin.bintray.com/kotlinx>" }
    }

}

rootProject.name = 'SharedCode'
enableFeaturePreview('GRADLE_METADATA')
t
you could try copying my gradle file, as that has ktor working
j
I ended up figuring it out. My main issue was that I mistyped the version number the error messaging didn’t help me figure that out. Thanks though.
👍 1
😳 1