Ktor HTTP Server on Android 5.0 (API 21) leads to an error
See original GitHub issueRunning the Ktor embedded HTTP Server in an Android app on a real Android 5.0 device (Lenovo Tab3 Essential 7) or in the Emulator leads to exactly the error described already at stackoverflow by someone else. When running the same app out of Android Studio in the Emulator with Android 7, it is working well without an error.
The basic code that leads to the error:
val server = embeddedServer(Netty, port = 8080) { routing { get("/") { call.respondText("Hello World!", ContentType.Text.Plain) } } }
The Error / Stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tabelos.tabelos, PID: 4028 java.lang.NoClassDefFoundError: io.ktor.application.ApplicationEvents$subscribe$1 at io.ktor.application.ApplicationEvents.subscribe(ApplicationEvents.kt:18) at io.ktor.server.engine.BaseApplicationEngine.<init>(BaseApplicationEngine.kt:29) at io.ktor.server.engine.BaseApplicationEngine.<init>(BaseApplicationEngine.kt:15) at io.ktor.server.netty.NettyApplicationEngine.<init>(NettyApplicationEngine.kt:17) at io.ktor.server.netty.Netty.create(Embedded.kt:10) at io.ktor.server.netty.Netty.create(Embedded.kt:8) at io.ktor.server.engine.EmbeddedServerKt.embeddedServer(EmbeddedServer.kt:50) at io.ktor.server.engine.EmbeddedServerKt.embeddedServer(EmbeddedServer.kt:40) at io.ktor.server.engine.EmbeddedServerKt.embeddedServer$default(EmbeddedServer.kt:27) at com.tabelos.tabelos.MainActivity.runServer(MainActivity.kt:48) at com.tabelos.tabelos.MainActivity.access$runServer(MainActivity.kt:21) at com.tabelos.tabelos.MainActivity$runnable$1.run(MainActivity.kt:43) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I would welcome any help or advice, because I definitely need to target Android 5.0 because of the mentioned Lenovo Tab 3 Essential 7 Tablets, I’m using. I already tried downgrading kotlin, ktor versions without any luck so far.
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (8 by maintainers)
Top GitHub Comments
Looks like AGP 4.0 will desugar it https://developer.android.com/studio/preview/features#j8-desugar so it should work
@kambojumang, we’re running a ktor server in the background in https://github.com/relaycorp/relaynet-gateway-android, in case you want a real-world example. Here’s the ktor server implementation: https://github.com/relaycorp/relaynet-gateway-android/tree/3680176902629b045a7231a2207d0585a20a8998/app/src/main/java/tech/relaycorp/gateway/pdc/local