当一个android程序过大的话代码编译的速度也会慢,代码冗余太厉害,而且团队开发时候也不方便,不能独立的开发自己的一块,这个时候我们都会用到模块化开发,每一个module依赖都会成为单独运行的app,这样就方便了
//主要是使用阿里开发的Arouter路由开方便模块开发,因为我们在跳转的时候要是使用原生的Intent后期会比较混乱,如果使用Arouter路由就不会发生了
这里就介绍Arouter的使用了,读者可以自己查询一下资料
就想这样把所有跳转的路径配置到一个类中
/**
- 跳转路径的配置
/
public class MyTaskArouterConsts {
/*- 登录页面
/
public static final String USER_LOGIN = “/user/login”;
/* - 数据页面
*/
public static final String Data = “/user/data”;
}
- 登录页面
这是我们一个module中build.gradle的配置,这个baselib是最底层的依赖
如果想把我们的module变成独立的app的话就在全局的gradle.properties
里面配置mytask=true,这样还不够我们还得再main文件下新创建buildApp文件夹和buildmodule供我们加载
if (mytask.toBoolean()) {
apply plugin: ‘com.android.application’
} else {
apply plugin: ‘com.android.library’
}
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName “1.0”
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//组件资源前缀
resourcePrefix "user_"
sourceSets {
main {
if (mytask.toBoolean()) {
manifest.srcFile 'src/main/buildApp/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/buildModule/AndroidManifest.xml'
}
}
}
}
dependencies {
implementation fileTree(dir: ‘libs’, include: [’*.jar’])
implementation ‘com.android.support:appcompat-v7:26.1.0’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘com.android.support.test🏃1.0.2’
androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’
annotationProcessor ‘com.alibaba:arouter-compiler:1.1.4’
implementation project(’:baselib’)
}
最后在app的build.gradle中配置
apply plugin: ‘com.android.application’
apply plugin: ‘kotlin-android’
apply plugin: ‘kotlin-android-extensions’
android {
compileSdkVersion 26
defaultConfig {
applicationId “com.example.drcomponentdemo”
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName “1.0”
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
}
dependencies {
implementation fileTree(dir: ‘libs’, include: [’*.jar’])
implementation “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version”
implementation ‘com.android.support:appcompat-v7:26.1.0’
implementation ‘com.android.support.constraint:constraint-layout:1.1.3’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘com.android.support.test🏃1.0.2’
androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’
annotationProcessor ‘com.alibaba:arouter-compiler:1.1.4’
implementation project(’:baselib’)
if (!myhome.toBoolean()) {
api project(’:myhome’)
}
if (!mytask.toBoolean()) {
api project(’:mytask’)
}
}
//我们都知道一个app只能有一个Application这个解决方案在代码中有解决有需要的评论区发邮箱就行,我会单独吧代码给你