因此,我在 Android Studio 上为我的应用程序创建了一个后端,并在 React Native 上创建了前端。但是,在 Android Studio 上运行该应用程序不会保持运行状态。它很快终止,我需要它保持运行。有想法该怎么解决这个吗?我尝试在 Android Manifest 文件中添加 Android:persistent: true 但没有成功
这就是我在我的原木猫中看到的
"C:\Program Files\Android\Android Studio\jbr\bin\java.exe" "-javaagent:C:\Program Files\Android\Android Studio\lib\idea_rt.jar=51284:C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 @C:\Users\haroo\AppData\Local\Temp\idea_arg_file2069765526 com.example.prosubscriberapp.ApplicationKt
16:52:02.016 [main] INFO Application -- Autoreload is disabled because the development mode is off.
16:52:02.137 [main] INFO Application -- Application started in 0.88 seconds.
16:52:02.307 [KtorShutdownHook] DEBUG io.netty.util.internal.logging.InternalLoggerFactory -- Using SLF4J as the default logging framework
16:52:02.693 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- -Dio.netty.noUnsafe: false
16:52:02.695 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- Java version: 17
16:52:02.710 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- sun.misc.Unsafe.theUnsafe: available
16:52:02.718 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- sun.misc.Unsafe.copyMemory: available
16:52:02.725 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- sun.misc.Unsafe.storeFence: available
16:52:02.732 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- java.nio.Buffer.address: available
16:52:02.740 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- direct buffer constructor: unavailable: Reflective setAccessible(true) disabled
16:52:02.746 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- java.nio.Bits.unaligned: available, true
16:52:02.753 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable: class io.netty.util.internal.PlatformDependent0$7 cannot access class jdk.internal.misc.Unsafe (in module java.base) because module java.base does not export jdk.internal.misc to unnamed module @1e8f3784
16:52:02.761 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent0 -- java.nio.DirectByteBuffer.<init>(long, {int,long}): unavailable
16:52:02.761 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- sun.misc.Unsafe: available
16:52:02.762 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- -Dio.netty.tmpdir: C:\Users\haroo\AppData\Local\Temp (java.io.tmpdir)
16:52:02.763 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- -Dio.netty.bitMode: 64 (sun.arch.data.model)
16:52:02.771 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- Platform: Windows
16:52:02.785 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- -Dio.netty.maxDirectMemory: -1 bytes
16:52:02.786 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- -Dio.netty.uninitializedArrayAllocationThreshold: -1
16:52:02.797 [KtorShutdownHook] DEBUG io.netty.util.internal.CleanerJava9 -- java.nio.ByteBuffer.cleaner(): available
16:52:02.797 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- -Dio.netty.noPreferDirect: false
16:52:03.580 [KtorShutdownHook] DEBUG io.netty.channel.MultithreadEventLoopGroup -- -Dio.netty.eventLoopThreads: 24
16:52:03.720 [KtorShutdownHook] DEBUG io.netty.util.concurrent.GlobalEventExecutor -- -Dio.netty.globalEventExecutor.quietPeriodSeconds: 1
16:52:03.792 [KtorShutdownHook] DEBUG io.netty.util.internal.InternalThreadLocalMap -- -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
16:52:03.792 [KtorShutdownHook] DEBUG io.netty.util.internal.InternalThreadLocalMap -- -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
16:52:03.949 [KtorShutdownHook] DEBUG io.netty.channel.nio.NioEventLoop -- -Dio.netty.noKeySetOptimization: false
16:52:03.950 [KtorShutdownHook] DEBUG io.netty.channel.nio.NioEventLoop -- -Dio.netty.selectorAutoRebuildThreshold: 512
16:52:04.073 [KtorShutdownHook] DEBUG io.netty.util.internal.PlatformDependent -- org.jctools-core.MpscChunkedArrayQueue: available
Process finished with exit code 0
这是我的 MainActivity.kt 文件
package com.example.prosubscriberapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Start Ktor server
val server = embeddedServer(Netty, port = 8080) {
proSubscriberApp()
}
server.start()
}
}
这是Application.kt
package com.example.prosubscriberapp
import com.example.prosubscriberapp.routes.planRoutes
import com.example.prosubscriberapp.routes.subscriptionRoutes
import com.example.prosubscriberapp.routes.transactionRoutes
import com.example.prosubscriberapp.routes.userRoutes
import com.example.prosubscriberapp.service.PlanService
import com.example.prosubscriberapp.service.RecommendationService
import com.example.prosubscriberapp.service.SubscriptionService
import com.example.prosubscriberapp.service.TransactionService
import com.example.prosubscriberapp.service.UserService
import com.typesafe.config.ConfigFactory
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.config.HoconApplicationConfig
import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.routing.routing
fun Application.installCORS() {
install(CORS) {
anyHost() // Allow requests from any origin
}
}
fun main(args: Array<String>) {
val environment = applicationEngineEnvironment {
config = HoconApplicationConfig(ConfigFactory.load())
// Other configuration settings
}
embeddedServer(Netty, environment).start()
}
fun Application.proSubscriberApp() {
// Initialize services
val userService = UserService()
val planService = PlanService()
val recommendationService = RecommendationService()
val subscriptionService = SubscriptionService(recommendationService)
val transactionService = TransactionService()
install(ContentNegotiation) {
json()
}
installCORS()
// Configure routes
routing {
userRoutes(userService)
planRoutes(planService)
subscriptionRoutes(subscriptionService)
transactionRoutes(transactionService)
}
}
应用布局并这样做
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Start Ktor server in a separate thread
GlobalScope.launch(Dispatchers.IO) {
val server = embeddedServer(Netty, port = 8080) {
proSubscriberApp()
}
server.start()
}
}
}