我试图将Ktor嵌入到Android服务中,以便在某个时刻远程检查应用程序上的某些资产。
我正在关注此tutorial
中的代码当我尝试访问链接时出现此错误,例如" 192.168.2.105:7070":
04-20 14:50:58.523 29389-29389 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mypackage.fetchingservice, PID: 29389
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:16)
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.hirschandmann.magickservice.KtorFetchService.onCreate(KtorFetchService.kt:30)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2761)
at android.app.ActivityThread.access$1800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:935)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730)
这是代码。服务已在清单中注册,我使用以下命令从adb启动它:
adb shell am startservice com.mypackage.service/.KtorFetchService
class KtorFetchService : Service() {
override fun onBind(intent: Intent?): IBinder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
private val HTTP_PORT = 7070
override fun onCreate() {
embeddedServer(Netty, HTTP_PORT) {
routing {
get("/") {
call.respondText("My Example Fetch", ContentType.Text.Html)
}
}
}.start(wait = true)
super.onCreate()
}
}
这是模块gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.hirschandmann.magickservice"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
pickFirst 'META-INF/*'
}
}
repositories {
jcenter()
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/ktor" }
maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}
configurations {
ktlint
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation'com.android.support:appcompat-v7:27.1.1'
testImplementation'junit:junit:4.12'
androidTestImplementation'com.android.support.test:runner:1.0.1'
androidTestImplementation'com.android.support.test.espresso:espresso-core:3.0.1'
// AutoValue
compileOnly "com.google.auto.value:auto-value:1.5.2"
annotationProcessor "com.google.auto.value:auto-value:1.5.2"
// ktlint
ktlint "com.github.shyiko:ktlint:0.15.0"
implementation'com.github.hkk595:Resizer:v1.5'
implementation'com.jrummyapps:android-shell:1.0.1'
implementation'org.nanohttpd:nanohttpd:2.3.1'
implementation"io.ktor:ktor:$ktor_version" // ktor dependency
implementation"io.ktor:ktor-server-netty:$ktor_version"
}
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
args "src/**/*.kt"
//args "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
}
check.dependsOn ktlint
androidExtensions {
experimental = true
}
这是项目的gradle文件:
buildscript {
ext.kotlin_version = '1.2.40'
ext.ktor_version = '0.9.1'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id "io.gitlab.arturbosch.detekt" version "1.0.0.RC6-3"
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
detekt {
version = "1.0.0.RC6-3"
profile("main") {
input = "$projectDir/app/src/main/java"
config = "$projectDir/default-detekt-config.yml"
filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
该设备是带有Android 5.1.1的Odroid C2(API 22) 我在MacOS 10.13.3上运行Android studio 3.1和JRE 1.8。
答案 0 :(得分:3)
该错误是由ktor ApplicationEvents.kt:18中的以下行引起的
handlers.computeIfAbsent(definition) { LockFreeLinkedListHead() }.addLast(registration)
这会导致它失败,直到在API级别24(proof)中最初引入了ConcurrentHashMap.computeIfAbsent
和java.util.function.Function
。
请注意,ktor服务器从未打算在android上运行,而最初是针对具有JDK8的服务器
答案 1 :(得分:0)
我已经在 Android 5+ 上运行基于 Netty 的 Ktor 服务器一段时间了,我创建了这个最小的存储库来演示它:https://github.com/gnarea/minimal-ktor-server-android