Skip to content

ClassNotFoundException for UserAgent plugin with R8 enabled (Ktor 3.3.2) #5240

@tristanjung1006

Description

@tristanjung1006

Ktor Version

3.3.2

Ktor Engine Used

ktor-client-android

Kotlin Version

2.2.21

Operating System

Android 13 (also affects other upper API level Android versions)

Describe the Bug

When a third-party SDK (Moloco SDK) initializes an HttpClient with the UserAgent plugin,
the app crashes with ClassNotFoundException on release builds with R8 enabled.

This issue affected 27 users over 1 day, occurring on Samsung Galaxy devices.

Stack Trace

Fatal Exception: java.lang.NoClassDefFoundError
Failed resolution of: Lio/ktor/client/plugins/UserAgent;

com.moloco.sdk.internal.http.a$a.a (SourceFile:1)
com.moloco.sdk.internal.http.a$a.invoke (SourceFile:1)
io.ktor.client.HttpClientKt.HttpClient (HttpClient.kt:648)
io.ktor.client.HttpClientJvmKt.HttpClient (HttpClientJvm.kt:25)
...

Caused by java.lang.ClassNotFoundException
io.ktor.client.plugins.UserAgent

Expected Behavior

The UserAgent class should be available at runtime and not stripped by R8.

Steps to Reproduce

  1. Add Ktor 3.3.2 dependencies to an Android project
  2. Enable R8 minification (minifyEnabled = true)
  3. Use a library (e.g., Moloco SDK) that initializes HttpClient with UserAgent plugin
  4. Build and run the release APK
  5. App crashes when HttpClient is initialized

Workaround

Downgrading to Ktor 2.3.12 resolves the issue.

Additional Context

  • I saw that Ktor 3.2.2 included an "Android D8 compatibility fix"
  • However, this issue persists in 3.3.2
  • Our ProGuard rules already include -keep class io.ktor.client.plugins.** { *; }
  • The issue may be related to how R8 handles Ktor's internal class references

Environment Details

// gradle/libs.versions.toml
ktor = "3.3.2"

// app/build.gradle.kts
implementation("io.ktor:ktor-client-android:3.3.2")
implementation("io.ktor:ktor-client-content-negotiation:3.3.2")
implementation("io.ktor:ktor-client-logging:3.3.2")
implementation("io.ktor:ktor-client-auth:3.3.2")

Questions

1. Is this a known issue with Ktor 3.3.2?
2. Are there additional ProGuard/R8 rules needed for the UserAgent plugin?
3. Should ktor-client-core explicitly expose the UserAgent class for R8 compatibility?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions