I’ve started working with ktor HttpClient and ran ...
# multiplatform
n
I’ve started working with ktor HttpClient and ran into a couple of issues. First, is that my IDE doesn’t recognize
HttpClient
and
runBlocking
, while the code itself compiles perfectly fine. What could be a reason for that?
g
Looks as IDE issue, try restart + invalidate caches
m
Please post your
build.gradle
n
Copy code
plugins {
    id 'com.android.library'
    id 'kotlin-multiplatform'
    id 'maven-publish'
}

repositories {
    google()
    jcenter()
    mavenCentral()
    maven { url '<http://archiva.example.com:9080/repository/internal>' }
}

group 'com.example.multiplatform'
version '0.4.1'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 21
    }
    buildTypes {
        release {
            minifyEnabled true
        }
    }
}

ext.ktor_version = '1.0.+'

kotlin {
    targets {
        final def iosTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") ? presets.iosArm64 : presets.iosX64
        fromPreset(iosTarget, 'ios') {
            compilations.main.outputKinds('FRAMEWORK')
        }
        fromPreset(presets.android, 'android')
    }
    sourceSets {
        all {
            languageSettings {
                languageVersion = '1.3'
                useExperimentalAnnotation 'kotlin.ExperimentalUnsignedTypes'
            }
        }
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation "io.ktor:ktor-client:$ktor_version"
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        iosMain
        iosTest
    }
}

dependencies {
    androidMainImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    androidMainImplementation "io.ktor:ktor-client-android:$ktor_version"

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'org.jetbrains.kotlin:kotlin-test'
    androidTestImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
    androidTestImplementation "io.ktor:ktor-client-android:$ktor_version"

    iosMainImplementation "io.ktor:ktor-client-ios:$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'

    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

task iosTest {
    def device = project.findProperty("iosDevice")?.toString() ?: "iPhone 8"
    dependsOn 'linkTestDebugExecutableIos'
    group = JavaBasePlugin.VERIFICATION_GROUP
    description = "Runs tests for target 'ios' on an iOS simulator"

    doLast {
        def binary = kotlin.targets.ios.compilations.test.getBinary('EXECUTABLE', 'DEBUG')
        exec {
            commandLine 'xcrun', 'simctl', 'spawn', device, binary.absolutePath
        }
    }
}

tasks.check.dependsOn iosTest
If anyone can spot an error that would be awesome.
m
Well, I don't know, looks sane. Maybe
ext.ktor_version = '1.0.+'
- better use exact version.
n
I tried that and then invalidated cache and restarted. Same thing 😞
g
I don’t think that it somehow related to build.gradle
Some IDE problem
and most probably related to Gradle Metadata
n
Yes, actually when I restart it it throws an exception.
g
try
io.ktor:ktor-client-jvm
n
Maybe that’s the reason, ktor-client-jvm hasn’t been updated for a long time. The latest version is 0.9.0-alpha.
m
And maybe it depend on your Gradle version... But if gradle build passed it should be fine. Strange.
g
It may be just a problem of Idea with gradle metadata dependencies
runBlocking is actually strange
m
As experiment - if leave only one target - ios or android - it works?
n
So I’ve thrown out the
android
target, invalidated cache and restarted. No change.
It looks like IntelliJ doesn’t understand the structure of the project completely. Although Gradle itself can build the project just fine. That’s a minor problem to me for now.
You won’t believe, but I started working on the project further, and somehow it now recognizes HttpClient, just out of the blue, I didn’t change anything, just was typing in more code.
Aha, now I get it. If I use HttpClient version 1.0.0, then it’s fine. With 1.0.1 it’s red. Problem solved, sort of.