ktor android local http server throws error
Asked Answered
T

4

7

What did I do?

  1. Added dependencies under build.gradle
implementation "io.ktor:ktor:1.3.2"
implementation "io.ktor:ktor-server-netty:1.3.2"
implementation "io.ktor:ktor-gson:1.3.2"
  1. AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
  1. MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        embeddedServer(Netty, 9000) {
            install(ContentNegotiation) {
                gson {}
            }
            routing {
                get("/") {
                    call.respond(mapOf("message" to "Hello world"))
                }
            }
        }.start(wait = true)
    }
}

What went wrong?

Upon running app, I get following errors & android local server isn't starting.

No implementation found for int io.netty.channel.kqueue.Native.sizeofKEvent() 
(tried Java_io_netty_channel_kqueue_Native_sizeofKEvent 
and Java_io_netty_channel_kqueue_Native_sizeofKEvent__)

No implementation found for int io.netty.channel.epoll.Native.offsetofEpollData() 
(tried Java_io_netty_channel_epoll_Native_offsetofEpollData 
and Java_io_netty_channel_epoll_Native_offsetofEpollData__)
Trimly answered 21/6, 2020 at 15:27 Comment(0)
E
4

Netty's native transport doesn't support Android. You should probably try another Ktor engine, like CIO or Jetty.

Elegit answered 21/6, 2020 at 15:44 Comment(1)
I followed this article - diamantidis.github.io/2019/11/10/… & it suggested. I will try Jetty or CIOTrimly
T
4

How did I fix it?

  1. Added dependencies under build.gradle
implementation "io.ktor:ktor:1.3.2"
implementation "io.ktor:ktor-server-jetty:1.3.2"
implementation "io.ktor:ktor-gson:1.3.2"

also

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
    ...
    minSdkVersion 26
    ...
}
  1. AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
  1. MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        embeddedServer(Jetty, 9000) {
            install(ContentNegotiation) {
                gson {}
            }
            routing {
                get("/") {
                    call.respond(mapOf("message" to "Hello world"))
                }
            }
        }.start(wait = false)
    }
}
Trimly answered 21/6, 2020 at 17:53 Comment(3)
What was the specific change here that made a difference? I'm also seeing same error....but if I try Jetty I'm getting a different oneWyckoff
Am getting "NoClassDefFoundError: Failed resolution of: Ljavax/naming/ldap/LdapName;" if I use JettyWyckoff
to me it was Jetty that made the warning go away. Btw it was still working with the message when using Netty.Pamilapammi
B
4

I had the same issue. Change:

start(wait = true)

To:

start(wait = false)

And the Ktor server should be up and running on Android.

Bankruptcy answered 6/7, 2020 at 16:41 Comment(2)
This worked for me too, developing on minSdkVersion 25 with io.ktor:ktor-server-netty:1.5.2. CIO works too, but Jetty doesn't compile because it requires minSdkVersion 26.Canteen
As a side note, I must add that in reality the server could work even with wait = true and served requests as expected. However, the rest of the Android app would freeze completely and remain unusable.Canteen
D
1

If you dig into the source code in ktor package. You'll find out the error is just a verbose message that indicates that if it doesn't find out os-dependent native event loop implementation: kqueue, epoll, it will fallback to use internal implementation: nioEventLoop. It is safe to ignore it.

https://github.com/ktorio/ktor/blob/40bb2a4829a6ad05fd5337f10bea7bf92f1a5fc2/ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt#L220

Darla answered 11/12, 2020 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.