首先可以先學習通過gradle的配置,去實現各個Module的可獨編譯,具體項目中各個Module的業務拆分和依賴問題,及基礎庫和公衆業務層的處理,還需要更多的業務實踐;
可以在項目的gradle.properties進行開關配置,也可以在build.gradle文件中處理;
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#
#
# 每個組件是否單獨編譯:true爲單獨編譯,false將作爲依賴的module編譯;
#
# compileSeparetelyHomeModule=true
compileSeparetelyHomeModule=false
# compileSeparetelyChatModule=true
compileSeparetelyChatModule=false
# compileSeparetelyRecomModule=true
compileSeparetelyRecomModule=false
# compileSeparetelyMeModule=true
compileSeparetelyMeModule=false
主項目的build.gradle中的配置如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
defaultConfig {
applicationId "com.windfallsheng.componentsimpleexample"
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation rootProject.ext.dependencies["junit"]
androidTestImplementation rootProject.ext.dependencies["test-runner"]
androidTestImplementation rootProject.ext.dependencies["test-espresso-core"]
implementation rootProject.ext.dependencies["appcompat-v7"]
implementation rootProject.ext.dependencies["design"]
if (!compileSeparetelyHomeModule.toBoolean()) {
implementation project(':module_home')
}
if (!compileSeparetelyChatModule.toBoolean()) {
implementation project(':module_chat')
}
if (!compileSeparetelyRecomModule.toBoolean()) {
implementation project(':module_recom')
}
if (!compileSeparetelyMeModule.toBoolean()) {
implementation project(':module_me')
}
implementation project(':lib_res')
implementation project(':lib_base')
implementation rootProject.ext.dependencies["arouter-api"]
annotationProcessor rootProject.ext.dependencies["arouter-compiler"]
}
在一個Module的build.gradle中的配置如下:
if (compileSeparetelyHomeModule.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
defaultConfig {
if (compileSeparetelyHomeModule.toBoolean()) {
applicationId "com.windfallsheng.modulehome"
}
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// 這個配置只能限制xml文件裏的資源,對於圖片資源還是通過命名區別;
resourcePrefix "home_"
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
/*java插件引入了一個概念叫做SourceSets,通過修改SourceSets中的屬性,可以指定哪些源文件
(或文件夾下的源文件)要被編譯,哪些源文件要被排除。*/
sourceSets {
main {
if (compileSeparetelyHomeModule.toBoolean()) {
manifest.srcFile 'src/main/module/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation rootProject.ext.dependencies["junit"]
androidTestImplementation rootProject.ext.dependencies["test-runner"]
androidTestImplementation rootProject.ext.dependencies["test-espresso-core"]
implementation rootProject.ext.dependencies["appcompat-v7"]
annotationProcessor rootProject.ext.dependencies["arouter-compiler"]
implementation rootProject.ext.dependencies["eventbus"]
implementation project(':lib_base')
implementation project(':lib_res')
// 方式一:
// if(mode_debug){
// implementation project(':lib_debug')
// }else{
// implementation project(':lib_release')
// }
// 方式二:
// debugImplementation project(':lib_debug')
// releaseImplementation project(':lib_release')
}
在每個Module中添加另一個獨立編譯時使用的AndroidManifest.xml,保證獨立編譯時有啓動的Activity等;
阿里Arouter的使用可以參考官方的文檔說明,不再贅述。
由於作者水平有限,語言描述及代碼實現中難免有紕漏,望各位看官多提寶貴意見!
Hello , World !
感謝所有!