Android kotlin jetpack compose 在APP中部署运行ktor服务器

本文档展示了如何在Android应用中使用Kotlin和Jetpack Compose部署Ktor服务器,实现局域网内通过Web访问APP功能。首先添加Ktor及相关依赖,解决编译时的资源冲突问题。然后创建KtorServer对象,包含启动和停止服务器的方法。在MainActivity中启动和关闭服务器,并提供了一个简单的DEMO展示。源代码已上传至GitHub。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android kotlin jetpack compose 在APP中部署运行ktor服务器

前言

遇到需求,需要在APP中部署一个服务器,局域网中通过web来访问这个APP的功能,相当于在APP中部署一个前后端项目。

后端我选择了ktor作为开发框架,基于kotlin,写协程挂起函数方便。

添加依赖

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        coreLibraryDesugaringEnabled true
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
            // 当编译时出现报错,按情况添加
            excludes += "/META-INF/INDEX.LIST"
            excludes += "/META-INF/io.netty.versions.properties"
        }
    }
}

dependencies {

    // Java 8
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

    // 协程
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0"

    // ktor
    def ktorVersion = '1.6.7'
    implementation "io.ktor:ktor-server-core:$ktorVersion"
    implementation "io.ktor:ktor-server-netty:$ktorVersion"
    testImplementation("io.ktor:ktor-server-tests:$ktorVersion")
    implementation 'ch.qos.logback:logback-classic:1.2.11'
}

编译的时候,我这边报了以下错误:

10 files found with path ‘META-INF/INDEX.LIST’.
Adding a packagingOptions block may help, please refer to
https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
for more information

11 files found with path ‘META-INF/io.netty.versions.properties’.
Adding a packagingOptions block may help, please refer to
https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
for more information

将提示的文件路径填写到build.gradle的Android里

android {
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
            // 当编译时出现报错,按情况添加
            excludes += "/META-INF/INDEX.LIST"
            excludes += "/META-INF/io.netty.versions.properties"
        }
    }
}

服务器管理

新建KtorServer.kt

import android.os.Build
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object KtorServer {

    private val server by lazy {
        embeddedServer(Netty, 12345) {
            install(CallLogging)
            // 跨域访问
            install(CORS) {
                anyHost()
                header(HttpHeaders.ContentType)
                method(HttpMethod.Options)
                method(HttpMethod.Put)
                method(HttpMethod.Patch)
                method(HttpMethod.Delete)
            }

            routing {
                get("/") {
                    call.respondText("手机型号 ${Build.MODEL} 运行正常", ContentType.Text.Plain)
                }
            }
        }
    }

    /** 启动服务器 */
    fun start() {
        CoroutineScope(Dispatchers.IO).launch { server.start(true) }
    }

    /** 停止服务器 */
    fun stop() {
        server.stop(1_000, 2_000)
    }
}

活动

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 启动服务器
        KtorServer.start()

        setContent {
            AndroidKtorServerDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting(localIPAddress?: "0.0.0.0")
                }
            }
        }
    }

    override fun onDestroy() {
        // 结束服务器
        KtorServer.stop()
        super.onDestroy()
    }
}

@Composable
fun Greeting(ip: String) {
    Text(text = "请打开局域网中的任意浏览器访问 Http://$ip:12345")
}

效果

在这里插入图片描述
在这里插入图片描述

DEMO

https://github.com/D10NGYANG/AndroidKtorServerDemo

完事

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值