学习目标:
熟练使用组件化开发,路由配置
学习内容:
在使用组件化开发前首先要明确项目整体框架,划分模块及业务(重点),好的开始才会有好的结果。模块划分明确后开始配置Module。
如图我们要完成以下功能:
1.点击商城进入ShoppingModule
2.点击登录进入LoginModule
3.点击账单红色区域展示账单列表(其他Module中的Fragment)
(shareModule为公共模块)
根据业务需求创建如下:
在App的gradle.properties文件下添加,用于控制module是否独立运行。
#配置某个组件是否可以独立运行
isShoppingRunAlone = true
isLoginRunALone = true
然后配置App build.gradle。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.moduledemo"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//导入公共模块
implementation project(':ShareModule')
// 根据gradle中的配置来决定是否引用module
if (!isLoginRunALone.toBoolean()){
implementation project(':LoginModule')
}
if (!isShoppingRunAlone.toBoolean()){
implementation project(':ShoppingModule')
}
}
继续配置其他Module的build.gradle文件。
if (isShoppingRunAlone.toBoolean()){
apply plugin: 'com.andr