ARouter配置纪要

一、基本步骤

1、业务Module的build.gradle中添加配置和依赖

(1)Java实现Module配置:

android {
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }
    }
}

dependencies {
    // Replace with the latest version
    compile 'com.alibaba:arouter-api:?'
    annotationProcessor 'com.alibaba:arouter-compiler:?'
    ...
}

(2)Kotlin实现Module配置

// You can refer to the wording in the "module-kotlin" module
apply plugin: 'kotlin-kapt'

kapt {
    arguments {
        arg("AROUTER_MODULE_NAME", project.getName())
    }
}

dependencies {
    compile 'com.alibaba:arouter-api:x.x.x'
    kapt 'com.alibaba:arouter-compiler:x.x.x'
    ...
}

2、Activity上添加注解

@Route(path = "/test/activity")
public class YourActivity extend Activity {
    ...
}

3、Application中初始化ARouter

if (isDebug()) {           // These two lines must be written before init, otherwise these configurations will be invalid in the init process
    ARouter.openLog();     // Print log
    ARouter.openDebug();   // Turn on debugging mode (If you are running in InstantRun mode, you must turn on debug mode! Online version needs to be closed, otherwise there is a security risk)
}
ARouter.init(mApplication); // As early as possible, it is recommended to initialize in the Application

4、跳转

// 1. Simple jump within application (Jump via URL in 'Advanced usage')
ARouter.getInstance().build("/test/activity").navigation();

// 2. Jump with parameters
ARouter.getInstance().build("/test/1")
            .withLong("key1", 666L)
            .withString("key3", "888")
            .withObject("key4", new Test("Jack", "Rose"))
            .navigation();

二、注意点:

1、Module如果用Kotlin实现(部分或者全部),配置要按kotlin类型配置

2、主Module(一般称为壳工程)即类型为application的Module需要将所有其他Module添加到依赖中,不然无法跳转其他Module的界面,library类型的Module不需要也不可以相互依赖


 

你可能感兴趣的:(Android,Android进阶之路)