'Certificate Pinning in ktor kmm on Android devices below android 7

After testing network_security_config file with android 28, I see the certificate pinning working when using ktor kmm (using OkHttp) However, in devices below android 7, Is there any way to setup certificate pinning.

I tried these codes, but it seems not working and how can I setup certifcate pinning in ios with kmm ?

actual class Platform actual constructor() {
    actual val clientEngine: HttpClientEngineFactory<HttpClientEngineConfig>
        get() = OkHttp.apply {
            config {
                val certificatePinner = CertificatePinner.Builder()
                    .add(
                        "test.com.co",
                        "sha256/xxxx"
                    ).build()
                preconfigured = OkHttpClient.Builder()
                    .certificatePinner(certificatePinner)
                    .build()
            }
        }
}

fun testHttpClient(
    config: EnvConfiguration,
    checker: NetworkChecker,
    header: HeaderProvider
): HttpClient =
    HttpClient(Platform().clientEngine) {
        install(JsonFeature) {
            serializer = KotlinxSerializer(
                kotlinx.serialization.json.Json {
                    prettyPrint = true
                }
            )
        }
    ...
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source