首先粘贴源码地址,欢迎frok,欢迎start
源码地址
目前,Android 组件化普遍使用于移动开发,但是组件化的初衷是为了解耦代码,并行开发效率;小型app似乎会care不到,完全解耦的组件化会在app越来越臃肿的时候带来很大的提升;
ok,那么我们需要知道完全解耦的组件化框架应该注意哪些点:
2.1. 首先在整个工程的gradle.properties中配置组件 Library/Application切换的开关:
isRunLogin = false //login组件
isRunHome = false //home组件
isRunNews = false //news组件
2.2. 由于android中Library(组件)/Application切换时的差异,需要单独配置主见
以home组件为例:
首先开build.gradle:
//注释1: 配置切换application/Library的打包
if (isRunHome.toBoolean()){
apply plugin: 'com.android.application'
}else{
apply plugin: 'com.android.library'
}
android {
.......
sourceSets {
//注释2: Library/Application切换 AndroidManifest
main {
if (isRunLogin.toBoolean()){
manifest.srcFile 'src/main/AndroidManifest.xml'
}else{
manifest.srcFile 'src/main/manifest/AndroidManifest.xml'
}
}
}
......
}
......
//集成编译,打包为Library
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="component.android.com.home">
<application android:theme="@style/home_AppTheme">
<activity android:name=".view.activity.HomeActivity"/>
</application>
</manifest>
//单独编译,打包为单独Application 可单独编译
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="component.android.com.home">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".global.HomeApp"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/home_AppTheme">
<activity android:name=".view.activity.HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3.1. 组件之间的activity跳转,这里使用ARouter
ARouter是阿里开源的一种页面跳转task
首先看ARouter在build.gralde的配置:
//主app build.gradle
......
dependencies {
......
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
//home/login/news 组件 build.gradle
dependencies {
........
annotationProcessor 'com.alibaba:arouter-compiler:1.1.4'
}
//common build.gradle
dependencies {
......
api 'com.alibaba:arouter-api:1.3.1'
// arouter-compiler 的注解依赖需要所有使用 ARouter 的 module 都添加依赖
annotationProcessor 'com.alibaba:arouter-compiler:1.1.4'
}
3.2 在app和各组件中进行page跳转
public class MainApplication extends BaseApp {
@Override
public void onCreate() {
super.onCreate();
//在主app中初始化ARouter
initRouter();
initMoudleApp(this);
initMoudleData(this);
}
private void initRouter() {
if (BuildConfig.DEBUG){
//打印日志
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
}
@Override
public void initMoudleApp(Application application) {
for (String moduleApp : AppConfig.moduleApps) {
try {
Class clazz = Class.forName(moduleApp);
BaseApp baseApp = (BaseApp) clazz.newInstance();
baseApp.initMoudleApp(this);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
@Override
public void initMoudleData(Application application) {
for (String moduleApp : AppConfig.moduleApps) {
try {
Class clazz = Class.forName(moduleApp);
BaseApp baseApp = (BaseApp) clazz.newInstance();
baseApp.initMoudleData(this);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
}
//组件中 home--HomeApplication
public class HomeApp extends BaseApp {
@Override
public void initMoudleApp(Application application) {
if (BuildConfig.DEBUG){
//打印日志
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
}
@Override
public void initMoudleData(Application application) {
}
}
这里需要注意一下 当集成编译时候 ,组件仅仅是一个组件,不会单独具备Applicagtion入口,所以需要在主app的MainApplication中利用反射的方式 initMoudleData/initMoudleData进行ARouter等初始化的配置;
下面看ARouter的跳转实例:
//app/MainActivity
....
private void initClick() {
findViewById(R.id.btn_nav_home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ARouter.getInstance().build("/home/homeActivity").navigation()
}
}
在app中实现跳转,但是这个 path/home/homeActivity
需要在home组件目标位置添加注解才能实现activity的跳转:
@Route(path = "/myhome/homeActivity")
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
这样 就成功实现类组件之间的activity的跳转;
App点击跳转home,须判断登录逻辑:
//ILoginService
public interface ILoginService {
boolean getLoginStatus();
int getLoginUserId();
}
//DefultLoginService
public class DefultLoginService implements ILoginService {
@Override
public boolean getLoginStatus() {
return false;
}
@Override
public int getLoginUserId() {
return 0;
}
}
//ComponentServiceFactory
public class ComponentServiceFactory {
......
public static ComponentServiceFactory getInstance(Context context){
if (instance == null){
synchronized (ComponentServiceFactory.class){
if (instance == null){
instance = new ComponentServiceFactory();
}
}
}
return instance;
}
private ILoginService loginService;
public void setLoginService(ILoginService iloginService){
loginService = iloginService;
}
public ILoginService getLoginService(){
if (loginService == null){
loginService = new DefultLoginService();
}
return loginService;
}
}
然后在login中通过common的ComponentServiceFactory注册对应的loginService
//LoginService
public class LoginService implements ILoginService {
@Override
public boolean getLoginStatus() {
return AccountUtils.getInstance().isAccountStatus();
}
@Override
public int getLoginUserId() {
return 0;
}
}
//Login组件LoginApp注册service
........
public class LoginApp extends BaseApp {
@Override
public void initMoudleApp(Application application) {
Log.i("LoginApp","initMoudleApp");
if (BuildConfig.DEBUG){
//打印日志
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
ComponentServiceFactory.getInstance(this).setLoginService(new LoginService());
}
@Override
public void initMoudleData(Application application) {
}
}
在App-MainActivity中跳转homeActivity,在home组件中使用ARouter的拦截器:
//app-mainActivity
private void initClick() {
findViewById(R.id.btn_nav_home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ARouter.getInstance().build("/home/homeActivity").navigation(MainActivity.this, new NavCallback() {
//ARouter拦截器的监听
@Override
public void onArrival(Postcard postcard) {
LogUtils.LogI("loginInterceptor","done");
}
@Override
public void onFound(Postcard postcard) {
//super.onFound(postcard);
LogUtils.LogI("loginInterceptor","found");
}
@Override
public void onLost(Postcard postcard) {
//super.onLost(postcard);
LogUtils.LogI("loginInterceptor","lost");
}
@Override
public void onInterrupt(Postcard postcard) {
//super.onInterrupt(postcard);
LogUtils.LogI("loginInterceptor","interrupt");
}
});
}
});
}
//home-HomeInterceptor
@Interceptor(priority = 1,name = "homeInterceptor")
public class HomeInterceptor implements IInterceptor {
private Context context;
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
switch (postcard.getPath()){
case "/myhome/homeActivity":
//通过component进行逻辑交互
if (ComponentServiceFactory.getInstance(context).getLoginService().getLoginStatus()){
callback.onContinue(postcard);
}else {
ARouter.getInstance().build("/login/loginActivity").navigation();
//callback.onInterrupt(new RuntimeException("请登录"));
//callback.onContinue(postcard);
}
break;
default:
callback.onContinue(postcard);
break;
}
}
@Override
public void init(Context context) {
this.context = context;
}
}
//login-loginInterceptor
@Interceptor(priority = 2,name = "loginInterceptor")
public class LoginInterceptor implements IInterceptor {
private Context context;
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
switch (postcard.getPath()){
case "/login/loginActivity":
LogUtils.LogI("loginInterceptor","请点击登录按钮");
callback.onContinue(postcard);
break;
default:
callback.onContinue(postcard);
//在每一个组件中添加一个navi的拦截器 逻辑在
}
}
@Override
public void init(Context context) {
this.context = context;
}
}
可以看到 app 通过ARouter跳home home通过component的注册分发,判断登录逻辑 进行下一步跳转;这样就实现了不依赖其他组件的逻辑交互
在android中我们使用最多的就是fragment,一般情况下 我们会实例化fragment再进行下一步逻辑;为了解耦我们在component中注册fragment接口,在相应组件中注册fragmentservice,在其他组件中实现分发:
//component-LoginFragmentService
public class LoginFragmentService implements IFragmentService {
@Override
public Fragment getFragment(String tag) {
return new LginHomeFragment();
}
@Override
public void newFragment(Activity activity, int resId, FragmentManager fragmentManager, Bundle bundle, String tag) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(resId,new LginHomeFragment(),tag);
transaction.commit();
}
}
//component-ComponentServiceFactory
public class ComponentServiceFactory {
private static volatile ComponentServiceFactory instance;
private IFragmentService newsFragmentService;
private IFragmentService homeFragmentService;
private IFragmentService loginFragmentService;
public static ComponentServiceFactory getInstance(Context context){
if (instance == null){
synchronized (ComponentServiceFactory.class){
if (instance == null){
instance = new ComponentServiceFactory();
}
}
}
return instance;
}
........
//主册fragmentservice入口
public void setHomeFragmentService(IFragmentService iFragmentService){
homeFragmentService = iFragmentService;
}
public void setLoginFragmentService(IFragmentService iFragmentService){
loginFragmentService = iFragmentService;
}
public void setNewsFragmentService(IFragmentService iFragmentService){
newsFragmentService = iFragmentService;
}
public IFragmentService getNewsFragmentService() {
return newsFragmentService;
}
public IFragmentService getHomeFragmentService() {
return homeFragmentService;
}
public IFragmentService getLoginFragmentService() {
return loginFragmentService;
}
}
在home组件中进行fragmentservice的注册工作:
//home-HomeApp
public class HomeApp extends BaseApp {
@Override
public void initMoudleApp(Application application) {
if (BuildConfig.DEBUG){
//打印日志
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
ComponentServiceFactory.getInstance(this).setHomeFragmentService(new HomeFragmentService());
}
@Override
public void initMoudleData(Application application) {
}
}
在App中调用:
app-MainActivity:
private void initBaseView() {
FragmentManager supportFragmentManager = getSupportFragmentManager();
ComponentServiceFactory.getInstance(this)
.getHomeFragmentService().newFragment(this,R.id.content,supportFragmentManager,null,null);
}
这样一个home组件中的homeFragment就加载到主app的xml中
同理组件之间的fragment引用亦如此
注意框架中component-IFragmentService实现了两个方法:
//获取目标的fragment来进行操作
Fragment getFragment(String tag);
//用于固定的区域来填充相应fragment
void newFragment(Activity activity, int resId, FragmentManager fragmentManager, Bundle bundle, String tag);
getFragment(String tag);
为获取目标fragment接口,获取到实例之后开发者自己实现fragment相关逻辑newFragment(...);
适用于在布局中静态添加fragment,一步到位当然框架中还有部分限制组件资源的gradle配置,有兴趣可以在github下载demo
源码地址
简书地址
感谢阅读 欢迎github star