root build.gradle
dependencies {
classpath "io.github.ckenergy:KRoutePlugin:$KRouter_version"
}
app build.gradle
plugins {
id 'KRoute-plugin'
}
implementation("io.github.ckenergy:kRouter-core:$KRouter_version")
add in module build.gradle where use @KRouter
ksp {
arg("KROUTER_PACKAGE", "you module namespace")
}
ksp("io.github.ckenergy:kRouter-compiler:$KRouter_version")
@KRouter(ComposeRouterMapper.Second)
@Composable
fun SecondPage(int: Int, string: String? = "default", testBean: TestBean, vararg long: Long)
Note: The parameters on the function can only put simple data (primitives , string and beans, because this piece is serialized by Gson)
The code above is equal to the original way of registering routes
NavHost() {
composable(ComposeRouterMapper.Second) {
//... get parameters
SecondPage(....)
}
}
NavHost() {
NavGraphManager.initKRouter(context, this, navController) {
notFindPage = { route: String -> NotFindPage(route) }
}
}
controller.navigateRoute(ComposeRouterMapper.Second) {
put("int", 1)//key的名字要和SecondPage函数参数名要保持一致一一对应
put("long", longArrayOf(1L, 3L))
put("testBean", TestBean(1.5f, false))
}
When your compose page parameters are more complex, such as parameter is function
@Composable
fun OtherPage(start: () -> Unit)
At this time, you need to customize your own to register the route
val navGraph = composeModules { controller ->
packageName = "com.ckenergy.compose.other"
composable(ComposeRouterMapper.Other) {
OtherPage {
controller.navigateRoute(ComposeRouterMapper.Second)
}
}
}
NavGraphManager.initKRouter(context, this, navController) {
notFindPage = { route: String -> NotFindPage(route) }
isAnimation = true
isDebug = BuildConfig.DEBUG
addModules(navGraph)//add route map
}