Linker provides an annotation-based API to handle URI routing for Android. This library is written in kotlin, and the generated codes are also pure kotlin.

Dependencies

Add the following to your build.gradle file:

dependencies {
	api 'Me. Twocities: would: 0.0.5'
	kapt 'Me. Twocities: would - compiler: 0.0.5'
}Copy the code

Annotations

@Link for activity

Use annotation @Link indicate which URI was respect:

@Link("link://product/detail{id})
class ProductActivity: AppCompatActivity {
}Copy the code

@LinkPath @LinkQuery for parameters

@Link("link://product/detail/{id})
class ProductActivity: AppCompatActivity {
  @LinkPath("id") lateinit var productId: String
  @LinkQuery("title") lateinit var productTitle: String
}Copy the code

After annotation processing, an extension function bindLinkParams() of ProductActivity will be generated, you can use it to get values of @LinkPath @LinkQuery params:

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  bindLinkParams()
}Copy the code

@LinkModule for module

// generate LinkerExampleLinkModule
@LinkModule
class ExampleLinkModuleCopy the code

After annotation processing, a module class was generated, will contains informations of all defined @Link, @LinkPath, @LinkQuery. The generated class will be used to decided which uri will be routed.

@LinkModule supports multi android’s library module.

@LinkResolverBuilder for builder

// generate LinkerExampleLinkResolverBuilder
@LinkResolverBuilder(modules = arrayOf(ExampleLinkModule: :class, LibraryLinkModule: :class))
interface ExampleLinkResolverBuilderCopy the code

This will generate a LinkResolver’s builder:

val resolver = LinkerExampleLinkResolverBuilder(context).build()
Copy the code

Advance

Linker also provide other mechanisms: Interceptor, FallbackHandler, which you can change the behavior of a link. You can add interceptors or set fallback handler by the generated builder class:

 val resolver = LinkerBuilder(context)
        .addInterceptor(HttpUrlInterceptor(context))
        .setFallbackHandler(DefaultUrlHandler(context))
        .setListener(ResolverListener())
        .build()Copy the code

the Listener will be notified when an link was resolved.

FallbackHandler

If there’s no activities match with the given link, or no interceptors has intercepted, the fallback handler be called. The [FallbackHandler] gives you the ability to handle unknown link: start another activity or show an error page.

TODO

  • Generate link’s builder when compiling
  • Support multi links for per activity
  • More unit tests

License

The Apache License, Version 2.0