一.名词解释java
模块化:模块是APP的组成部分,全部的模块组装起来即是一个完整的APPandroid
组件化:使模块可以单独运行为组件化app
二.模块化与组件化的差异模块化
(1) 类型不一样组件化
模块:com.android.libray测试
组件:com.android.applicationgradle
(2)有无applicationIdui
模块:无spa
组件:有debug
(3)有无启动页
模块:无
组件:有
三.代码实现
思想思路,经过一个变量进行他们之间的差别化处理
a.建立配置文件
在于项目级build.gradle同级下下建立config.gradle
ext {
//是否为模块模式.
// isModule=true 模块模式
//isModule=false 组件模式
isModule = false;
android = [
compileSdkVersion: 26,
buildToolsVersion: "26.0.2",
minSdkVersion : 19,
targetSdkVersion : 26,
versionCode : 1,
versionName : "1.0"
]//
//模块名称 添加你的模块,用于模块和组件切换
appId = [
"app" : "yourAPPId",
]
//其余配置
}
b.引用config.gradle
在项目级别的build.gradle引用
apply from: "config.gradle"
c.差别处理
在模块级别的build.gradle中使用isModule变量进行控制
def appId = rootProject.ext.appId
def android = rootProject.ext.android
if (isModule) {
apply plugin: 'com.android.library'
} else {
apply plugin: 'com.android.application'
}
android {
compileSdkVersion android.compileSdkVersion
buildToolsVersion android.buildToolsVersion
defaultConfig {
minSdkVersion android.minSdkVersion
targetSdkVersion android.targetSdkVersion
versionCode android.versionCode
versionName android.versionName
if (!isModule) {
applicationId appId["activerepair"]
multiDexEnabled true //分包配置
ndk { abiFilters "armeabi" }//ndk配置
signingConfigs {
debug {
//测试keystore配置
}
release {
//正式
storeFile file("key地址")
storePassword '密码'
keyAlias '名称'
keyPassword '密码'
}
}
}
sourceSets {
main {
if (isModule) {
manifest.srcFile 'src/main/AndroidManifest.xml'
} else {
//组件下特有的文件
manifest.srcFile 'src/main/assembly/AndroidManifest.xml' //配置启动Activity
java.srcDirs 'src/main/assembly/java', 'src/main/java' //模块化下不须要打包的文件
}
}
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//your dependencies
}