WHCSRL 技术网

ARouter的集成使用

1.这里区分Java和Kotlin,两种方式引用方式略有不同

在app的build.gradle的defaultConfig中配置

  1. android{
  2. defaultConfig {
  3. applicationId "com.example.myapplication"
  4. minSdkVersion 16
  5. targetSdkVersion 29
  6. versionCode 1
  7. versionName "1.0"
  8. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  9. ndk {
  10. abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
  11. }
  12. // Java ARouter配置
  13. // javaCompileOptions {annotationProcessorOptions {arguments = [AROUTER_MODULE_NAME: project.getName()]}}
  14. // Kotlin ARouter配置
  15. kapt {arguments {arg("AROUTER_MODULE_NAME", project.getName())}}
  16. }
  17. }

引入依赖:

  1. // Java ARouter依赖
  2. // compile 'com.alibaba:arouter-api:1.5.2'
  3. // annotationProcessor 'com.alibaba:arouter-compiler:1.5.2'
  4. // Kotlin ARouter依赖
  5. implementation "com.alibaba:arouter-api:1.5.2"
  6. kapt "com.alibaba:arouter-compiler:1.5.2"

另外:如果是Kotlin项目 需要添加plug:

  1. plugins {
  2. id 'com.android.application'
  3. id 'kotlin-android'
  4. //如果是Kotlin项目 添加以下Pluging
  5. id 'kotlin-kapt'
  6. id 'kotlin-android-extensions'
  7. }

这里我放一下我的目录结构

 主要是从MainActivity跳转到不同的Activity

 下面是代码部分:

1.统一的路径类

  1. package com.example.myapplication.aaa
  2. object RouterPathCons {
  3. //路径可以随便配置 但是必须以/开头 一般最好两级/ / 跳转时候build()括号中的路径和目标Activity的@Route(path="路径")必须一致
  4. const val RouterActivity1: String = "/aaa/a1";
  5. const val RouterActivity2: String = "/bbb/RouterActivity2";
  6. const val RouterActivity3: String = "/ccc/ccc1";
  7. const val RouterActivity4: String = "/ddd/ddd2";
  8. }

2.application中初始化

  1. package com.example.myapplication
  2. import android.app.Application
  3. import com.alibaba.android.arouter.launcher.ARouter
  4. class Apps: Application(){
  5. //Arouter调试开关
  6. private val isDebugArouter : Boolean =true;
  7. override fun onCreate() {
  8. super.onCreate()
  9. if (isDebugArouter) {
  10. ARouter.openLog()
  11. ARouter.openDebug()
  12. }
  13. ARouter.init(this)
  14. }
  15. }

 3.MainActivity中的跳转

  1. package com.example.myapplication
  2. import androidx.appcompat.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.view.View
  5. import android.widget.Button
  6. import com.alibaba.android.arouter.launcher.ARouter
  7. import com.example.myapplication.aaa.RouterPathCons
  8. class MainActivity : AppCompatActivity(),View.OnClickListener {
  9. override fun onCreate(savedInstanceState: Bundle?) {
  10. super.onCreate(savedInstanceState)
  11. ARouter.getInstance().inject(this)
  12. setContentView(R.layout.activity_main)
  13. val btr1= findViewById<Button>(R.id.bt_r1);
  14. val btr2= findViewById<Button>(R.id.bt_r2);
  15. val btr3= findViewById<Button>(R.id.bt_r3);
  16. val btr4= findViewById<Button>(R.id.bt_r4);
  17. btr1.setOnClickListener(this)
  18. btr2.setOnClickListener(this)
  19. btr3.setOnClickListener(this)
  20. btr4.setOnClickListener(this)
  21. }
  22. override fun onClick(v: View?) {
  23. when (v?.id) {
  24. R.id.bt_r1 -> {
  25. ARouter.getInstance().build(RouterPathCons.RouterActivity1).navigation()
  26. }
  27. R.id.bt_r2 -> {
  28. ARouter.getInstance().build(RouterPathCons.RouterActivity2)
  29. .withByte("p_byte",88)
  30. .withBoolean("p_bool",false)
  31. .withChar("p_char",'u')
  32. .withDouble("p_double",0.34)
  33. .withFloat("p_float",0.12f)
  34. .navigation()
  35. }
  36. R.id.bt_r3 -> {
  37. ARouter.getInstance().build(RouterPathCons.RouterActivity3).navigation()
  38. }
  39. R.id.bt_r4 -> {
  40. ARouter.getInstance().build(RouterPathCons.RouterActivity4)
  41. .withByte("p_byte",99)
  42. .withBoolean("p_bool",true)
  43. .withChar("p_char",'j')
  44. .withDouble("p_double",0.88)
  45. .withFloat("p_float",0.77f)
  46. .navigation()
  47. }
  48. else -> {
  49. }
  50. }
  51. }
  52. }

4.其他的类  

JavaRouter1Activity

  1. package com.example.myapplication.aaa;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import androidx.annotation.Nullable;
  5. import com.alibaba.android.arouter.facade.annotation.Route;
  6. import com.example.myapplication.R;
  7. @Route(path = RouterPathCons.RouterActivity3)
  8. public class JavaRouter1Activity extends Activity {
  9. @Override
  10. protected void onCreate(@Nullable Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_route3);
  13. }
  14. }
JavaRouter2Activity
  1. package com.example.myapplication.aaa;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import androidx.annotation.Nullable;
  6. import com.alibaba.android.arouter.facade.annotation.Autowired;
  7. import com.alibaba.android.arouter.facade.annotation.Route;
  8. import com.alibaba.android.arouter.launcher.ARouter;
  9. import com.example.myapplication.R;
  10. import kotlin.jvm.JvmField;
  11. @Route(path = RouterPathCons.RouterActivity4)
  12. public class JavaRouter2Activity extends Activity {
  13. @Autowired(name = "p_char")
  14. char cuchar;
  15. @Autowired(name = "p_float")
  16. float a;
  17. @Autowired(name = "p_double")
  18. double b;
  19. @Override
  20. protected void onCreate(@Nullable Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. ARouter.getInstance().inject(this);
  23. setContentView(R.layout.activity_route4);
  24. Log.e("======",cuchar+","+"float:"+a+",double:"+b);
  25. }
  26. }
KtRouter1Activity
  1. package com.example.myapplication.aaa
  2. import android.os.Bundle
  3. import androidx.appcompat.app.AppCompatActivity
  4. import com.alibaba.android.arouter.facade.annotation.Route
  5. import com.alibaba.android.arouter.launcher.ARouter
  6. import com.example.myapplication.R
  7. @Route(path = RouterPathCons.RouterActivity1)
  8. class KtRouter1Activity :AppCompatActivity() {
  9. override fun onCreate(savedInstanceState: Bundle?) {
  10. super.onCreate(savedInstanceState)
  11. setContentView(R.layout.activity_route1)
  12. ARouter.getInstance().inject(this)
  13. }
  14. }

 RouterActivity2

  1. package com.example.myapplication.aaa
  2. import android.os.Bundle
  3. import android.util.Log
  4. import androidx.appcompat.app.AppCompatActivity
  5. import com.alibaba.android.arouter.facade.annotation.Autowired
  6. import com.alibaba.android.arouter.facade.annotation.Route
  7. import com.alibaba.android.arouter.launcher.ARouter
  8. import com.example.myapplication.R
  9. @Route(path = RouterPathCons.RouterActivity2)
  10. class RouterActivity2 :AppCompatActivity() {
  11. @Autowired(name="p_byte")
  12. @JvmField
  13. var mbyte:Byte=0
  14. @Autowired(name="p_float")
  15. @JvmField
  16. var mfloat:Float=0.0f
  17. @Autowired(name="p_double")
  18. @JvmField
  19. var mdouble:Double=-1.0
  20. override fun onCreate(savedInstanceState: Bundle?) {
  21. super.onCreate(savedInstanceState)
  22. setContentView(R.layout.activity_route2)
  23. ARouter.getInstance().inject(this)
  24. Log.e("KtRouter2","Byte:$mbyte ,Float:$mfloat ,Double:$mdouble")
  25. }
  26. }

需要注意的是:使用ARouter跳转  

1.目标Activity必须配置

@Route(path="路径") 

2.如果目标Activity需要接收携带过来的参数除了要配置

  1. ARouter.getInstance().inject(this)
  2. @Autowired(name="需要与传递过来的Key一致")

如果是Kotlin类的Activity  需要加上@JvmField 使变量非私有,否则编译会报错

ARouter::Compiler An exception is encountered, [The inject fields CAN NOT BE 'private'!!! please check field [mdouble] in class [com.example.myapplication.aaa.RouterActivity2]]

 The inject fields CAN NOT BE 'private'!!! 

 我就匆匆忙忙引入试了一下   还有其他没介绍的 之后有时间再写

推荐阅读