• 组件化架构搭建——铺路Android架构师


    组件化定义

    组件化是建立在模块化思想上的一次演进,一个变种。组件化本来就是模块化的概念。核心是模块角色的可转化换性,在打包时,是library;调试时,是application。

    组件化的单位是组件,这里跟模块化的一个最大的区别就是组件之间是可以相互进行跳转的,其实现就是通过路由,这个在之后会手动来实现这个路由的功能。它是一个编译时的行为

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NdJc2F4N-1656942017486)(https://www.freesion.com/images/332/53de17d54ffd657e36de0eb919300d9c.png)]

    组件化优点:

    • 可重用
    • 代码简洁
    • 易测试

    为什么使用组件化

    一直使用单工程操作,项目越来越大导致出现了不少的问题:

    1. 查找问题慢:定位问题,需要在多个代码混合的模块中寻找和跳转。
    2. 开发维护成本增加:避免代码的改动影响其它业务的功能,导致开发和维护成本不断增加。
    3. 编译时间长:项目工程越大,编译完整代码所花费的时间越长。
    4. 开发效率低:多人协作开发时,开发风格不一,又很难将业务完全分割,大家互相影响,导致开发效率低下。
    5. 代码复用性差:写过的代码很难抽离出来再次利用。

    组件化项目整体结构

    在这里插入图片描述

    COMPONENTLIB2 LIBRARY项目结构

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ye9VtoLd-1656942017493)(https://www.freesion.com/images/488/c074c5e85663d6800344475dedbe5c00.png)]

    BUILD.GRADLE

    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 28
    
    
    
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
        implementation 'com.android.support:appcompat-v7: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'
    }
    
    1234567891011121314151617181920212223242526272829303132333435
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    APPCONFIG

    package com.example.componentlib2;
    
    public class AppConfig {
        public static final String[] COMPONENTS = {
                "com.example.logincomponent.LoginApplication",
                "com.example.minecomponent.MineApplication"
        };
    }
    
    123456789
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    IAPPCOMPONENT

    package com.example.componentlib2;
    
    import android.app.Application;
    
    public interface IAppComponent {
        void initalliza(Application app);
    }
    
    12345678
    package com.example.componentlib2;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    
    public interface ILoginService {
    
        // 跳转 从主 app  跳转到 login app
        void launch(Context context, String targetClass);
    
        //获取数据主app从login app 获取返回数据并显示
        Fragment newUserInfoFragment(FragmentManager fragmentManager, int viewId, Bundle bundle);
    
    }
    
    1234567891011121314151617
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    IMINESERVICE

    package com.example.componentlib2;
    
    import android.content.Context;
    
    public interface IMineService {
        void launch(Context context,int userId);
    }
    
    12345678
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    SERVICEFACTORY

    package com.example.componentlib2;
    
    public class ServiceFactory {
        private static final ServiceFactory instance = new ServiceFactory();
    
        public static ServiceFactory getInstance() {
            return instance;
        }
    
        private ServiceFactory() {
    
        }
    
        private ILoginService mLoginService;
        private IMineService mMineService;
    
    
        public ILoginService getLoginService() {
            return mLoginService;
        }
    
        public void setLoginService(ILoginService mLoginService) {
            this.mLoginService = mLoginService;
        }
    
        public IMineService getMineService() {
            return mMineService;
        }
    
        public void setMineService(IMineService mMineService) {
            this.mMineService = mMineService;
        }
    }
    
    12345678910111213141516171819202122232425262728293031323334
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    LOGINCOMPONENT 组件

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GEmfRmH0-1656942017495)(https://www.freesion.com/images/593/9426c226f86f4afebd11bec1e732f689.png)]

    LOGINACTIVITY

    package com.example.logincomponent;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class LoginActivity extends AppCompatActivity {
    
        public static String EXTRA_TARGET_CLASS = "extra_target_class";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login_main);
        }
    }
    
    123456789101112131415
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    LOGINAPPLICATION

    package com.example.logincomponent;
    
    import android.app.Application;
    
    import com.example.componentlib2.IAppComponent;
    import com.example.componentlib2.ServiceFactory;
    
    public class LoginApplication extends Application implements IAppComponent {
    
        private static Application application;
    
        public static Application getApplication() {
            return application;
        }
    
        // 当 login 是 application 的时候 调用 initalliza 方法时,上下文指向的就是 LoginApplication
        @Override
        public void onCreate() {
            super.onCreate();
            initalliza(this);
        }
    
        //当 login是 lib 的时候 被主 App 调用被赋值
        @Override
        public void initalliza(Application app) {
            application = app;
            ServiceFactory.getInstance().setLoginService(new LoginService());
        }
    }
    
    123456789101112131415161718192021222324252627282930
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    LOGINSERVICE

    package com.example.logincomponent;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    
    import com.example.componentlib2.ILoginService;
    
    public class LoginService implements ILoginService {
    
        @Override
        public void launch(Context context, String targetClass) {
            Intent intent = new Intent(context, LoginActivity.class);
            intent.putExtra(LoginActivity.EXTRA_TARGET_CLASS, targetClass);
            context.startActivity(intent);
        }
    
        @Override
        public Fragment newUserInfoFragment(FragmentManager fragmentManager, int viewId, Bundle bundle) {
            UserInfoFragment fragment = new UserInfoFragment();
            fragment.setArguments(bundle);
            fragmentManager.beginTransaction().add(viewId, fragment).commit();
            return fragment;
        }
    }
    
    12345678910111213141516171819202122232425262728
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    USERINFOFRAGMENT

    package com.example.logincomponent;
    
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class UserInfoFragment extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.userinfo_fragment, null);
        }
    }
    
    12345678910111213141516171819
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    MINECOMPONENT 组件

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Yn1HBU28-1656942017496)(https://www.freesion.com/images/473/ca8f674109637f9da930c1ebf92c7ef9.png)]

    MINEACTIVITY

    package com.example.minecomponent;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MineActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mineactivity_main);
        }
    }
    
    1234567891011121314
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    MINEAPPLICATION

    package com.example.minecomponent;
    
    import android.app.Application;
    
    import com.example.componentlib2.IAppComponent;
    import com.example.componentlib2.ServiceFactory;
    
    public class MineApplication extends Application implements IAppComponent {
    
        private static Application application;
    
        public static Application getApplication() {
            return application;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            initalliza(this);
        }
    
        @Override
        public void initalliza(Application app) {
            application = app;
            ServiceFactory.getInstance().setMineService(new MineService());
        }
    }
    
    12345678910111213141516171819202122232425262728
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    MINESERVICE

    package com.example.minecomponent;
    
    import android.content.Context;
    import android.content.Intent;
    
    import com.example.componentlib2.IMineService;
    
    public class MineService implements IMineService {
        @Override
        public void launch(Context context, int userId) {
            Intent intent = new Intent(context, MineActivity.class);
            intent.putExtra("ID", userId);
            context.startActivity(intent);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    文末

    文章介绍了组件化,以及如何搭建好一个组件化。本文可能写的有点粗糙,有关组件化、热修复等进入架构师的框架搭建可以私信解答。

  • 相关阅读:
    杀戮空间2开服服务器架设教程UE3Redist
    简单宿舍管理系统(springboot+vue)
    华为防火墙基础自学系列 | 证书申请步骤
    高性能网络编程 - 解读5种I/O模型
    前端学习第三天-css基础
    SpringSecurity6 | 默认登录页
    MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
    拼搏半个月,刷了 571道Java高频面试题喜提阿里 offer
    《HelloGitHub》第 75 期
    JSR 303 以及 拦截器
  • 原文地址:https://blog.csdn.net/Androidxiaofei/article/details/125609219