组件化
ARouter gradle.properties
#配置全局的环境
compile_SdkVersion = 28
min_SdkVersion = 20
target_SdkVersion = 28
#配置组件是否单独运行
loginRunAlone = false
mineRunAlone = false
LoginComponent的Gradle配置
if (loginRunAlone.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
android {
compileSdkVersion compile_SdkVersion.toInteger()
defaultConfig {
if (loginRunAlone.toBoolean()) {
applicationId "com.example.logincomponent"
}
minSdkVersion min_SdkVersion.toInteger()
targetSdkVersion target_SdkVersion.toInteger()
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
//独立运行和集成
sourceSets {
main {
if (loginRunAlone.toBoolean()) {
manifest.srcFile "src/main/manifest/AndroidManifest.xml"
} else {
manifest.srcFile "src/main/AndroidManifest.xml"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':componentlib')
}
Main App的初始化
package com.example.ljp.zujian;
import android.app.Application;
import com.example.componentlib.AppCompat;
import com.example.componentlib.ServiceFactory;
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
for (String s : ServiceFactory.getServiceFactory().getAppComponent()) {
try {
Class c = Class.forName(s);
Object o = c.newInstance();
if (o instanceof AppCompat) {
((AppCompat) o).init(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Base Library的业务处理
package com.example.componentlib;
public class ServiceFactory {
private static ServiceFactory serviceFactory = new ServiceFactory();
private String[] appComponent = {
"com.example.logincomponent.LoginApplication",
"com.example.minecomponent.MineApplication"
};
private ILoginService loginService;
private IMineService mineService;
public static ServiceFactory getServiceFactory() {
return serviceFactory;
}
public IMineService getMineService() {
if (null == mineService) {
return new EmptyService();
}
return mineService;
}
public void setMineService(IMineService mineService) {
this.mineService = mineService;
}
public ILoginService getLoginService() {
if (null == loginService) {
return new EmptyService();
}
return loginService;
}
public void setLoginService(ILoginService loginService) {
this.loginService = loginService;
}
public String[] getAppComponent() {
return appComponent;
}
}
最后
如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。
如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。
欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓(文末还有ChatGPT机器人小福利哦,大家千万不要错过)
![](https://i-blog.csdnimg.cn/blog_migrate/3cb49c343c3f9e220355f8419ed9084d.jpeg)
PS:群里还设有ChatGPT机器人,可以解答大家在工作上或者是技术上的问题