The original blog
Usage scenarios
To decouple software operations, ensure that activities jump between modules without affecting each other. Hence ali Baba’s Arouter. However, there is no integration description for Kotlin on the official website, so it is noted here
How to use
gradle
configuration- Directory configuration, constant class configuration
- in
Application
In theArouter
Initialize the Activity
The configuration of the
1. gradle
configuration
Note that you need to configure in two places
1. Add the arouter-register reference to dependencies in the build.gradle directory
Dependencies {classpath "com.alibaba:arouter-register:1.0.2"}Copy the code
2. Add references and build configurations in build.gradle where the module is located
Plugins {// 1. Add kotlin-kapt reference id 'kotlin-kapt'} android {// 2. Add Arouter compile configuration, pay attention to the order. Kapt {arguments {arg("AROUTER_MODULE_NAME", project.getName())}}} dependencies {// 3. Add gradle reference implementation 'com.alibaba: arouter-API :1.5.1' kapt "com.alibaba:arouter-compiler:1.5.1"}Copy the code
Note: this tutorial is different from the one on the official website. The official configuration is Java specific, so I didn’t use javaCompileOptions and annotationProcessor’com.alibaba: arouter-Compiler :1.5.1′, which don’t work with Kotlin.
2. Directory configuration and constant class configuration
Create a UI package for the Activity to jump to, and then create a Constant Object file. Add the Activity’s constant resource
Constant class Constants
object Constants {
object Activitys{
const val RECYCLELIST_ACTIVITY = "/ui/RecycleListActivity"
}
}
Copy the code
The directory structure
In 3.Application
In theArouter
Initialize the
class App : Application() { override fun onCreate() { super.onCreate() if (BuildConfig.DEBUG){ ARouter.openLog() ARouter.openDebug() } ARouter.init(this) } override fun onTerminate() { super.onTerminate() ARouter.getInstance().destroy() } }Copy the code
** Note :** there are two pits here.
- Rewrite the
APP
Class need to be inManifest
Otherwise, the command will not be executed. (You just need to be thereapplication
Add to nodename
And point to this class.) - Pay attention to
BuildConfig
Who does this class refer to becauseArouter
Itself hasBuildConfig
.A reference is needed hereAnroid
theBuildConfig
. The blogger has been unable to jump after quoting the wrong one. And there has been no error, pit for a long time
4. Activity
The configuration of the
With that done, you can configure the Activity to jump to.
To jump to the Activity, add the @route annotation
@Route(path = Constants.Activitys.RECYCLELIST_ACTIVITY)
class RecycleListActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycle_list)
}
}
Copy the code
For an Activity that needs to jump, call Arouter singleton to jump
mBtnList.setOnClickListener {
ARouter.getInstance().build(Constants.Activitys.RECYCLELIST_ACTIVITY).navigation()
}
Copy the code