Foso / Ktorfit

HTTP client generator / KSP plugin for Kotlin Multiplatform (Android, iOS, Js, Jvm, Native, WasmJs)) using KSP and Ktor clients inspired by Retrofit https://foso.github.io/Ktorfit
https://foso.github.io/Ktorfit
Apache License 2.0
1.6k stars 43 forks source link

[Android] R8 - Missing class io.ktor.client.network... #719

Open Monabr opened 3 weeks ago

Monabr commented 3 weeks ago

Ktorfit version

2.1.0

What happened and how can we reproduce this issue?

isDebuggable = false
isMinifyEnabled = true

And try to build release android project.

The build fails with big message:

Missing class io.ktor.client.network.sockets.SocketTimeoutException (referenced from: java.lang.Throwable io.ktor.client.engine.cio.ExceptionsKt.mapToKtor(java.lang.Throwable, io.ktor.client.request.HttpRequestData))
Missing class io.ktor.client.network.sockets.TimeoutExceptionsCommonKt (referenced from: java.lang.Object io.ktor.client.engine.cio.Endpoint.makeDedicatedRequest(io.ktor.client.request.HttpRequestData, kotlin.coroutines.CoroutineContext, kotlin.coroutines.Continuation))
Missing class io.ktor.client.plugins.HttpTimeout$HttpTimeoutCapabilityConfiguration (referenced from: kotlin.Pair io.ktor.client.engine.cio.Endpoint.retrieveTimeouts(io.ktor.client.request.HttpRequestData) and 1 other context)
Missing class io.ktor.client.plugins.HttpTimeout$Plugin (referenced from: kotlin.Pair io.ktor.client.engine.cio.Endpoint.retrieveTimeouts(io.ktor.client.request.HttpRequestData))
Missing class io.ktor.client.plugins.HttpTimeout (referenced from: kotlin.Pair io.ktor.client.engine.cio.Endpoint.retrieveTimeouts(io.ktor.client.request.HttpRequestData))
Missing class io.ktor.util.InternalAPI (referenced from: io.ktor.client.engine.cio.CIOEngineContainer and 1 other context)
Missing class io.ktor.util.KtorDsl (referenced from: io.ktor.client.plugins.json.JsonPlugin$Config)
Missing class io.ktor.utils.io.ByteReadChannelJVMKt (referenced from: java.lang.Object io.ktor.client.engine.cio.UtilsKt$withoutClosePropagation$2.invokeSuspend(java.lang.Object) and 3 other contexts)
Missing class io.ktor.utils.io.CoroutinesKt (referenced from: java.lang.Object io.ktor.client.engine.cio.UtilsKt$readResponse$2.invokeSuspend(java.lang.Object) and 9 other contexts)
Missing class io.ktor.utils.io.DelimitedKt (referenced from: java.lang.Object io.ktor.http.cio.MultipartKt$parseMultipart$1.invokeSuspend(java.lang.Object) and 1 other context)
Missing class io.ktor.utils.io.LookAheadSession (referenced from: void io.ktor.http.cio.MultipartKt$skipDelimiterOrEof$3.invoke(io.ktor.utils.io.LookAheadSession) and 5 other contexts)
Missing class io.ktor.utils.io.ReadSessionKt (referenced from: java.lang.Object io.ktor.http.cio.ChunkedTransferEncodingKt.encodeChunked(io.ktor.utils.io.ByteWriteChannel, io.ktor.utils.io.ByteReadChannel, kotlin.coroutines.Continuation))
Missing class io.ktor.utils.io.bits.Memory (referenced from: java.lang.Object io.ktor.http.cio.MultipartInput$fill$1.invokeSuspend(java.lang.Object) and 1 other context)
Missing class io.ktor.utils.io.core.Buffer$Companion (referenced from: java.lang.Object io.ktor.http.cio.ChunkedTransferEncodingKt.encodeChunked(io.ktor.utils.io.ByteWriteChannel, io.ktor.utils.io.ByteReadChannel, kotlin.coroutines.Continuation))
Missing class io.ktor.utils.io.core.Buffer (referenced from: java.lang.Object io.ktor.http.cio.ChunkedTransferEncodingKt.encodeChunked(io.ktor.utils.io.ByteWriteChannel, io.ktor.utils.io.ByteReadChannel, kotlin.coroutines.Continuation))
Missing class io.ktor.utils.io.core.ByteBuffersKt (referenced from: java.lang.Object io.ktor.network.sockets.DatagramSendChannel.trySend-JP2dKIU(io.ktor.network.sockets.Datagram) and 3 other contexts)
Missing class io.ktor.utils.io.core.BytePacketBuilder (referenced from: io.ktor.utils.io.core.BytePacketBuilder io.ktor.http.cio.MultipartKt$parsePreambleImpl$2.$output and 87 other contexts)
Missing class io.ktor.utils.io.core.ByteReadPacket$Companion (referenced from: void io.ktor.network.tls.TLSHandshake.<init>() and 1 other context)
Missing class io.ktor.utils.io.core.ByteReadPacket (referenced from: io.ktor.utils.io.core.ByteReadPacket io.ktor.http.cio.MultipartEvent$Epilogue.body and 83 other contexts)
Missing class io.ktor.utils.io.core.CloseableJVMKt (referenced from: java.lang.Object io.ktor.client.engine.cio.UtilsKt$readResponse$2.invokeSuspend(java.lang.Object) and 2 other contexts)
Missing class io.ktor.utils.io.core.Input (referenced from: io.ktor.utils.io.core.Input io.ktor.http.cio.CIOMultipartDataBase$partToData$3.$input and 22 other contexts)
Missing class io.ktor.utils.io.core.InputArraysKt (referenced from: java.util.List io.ktor.network.tls.ParserKt.readTLSCertificate(io.ktor.utils.io.core.ByteReadPacket) and 1 other context)
Missing class io.ktor.utils.io.core.InputPrimitivesKt (referenced from: io.ktor.network.tls.extensions.NamedCurve io.ktor.network.tls.ParserKt.readCurveParams(io.ktor.utils.io.core.ByteReadPacket) and 8 other contexts)
Missing class io.ktor.utils.io.core.Output (referenced from: java.lang.Object io.ktor.http.cio.MultipartKt$parsePreambleImpl$2.invokeSuspend(java.lang.Object) and 27 other contexts)
Missing class io.ktor.utils.io.core.OutputPrimitivesKt (referenced from: io.ktor.utils.io.core.ByteReadPacket io.ktor.network.tls.RenderKt.buildECCurvesExtension(java.util.List) and 10 other contexts)
Missing class io.ktor.utils.io.core.PreviewKt (referenced from: byte[] io.ktor.network.tls.Digest.doHash-impl(io.ktor.utils.io.core.BytePacketBuilder, java.lang.String) and 1 other context)
Missing class io.ktor.utils.io.core.internal.ChunkBuffer (referenced from: void io.ktor.http.cio.MultipartInput.<init>(java.nio.ByteBuffer, io.ktor.utils.io.ByteReadChannel))
Missing class io.ktor.utils.io.streams.InputKt (referenced from: io.ktor.utils.io.core.Input io.ktor.http.cio.CIOMultipartDataBase$withTempFile$lazyInput$1.invoke() and 1 other context)
Missing class java.lang.management.ManagementFactory (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())
Missing class java.lang.management.RuntimeMXBean (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())

What did you expect to happen?

Project builds successfully.

Is there anything else we need to know about?

I tried with

            implementation(libs.ktorfit.lib)
            implementation(libs.ktor.client.serialization)
            implementation(libs.ktor.client.content.negotiation)
            implementation(libs.ktor.serialization.kotlinx.json)

and

            implementation(libs.ktorfit.lib)
            implementation(libs.ktor.client.serialization)
            implementation(libs.ktor.client.content.negotiation)
            implementation(libs.ktor.serialization.kotlinx.json)
            implementation(libs.ktor.client.core) <------------------------- Added this            

and still getting this error.

Monabr commented 1 week ago

@Foso Hello. Are there any updates? This is a blocking issue for me.

Foso commented 1 week ago

Hi @Monabr for me its look all missing classes are related to the Ktor Websockets plugin and not Ktorfit.

Try adding this to your proguard/R8 file:

-keep class io.ktor.client.network.sockets. { *; } -keep class io.ktor.client.plugins.* { ; } -keep class io.ktor.util. { *; } -keep class io.ktor.utils.io.* { ; } -keep class java.lang.management.* { ; }

Monabr commented 1 week ago

Hi @Monabr for me its look all missing classes are related to the Ktor Websockets plugin and not Ktorfit.

Try adding this to your proguard/R8 file:

-keep class io.ktor.client.network.sockets.* { ; } -keep class io.ktor.client.plugins. { ; } -keep class io.ktor.util. { ; } -keep class io.ktor.utils.io. { ; } -keep class java.lang.management. { ; }

Hi @Foso. After I added this I still have errors.

Missing class io.ktor.client.network.sockets.SocketTimeoutException (referenced from: java.lang.Throwable io.ktor.client.engine.cio.ExceptionsKt.mapToKtor(java.lang.Throwable, io.ktor.client.request.HttpRequestData))
Missing class io.ktor.client.network.sockets.TimeoutExceptionsCommonKt (referenced from: java.lang.Object io.ktor.client.engine.cio.Endpoint.makeDedicatedRequest(io.ktor.client.request.HttpRequestData, kotlin.coroutines.CoroutineContext, kotlin.coroutines.Continuation))
Missing class io.ktor.client.plugins.HttpTimeout$HttpTimeoutCapabilityConfiguration (referenced from: kotlin.Pair io.ktor.client.engine.cio.Endpoint.retrieveTimeouts(io.ktor.client.request.HttpRequestData) and 1 other context)
Missing class io.ktor.client.plugins.HttpTimeout$Plugin (referenced from: kotlin.Pair io.ktor.client.engine.cio.Endpoint.retrieveTimeouts(io.ktor.client.request.HttpRequestData))
Missing class io.ktor.client.plugins.HttpTimeout (referenced from: kotlin.Pair io.ktor.client.engine.cio.Endpoint.retrieveTimeouts(io.ktor.client.request.HttpRequestData))
Missing class io.ktor.util.InternalAPI (referenced from: io.ktor.client.engine.cio.CIOEngineContainer and 1 other context)
Missing class io.ktor.util.KtorDsl (referenced from: io.ktor.client.plugins.json.JsonPlugin$Config)
Missing class io.ktor.utils.io.ByteReadChannelJVMKt (referenced from: java.lang.Object io.ktor.client.engine.cio.UtilsKt$withoutClosePropagation$2.invokeSuspend(java.lang.Object) and 3 other contexts)
Missing class io.ktor.utils.io.CoroutinesKt (referenced from: java.lang.Object io.ktor.client.engine.cio.UtilsKt$readResponse$2.invokeSuspend(java.lang.Object) and 9 other contexts)
Missing class io.ktor.utils.io.DelimitedKt (referenced from: java.lang.Object io.ktor.http.cio.MultipartKt$parseMultipart$1.invokeSuspend(java.lang.Object) and 1 other context)
Missing class io.ktor.utils.io.LookAheadSession (referenced from: void io.ktor.http.cio.MultipartKt$skipDelimiterOrEof$3.invoke(io.ktor.utils.io.LookAheadSession) and 5 other contexts)
Missing class io.ktor.utils.io.ReadSessionKt (referenced from: java.lang.Object io.ktor.http.cio.ChunkedTransferEncodingKt.encodeChunked(io.ktor.utils.io.ByteWriteChannel, io.ktor.utils.io.ByteReadChannel, kotlin.coroutines.Continuation))
Missing class io.ktor.utils.io.bits.Memory (referenced from: java.lang.Object io.ktor.http.cio.MultipartInput$fill$1.invokeSuspend(java.lang.Object) and 1 other context)
Missing class io.ktor.utils.io.core.Buffer$Companion (referenced from: java.lang.Object io.ktor.http.cio.ChunkedTransferEncodingKt.encodeChunked(io.ktor.utils.io.ByteWriteChannel, io.ktor.utils.io.ByteReadChannel, kotlin.coroutines.Continuation))
Missing class io.ktor.utils.io.core.Buffer (referenced from: java.lang.Object io.ktor.http.cio.ChunkedTransferEncodingKt.encodeChunked(io.ktor.utils.io.ByteWriteChannel, io.ktor.utils.io.ByteReadChannel, kotlin.coroutines.Continuation))
Missing class io.ktor.utils.io.core.ByteBuffersKt (referenced from: java.lang.Object io.ktor.network.sockets.DatagramSendChannel.trySend-JP2dKIU(io.ktor.network.sockets.Datagram) and 3 other contexts)
Missing class io.ktor.utils.io.core.BytePacketBuilder (referenced from: io.ktor.utils.io.core.BytePacketBuilder io.ktor.http.cio.MultipartKt$parsePreambleImpl$2.$output and 87 other contexts)
Missing class io.ktor.utils.io.core.ByteReadPacket$Companion (referenced from: void io.ktor.network.tls.TLSHandshake.<init>() and 1 other context)
Missing class io.ktor.utils.io.core.ByteReadPacket (referenced from: io.ktor.utils.io.core.ByteReadPacket io.ktor.http.cio.MultipartEvent$Epilogue.body and 83 other contexts)
Missing class io.ktor.utils.io.core.CloseableJVMKt (referenced from: java.lang.Object io.ktor.client.engine.cio.UtilsKt$readResponse$2.invokeSuspend(java.lang.Object) and 2 other contexts)
Missing class io.ktor.utils.io.core.Input (referenced from: io.ktor.utils.io.core.Input io.ktor.http.cio.CIOMultipartDataBase$partToData$3.$input and 22 other contexts)
Missing class io.ktor.utils.io.core.InputArraysKt (referenced from: java.util.List io.ktor.network.tls.ParserKt.readTLSCertificate(io.ktor.utils.io.core.ByteReadPacket) and 1 other context)
Missing class io.ktor.utils.io.core.InputPrimitivesKt (referenced from: io.ktor.network.tls.extensions.NamedCurve io.ktor.network.tls.ParserKt.readCurveParams(io.ktor.utils.io.core.ByteReadPacket) and 8 other contexts)
Missing class io.ktor.utils.io.core.Output (referenced from: java.lang.Object io.ktor.http.cio.MultipartKt$parsePreambleImpl$2.invokeSuspend(java.lang.Object) and 27 other contexts)
Missing class io.ktor.utils.io.core.OutputPrimitivesKt (referenced from: io.ktor.utils.io.core.ByteReadPacket io.ktor.network.tls.RenderKt.buildECCurvesExtension(java.util.List) and 10 other contexts)
Missing class io.ktor.utils.io.core.PreviewKt (referenced from: byte[] io.ktor.network.tls.Digest.doHash-impl(io.ktor.utils.io.core.BytePacketBuilder, java.lang.String) and 1 other context)
Missing class io.ktor.utils.io.core.internal.ChunkBuffer (referenced from: void io.ktor.http.cio.MultipartInput.<init>(java.nio.ByteBuffer, io.ktor.utils.io.ByteReadChannel))
Missing class io.ktor.utils.io.streams.InputKt (referenced from: io.ktor.utils.io.core.Input io.ktor.http.cio.CIOMultipartDataBase$withTempFile$lazyInput$1.invoke() and 1 other context)
Missing class java.lang.management.ManagementFactory (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())
Missing class java.lang.management.RuntimeMXBean (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())
Monabr commented 1 week ago

@Foso After switching from de.jensklingenberg.ktorfit:ktorfit-lib

to de.jensklingenberg.ktorfit:ktorfit-lib-light

the errors reduced to

Missing class io.ktor.util.KtorDsl (referenced from: io.ktor.client.plugins.json.JsonPlugin$Config)
Missing class io.ktor.utils.io.core.Input (referenced from: java.lang.Object io.ktor.client.plugins.json.JsonPlugin$Plugin$install$2.invokeSuspend(java.lang.Object) and 2 other contexts)
Missing class java.lang.management.ManagementFactory (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())
Missing class java.lang.management.RuntimeMXBean (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0()) 
Monabr commented 1 week ago

@Foso After switching from de.jensklingenberg.ktorfit:ktorfit-lib-light

to de.jensklingenberg.ktorfit:ktorfit-lib-light-ktor-3.0.0

the errors reduced to

Missing class java.lang.management.ManagementFactory (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())
Missing class java.lang.management.RuntimeMXBean (referenced from: boolean io.ktor.util.debug.IntellijIdeaDebugDetector.isDebuggerConnected_delegate$lambda$0())

I still have rule -keep class java.lang.management.** { *; }

But it does not help