本节完整代码可以前往github查看,项目地址:https://github.com/N0tExpectErr0r/Zhihu-Daily
文章目录:
Android组件化打造知乎日报系列(一)—— 项目架构搭建
Android组件化打造知乎日报系列(二)—— 主界面框架搭建
AppModule
AppModule是我们的壳工程,我们通过这个壳工程来将各个组件层的Module集合到一起。
在AppModule中我们只有一个空的Activity—LauncherActivity,这个Activity用于管理我们启动应用后的跳转。
public class LauncherActivity extends DailyBaseActivity {
@Override
protected int getContentViewId() {
return R.layout.app_activity_launcher;
}
@Override
protected void init(Bundle savedInstanceState) {
// 一些预处理及跳转
}
}
HomeModule
整体设计
HomeModule中展示了我们的主界面,是我们的第一个组件层的Module,我们的主界面决定采用底部栏TabLayout配合ViewPager的形式来实现。这里TabLayout使用了第三方库FlycoTabLayout。
build.gradle
由于这里是第一个组件层的Module,所以先给大家展示一下它的build.gradle,其他组件层的gradle大同小异,以后就不再赘述。
其中我们需要关注的有几点。
注释1处,根据是否是集成编译运用了不同的apply语句,使得该模块在非集成编译的时候以application的形式存在,可以单独编译。而在集成编译时则以library的形式存在,可以被AppModule所引用。
注释2处,如果是单独编译,则使用自己的Manifest。如果是集成编译,则使用App壳工程的Manifest。
注释3处,就像第一篇中说到的,BaseModule被我们的各个组件层的Module所引用。
// 1
if(isSingleBuild.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
android {
compileSdkVersion COMPILE_SDK_VERSION
defaultConfig {
minSdkVersion MIN_SDK_VERSION
targetSdkVersion TARGET_SDK_VERSION
versionCode VERSION_CODE
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
resourcePrefix "home_"
sourceSets{
main{
// 2
if(isSingleBuild.toBoolean()) {
manifest.srcFile '../home/src/main/AndroidManifest.xml'
} else {
manifest.srcFile '../app/src/main/AndroidManifest.xml'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs'