ktorio / ktorio/ktor

ClassNotFoundException for UserAgent plugin with R8 enabled (Ktor 3.3.2)

Open
#5,240 1 comment 0 reactions 0 assignees View on GitHub
wait for response
Dominant language
Kotlin
Stars
14.5k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
49

Description

## 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

```kotlin
// 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?

Contributor guide

Open the contributing guide

Research direction

Start with app/build.gradle.kts and gradle/libs.versions.toml, then reproduce the failure by building the minified Android release APK and initializing HttpClient with UserAgent. Trace the UserAgent reference from HttpClient.kt:648 and compare it with the existing R8 keep rule. Done means the release APK initializes without ClassNotFoundException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
build-system, mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.