组件化
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;
}
}
网友评论