Android 组件化开发的意义何在?,androidui 开发框架
gradle.properties 文件设置切换开关
application 和 library 切换开关
isLoginApplication=trueisMeApplication=true
主应用动态添加依赖
//不要忘记 toBoolean()if (!isLoginApplication.toBoolean()) {implementation project(':loginmodule')}if (!isMeApplication.toBoolean()) {implementation project(':memodule')}implementation project(':modulelibrary')
子应用切换 application 和 library、动态设置 applicationId、动态设置 AndroidManifest.xml、添加基础依赖。以 login 模块为例
//1.动态设置 application 或 libraryif (isLoginApplication.toBoolean()){apply plugin: 'com.android.application'}else{apply plugin: 'com.android.library'}
android {compileSdkVersion compile_Sdk_Version.toInteger()
defaultConfig {//2.library 不存在 applicationIdif (isLoginApplication.toBoolean()){applicationId "com.syw.loginmodule"}minSdkVersion min_Sdk_Version.toInteger()targetSdkVersion target_Sdk_Version.toInteger()versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}
buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
// 3.library 状态下 AndroidManifest.xml 没有 application 信息,不设置启动 activitysourceSets{main{if (isLoginApplication.toBoolean()){manifest.srcFile 'src/main/AndroidManifest.xml'}else{manifest.srcFile 'src/main/manifests/AndroidManifest.xml'}}}}
dependencies {implementation fileTree(include: ['*.jar'], dir: 'libs')implementation 'com.android.support:appcompat-v7:28.0.0'implementation 'com.android.support.constraint:constraint-layout:2.0.2'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(':modulelibrary')}
依赖库的 manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.syw.loginmodule">
<application><activity android:name=".LoginActivity"></activity></application>
</manifest>
基础库设置
基础库设置接口,由子项目实现
以 login 模块为例,编写 ILoginService
public interface ILoginService {void launch(Context context);
Fragment getFragment(FragmentManager fragmentManager, int containerId, Bundle bundle);}
编写接口管理类
public class ServiceFactory {// 单例模式 private static final ServiceFactory instance=new ServiceFactory();public ServiceFactory(){}public static ServiceFactory getInstance() {return instance;}
private ILoginService iLoginService;private IMeService iMeService;
public ILoginService getLoginService() {return iLoginService;}
public void setLoginService(ILoginService iLoginService) {this.iLoginService = iLoginService;}
public IMeService getMeService() {return iMeService;}
public void setMeService(IMeService iMeService) {this.iMeService = iMeService;}}
子项目完成具体实现
public class LoginBridge implements ILoginService {@Overridepublic void launch(Context context) {Intent intent = new Intent(context, LoginActivity.class);context.startActivity(intent);}
@Overridepublic Fragment getFragment(FragmentManager fragmentManager, int containerId, Bundle bundle) {LoginFragment loginFragment=new LoginFragment();loginFragment.setFragmentData(bundle);fragmentManager.beginTransaction().add(containerId,loginFragment).commit();return loginFragment;}}
跳转方法的调用
跳转的具体实现已经完成,现在的问题是怎么调用,我们想要实现的是在 MainActivity 中通过如下代码调用
ServiceFactory.getInstance().getLoginService().launch(MainActivity.this);
那么现在的问题是,什么时候将 LoginService 设置到 ServiceFactory,刚开始的时候还在想,既然已经依赖了 loginmodule,为啥不直接跳转。仔细想想,这就违背了组件化的本意了,我们应该减少子项目和主项目之间的耦合,通过中间库的方式。为了调用 getLoginService,我们首先需要 setLoginService,将实现类设置到 ServiceFactory。于是就有了下面的方法
在子项目中创建一个方法,在主项目初始化的时候,通过主项目调用,子项目将实现类设置给 ServiceManager
我们首先需要在基础库创建一个接口,通过主项目调用,实现全部子应用设置 Service
public interface Appcompat {void initializa();}
我们首先创建一个类,实现该接口,设置 LoginService
public class LoginApplication extends Application implements Appcompat {@Overridepublic void initializa() {ServiceManager.getInstance().setLoginService(new LoginBridge());}
评论