I am trying to use this <https://khttp.readthedocs...
# kotlin-native
k
I am trying to use this https://khttp.readthedocs.io/en/latest/ library instead of Java HTTP. But it seems to work only for the Android target in a multiplatform scenario. It’s not even detecting on iOS or Common targets, although I have mentioned the dependency tag and repository URL in the respective gradle files! Any ideas?
k
Github is giving me issues so it’s hard to nav the project, but that’s not multiplatform. Only the JVM (android) would be available
k
hmm, so if I want to write common network library to work for both the platforms, is there a way?
Or isn’t that a multiplatform scenario?
k
If you want to use a different library, it has to be written for multiplatform
Is sharing networking code a multiplatform thing? Absolutely, but the library has to work.
k
Yeah, I got your point. Thank you. So, are you saying if I write the common network calls using ktor in my commonMain, it would work for both iOS and Android?
k
Yes
k
Wow, that’s cool. Thank you so much
k
Would send some code, but apparently github is down:
k
Oh that would be really helpful. I will ping you sometime later to check back, if you don’t mind.
l
GitHub is up
👏 1
k
Hey Kevin @kpgalligan, can you share a sample project. That would be helpful.
k
Here’
Ugh…
It kind of works like you’d expect. client makes calls
Copy code
override suspend fun getSpeakersJson(): String = client.get<String> {
        sessionize(“/api/v2/$SPONSOR_INSTANCE_ID/view/speakers”)
    }
k
Thank you Kevin, much appreciated!
I tried writing the common code under the commonMain heirarchy. Although imports are working fine, HttpClient() is not being detected! Weird.
message has been deleted
And gradle build is working fine too, but I am unable to access the same in Xcode. I am assuming that’s because compilation of HttpClient() isn’t done appropriately!
k
I get the unresolved in the editor too, but it works
message has been deleted
k
Wow, that’s a relief.
Thank you!
k
👍
k
And the debugging video really helped. Is that you in there?
k
Yep
k
message has been deleted
I added appropriate dependency in my sharedLib gradle
still it’s not compiling
k
Send the whole build.gradle
k
k
Well, I don’t know what the issue is, but I’d say if you took my code as the sample to start from, you’re missing dependencies
‘ios’ : “io.ktorktor client ios${versions.ktor}“, ‘iosCore’ : “io.ktorktor client core native${versions.ktor}“,
implementation deps.ktor.ios implementation deps.ktor.iosCore
message has been deleted
👍 1
k
So, shouldn’t any of these be in SharedLib gradle at all? But if we are going to use the code in SharedLib for just the common codebase of the whole project, why are we mentioning all of these in the main build.gradle? So what goes in the SharedLib gradle file exactly?
k
I define the strings in the main gradle file, but those are just strings. The actual adding to the dependencies happens in the SharedLib gradle
So, it could look like this
k
Yeah right, made the changes like that. But still the client engine for Ios is not getting detected by the IDE.
By the way I have written this piece of code in the commonMain
not in iosMain
does that make a difference
k
What is
Ios
? I’m 99% sure that shouldn’t be in common, unless it has nothing to do with iOS the platform.
The engine mentioned here
If that snippet shouldn’t be in common, I am wondering I will have to write separate logic for both the platforms -Android and iOS. But what I am trying is use the common codebase to make a network call from the commonMain module.
k
At worst, you’d write an expect/actual fun to serve as a factory. However, assuming you don’t care what it’s using on the platform, you’re calling this
Again, as per sample I sent. No platform-specific config required
k
But when I was using without platform specific engine, like you showed above, Xcode started throwing me this
message has been deleted
k
message has been deleted
I mean, I know it’s confusing at first, but again, you’re missing a dependency.
k
oh really, let me go through my files again. Thanks for noticing that. I saw this error and thought I double checked. Let me check again.
I was trying to execute the DroidCon iOS app and ran into issues like “lib” not found. That’s weird considering I have installed the pods and lib is available as a development pod and linked.
message has been deleted
message has been deleted
k
Definitely don’t try to run
pod install
on Droidcon. There’s an issue with that. It’s a big install. Plan to get back to that later
k
Okay cool. Thanks.
k
Droidcon is delicate. I think we need to rebuild the config from the ground up. You can clone it and run it as-is, but running pods blows it up. That shut down our team for a day or so
k
Wow, that sounds like a lot. Seemed kinda heavy when I ran `Pod install’
Yeah I will clone and run then. Thank you.
k
Cocoapods does a lot to the xcode projects, and Apple does not care at all if they make breaking changes, so there’s kind of an ongoing battle
k
Ha ha, that’s true. Can’t agree more!
message has been deleted
message has been deleted
message has been deleted
Doesn’t throw any error either and doesn’t give a success response for the network call I am making
k
I would guess it’s related to GlobalScope, but don’t know
k
Yeah, I changed it, but still no luck. Got to explore more I guess. But the Android counterpart works perfectly fine when invoked from the common code.
k
The android side probably doesn’t care what thread you’re coming back on. The iOS side definitely does. For ktor, you need to call from the main thread and definitely return on the main thread
That may not be the issue, but I’d start there
k
Figured that out. I had to write a custom dispatcher for iOS. And tada! Phew 😅 .. I think that should be part of the documentation somewhere. I will post somewhere like Medium if that will help others.