Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Crash with Xiaomi on create Ktorfit.Builder #372

Closed
princeparadoxes opened this issue Jul 10, 2023 · 0 comments · Fixed by #375
Closed

[Bug]: Crash with Xiaomi on create Ktorfit.Builder #372

princeparadoxes opened this issue Jul 10, 2023 · 0 comments · Fixed by #375
Labels
bug Something isn't working

Comments

@princeparadoxes
Copy link
Contributor

Ktorfit version

1.4.2

What happened and how can we reproduce this issue?

Hello. When I invoke Ktorfit.Builder() I get a crash on Xiaomi.

Caused by java.lang.IllegalStateException: Failed to find HTTP client engine implementation in the classpath: consider adding client engine dependency. See https://ktor.io/docs/http-client-engines.html
       at io.ktor.client.HttpClientJvmKt.<clinit>(HttpClientJvm.kt:43)
       at io.ktor.client.HttpClientJvmKt.b(HttpClientJvm.kt:1)
       at de.jensklingenberg.ktorfit.Ktorfit$Builder.<init>(Ktorfit.kt:50)
       at ru.cian.base.kmm.impl.network.BaseNetworkModuleKt$baseNetworkModule$1$2.invoke(BaseNetworkModule.kt:53)
       .....

What did you expect to happen?

No crash)

Is there anything else we need to know about?

I create Ktorfit.Builder with getting a HttpClient from Koin. Like this:

Ktorfit.Builder()
      .baseUrl(baseUrl)
      .httpClient(get<HttpClient>())
      .build()

In the release app users get a crash:

Caused by java.lang.IllegalStateException: Failed to find HTTP client engine implementation in the classpath: consider adding client engine dependency. See https://ktor.io/docs/http-client-engines.html
       at io.ktor.client.HttpClientJvmKt.<clinit>(HttpClientJvm.kt:43)
       at io.ktor.client.HttpClientJvmKt.b(HttpClientJvm.kt:1)
       at de.jensklingenberg.ktorfit.Ktorfit$Builder.<init>(Ktorfit.kt:50)
       at ru.cian.base.kmm.impl.network.BaseNetworkModuleKt$baseNetworkModule$1$2.invoke(BaseNetworkModule.kt:53)
       .....

But I always create a HttpClient with an engine

HttpClient(OkHttp) {
        followRedirects = false
        extensions.forEach { this@HttpClient.it() }
 }

This crash only appears on Xiaomi devices with Android 12 and 13. Other devices is OK.
Screenshot 2023-07-10 at 12 58 38

I found a cause. When Ktorfit.Builder() is called, one of its fields calls the HttpClient() constructor with no arguments.

public class Builder {
        private var _baseUrl: String = ""
        private var _httpClient = HttpClient()
       ....

In HttpClientKvm this crash appears when Ktor can't find any HttpClientEngineContainer in ClassLoader.

private val engines: List<HttpClientEngineContainer> = HttpClientEngineContainer::class.java.let {
    ServiceLoader.load(it, it.classLoader).toList()
}

private val FACTORY = engines.firstOrNull()?.factory ?: error(
    "Failed to find HTTP client engine implementation in the classpath: consider adding client engine dependency. " +
        "See https://ktor.io/docs/http-client-engines.html"
)

My hypothesis is that Xiaomi has a bug in custom ClassLoader. It cannot load OkHttp until the Koin method to create the HttpClient is called.
In my app now, I create an HttpClient before invoking Ktorfit.Builder(). Like this:

val httpClient = get<HttpClient>()
Ktorfit.Builder()
    .baseUrl(baseUrl)
    .httpClient(httpClient)
    .build()

I think, need to remove the HttpClient creation in the Ktorfit.Builder field as it causes a crash. In addition, an extra HttpClient instance will not be created if I use a custom HttpClient

@princeparadoxes princeparadoxes added the bug Something isn't working label Jul 10, 2023
princeparadoxes pushed a commit to princeparadoxes/Ktorfit that referenced this issue Jul 10, 2023
@Foso Foso closed this as completed in #375 Jul 12, 2023
Foso pushed a commit that referenced this issue Jul 12, 2023
* Fix #372 Crash with Xiaomi on create Ktorfit.Builder

* Update CHANGELOG.md

---------

Co-authored-by: Перевалов Данил <d.perevalov@cian.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant