安卓组件化开发

xiaoxiao2021-02-28  112

组件化开发的相关文章: Android 业务组件化开发实践 关于Android业务组件化的一些思考 组件化开发的参考例子: Modular

组件化

android工程的组件一般分为两种,lib组件和application组件 application组件是指该组件本身就可以运行并打包成apk lib组件是指该组件属于app的一部分,可以供其它组件使用但是本身不能打包成apk

为什么要有组件化?

加入一个app工程只有一个组件,随着app业务的壮大模块越来越多,代码量超10万是很正常的,这个时候我们会遇到以下问题

- 稍微改动一个模块的一点代码都要编译整个工程,耗时耗力 - 公共资源、业务、模块混在一起耦合度太高 - 不方便测试

组件化大大缩减了工程结构直接降低了编译时间

代码实现过程

新建一个项目ComponentTest新建New Module =>ComponentOne, ComponentTwo在gradle.properties中添加isDebug = true 在ComponentOne中进行如下修改: a.在main文件夹下新建debug和release两个文件夹,将AndroidManifest.xml移动到debug文件夹下,复制一份到release文件夹下。 b.修改release下的AndroidMainfest.xml 将 改为

debug不变,release修改的原因在于,当组件组合到主app中时,只能有一个启动入口 c.修改componentone下的build.gradle 去掉defaultConfig中的applicationId.

if(isDebug.toBoolean()){ apply plugin: 'com.android.application' }else{ apply plugin: 'com.android.library' }

上面代码的作用是:如果在isDebug = true,则组件是application,可以单独运行。如果isDebug = false,则组件变为library,可以被app module引入使用

resourcePrefix “name_”:这句话是为了避免R冲突

sourceSets{ main{ if(isDebug.toBoolean()){ manifest.srcFile 'src/main/debug/AndroidManifest.xml' }else{ manifest.srcFile 'src/main/release/AndroidManifest.xml' java{ exclude 'debug/**' } } } }

上面代码的作用是:debug模式使用debug下的AndroidManifest.xml,release模式下使用release下的AndoridManifest.xml 5. ComponentTwo同上修改 6. 修改app下的build.gradle,添加如下代码,引入项目依赖 7. 由于各个module都有自己的activity,如果要实现相互跳转,需要采用隐式跳转,因此我们可以写一个新的module=》router,充当路由 a.创建一个module名字为Router,选择无Activity。 修改build.gradle为: b.新建ActivityRouter.java, FragmentRouter.java,RouterList.java 代码分别为:

package component.test.git.router; import android.content.Context; import android.content.Intent; public class ActivityRouter { public static void startActivity(Context context, String action) { context.startActivity(new Intent(action)); } public static void startActivity(Context context, Class clazz) { context.startActivity(getIntent(context, clazz)); } public static Intent getIntent(Context context, Class clazz) { return new Intent(context, clazz); } public static void startActivityForName(Context context, String name) { try { Class clazz = Class.forName(name); startActivity(context, clazz); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } } package component.test.git.router; import android.support.v4.app.Fragment; public class FragmentRouter { public static Fragment getFragment(String name) { Fragment fragment; try { Class fragmentClass = Class.forName(name); fragment = (Fragment) fragmentClass.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return fragment; } } package component.test.git.router; public class RouterList { public static final String COMPONENT_ONE = "component.test.git.componentone.comoneactivity"; public static final String COMPONENT_TWO = "component.test.git.componenttwo.comtwoactivity"; }

c.其他module通过:compile project(“:router”) 引入 8. app下在MainActivity中新建两个按钮,点击事件为:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ActivityRouter.startActivity(MainActivity.this, RouterList.COMPONENT_ONE); } }); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ActivityRouter.startActivity(MainActivity.this, RouterList.COMPONENT_TWO); } }); }

将gradle.properties中的isDebug改为false,运行app,即可实现各组件之间的跳转

ComponentOne和ComponentTwo就可以单独进行开发,而且互相不受影响,从而实现了结偶以及快速开发的目的

转载请注明原文地址: https://www.6miu.com/read-32253.html

最新回复(0)