Hey guys, I am using ktor in a KMP Project (using ...
# ktor
a
Hey guys, I am using ktor in a KMP Project (using Compose Multiplatform), running the app on an Android Device. I am trying to connect to a local IP using http, because the local backend does support https. Even though I have provided a network security config, which is the usual way to handle this (see thread) I am getting the following:
Copy code
java.net.UnknownServiceException: CLEARTEXT communication to 10.0.2.2 not permitted by network security policy
Any help would be appreciated.
My network security config is as follows:
Copy code
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>
This should work, but it seems like it does not get picked up by ktor in this case and I am lost as to why. I am using OkHttp as engine on Android.
I guess the problem is you are putting an IP address and not a domain name.
a
It's a local server, 10.0.2.2 is the equivalent of 127.0.0.1 for Android Emulators when trying to reach the host computer. So there wouldn't really be any domain I could add here.
c
Sure, so you cannot use the network config for that. I think there is a
clearTextTraffic
. attribute on the
<application>
that you can override in a debug builds manifest file.
a
I added the
android:usesCleartextTraffic="true"
to the application tag in the manifest, building only debug but the error persists
c
I have
Copy code
<network-security-config xmlns:tools="<http://schemas.android.com/tools>">
    <base-config
        cleartextTrafficPermitted="true"
        tools:ignore="InsecureBaseConfiguration" />
</network-security-config>
in my
app/src/debug/AndroidManifest.xml
referenced and this works
a
I couldn't get it to work with these configs, there is no significant difference between yours and mine, except for the
tools:
tag which should be irrelevant. I have made it work by installing a https proxy (npm local-ssl-proxy), proxying a https endpoint to the http endpoint internally and by installing a trust all certs and domains trust manager on the OkHttp client side. This works well enough for my usecase, since ktor and the android system now thinks I am accessing a https endpoint. I am not sure why this does not work with the network security config, nothing obvious comes to mind.
👍🏼 1
👍 1
v
@Alex dumb question, but do you actually add the network security config to your manifest? (asking just because that's something Ive forgotten to do once or twice)
a
Yes I did, this would've been a great fix if I had forgotten that, unfortunately I added it there 😉
1