Arouter配置指南

Arouter实现了大型App之间的轻耦合,降低代码的复杂度,易维护。如果不使用Arouter那么项目之前的关系可能是如下:底层模块间相互耦合,工程代码复杂度高,不便于管理。

https://feed.iggv5.com/c/69928a9d-c870-405f-9a72-ea8af96ed218
请添加图片描述

Arouter

引入Arouter后希望底层模块间轻耦合,模块间的引用跳转交给Arouter完成,各个模块间的效果如下。
请添加图片描述
Module之间不通过依赖的关系,而是通过Arouter路由转发,App作为一个壳不实现具体功能,而是作为路由的中转,另外模块之间也可以通过Arouter相互引用。

Android项目Arouter引入

新建Android工程,新建四个模块分为App模块,base_interface模块, base_impl模块,app_setting模块。
在这里插入图片描述

依赖关系如下:
在这里插入图片描述
app_setting和base_impl间不相互依赖。将各个模块导入Arouter依赖,app作为Application,其他作为library. App模块build.gradle文件如下:


plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

// kotlin/java混编
apply plugin: 'kotlin-kapt'

android {
    namespace 'com.hxzlj.multi_module'
    compileSdk 33

    defaultConfig {
        applicationId "com.hxzlj.multi_module"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

	//工程依赖
    implementation project(path: ':base_impl')
    implementation project(path: ':app_setting')
    //Arouter
    api 'com.alibaba:arouter-api:1.5.1'
    kapt 'com.alibaba:arouter-compiler:1.5.1'
}

base_impl的build.gradle文件

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
    namespace 'com.hxzlj.base_impl'
    compileSdk 33

    defaultConfig {
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    api project(path: ':base_interface')

    api 'com.alibaba:arouter-api:1.5.1'
    kapt 'com.alibaba:arouter-compiler:1.5.1'
}

必须注意, androidx必须要有android.enableJetifier=true。工程的gradle.properties中。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值