组件化的好处
分模块打包 测试 统一版本管理
组件化可以不互相依赖,但可以互相交互,任意组合,高度结偶
phone module:新建出可以独立运行的模块
apply plugin: 'com.android.application'
它是有
applicationId: "com.example.gradle"
lib module:新建出不可以可以独立运行的模块
apply plugin: 'com.android.library'
没有applicationId
如果是app和lib切换的只需要改二个地方
第一:applicationId: "com.example.gradle"
第二:apply plugin: 'com.android.application'
那么什么是组件化和集成化呢?
我们看到上面的app和order都是module 都是可以直接运行的,这是组件化 ,而personal是一个lib 它就是集成化,它上面没有录色的点
可以看到这二个都可以独立运行.
上篇博客讲了gradle的一些语法
config.gradle
ext{
//false组件化 true集成化表示要打包测试上线
isRelease = false
androidId=[
compileSdkVersion:28,
buildToolsVersion:"29.0.0",
minSdkVersion: 15,
targetSdkVersion: 28,
versionCode: 1,
versionName: "1.0"
]
appId=[
app: "com.example.gradle",
order: "com.example.order",
personal: "com.example.personal",
]
url=[
debug : "https://www.baidu.com",
release : "https://qq.com"
]
supportLibrary = '28.0.0'
support=[
appcompat:"com.android.support:appcompat-v7:${supportLibrary}",
constraint: "com.android.support.constraint:constraint-layout:1.1.3"
]
}
是组件化还是集成主要是通过isRelease去判断的,现在看下build.gradle的配置
现在看下app中的build.gradle配置
apply plugin: 'com.android.application'
def androidId=rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.support
android {
compileSdkVersion androidId.compileSdkVersion
buildToolsVersion androidId.buildToolsVersion
defaultConfig {
applicationId appId.app
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug{
buildConfigField("Boolean","isRelease",String.valueOf(isRelease))
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation support.appcompat
implementation support.constraint
implementation project(":common")
if(isRelease){
implementation project(":order")
implementation project(":personal")
}
}
order是一个module build.gradle
//如果是集成环境 这个order就是一个library
if(isRelease){
apply plugin: 'com.android.library'
}else{
apply plugin: 'com.android.application'
}
def androidId=rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.support
android {
compileSdkVersion androidId.compileSdkVersion
buildToolsVersion androidId.buildToolsVersion
defaultConfig {
if(!isRelease){
applicationId appId.order
}
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation support.appcompat
implementation support.constraint
}
那么personal模块中的build.gradle是一样的,现在来切换下isRelease这个变量,
isRelease为true也就是集成化
如果是集成开发的话就是app可以运行,其他order personal都是lib,
现在把isRelease改为false 即是组件化开发
从图中看到app order personal都是module 都是可以直接运行或者打包成apk的,
从上面的图中也发现app order personal确实是三个module,这就是组件化和集成化的区别了
还可以通过打包的方式也能验证是三个apk
这样测试也可以按照模块去测试,而不需要打整个包了
注意:如果是创建了一个library,记得把在别地module中把res目录和AndroidManifest.xml拷贝过来,还有就是在src下写个activity进去,记得组件化开发中开发规范 src res下的类名和资源命名
组件化开发的临时代码 集成化打包动态隔离
如果ceo说想看下整个app目前开发到什么程度了,要打包成apk 在它手机上跑起来看看,这个是Android程序小周又在写其他功能,这些功能又没写完,但是又不想打包的时候把我这些类带上,怎么办呢?这个时候就知道gradle都牛逼了.
上面是小周整在开发的三个类A,B,C都没写完,这个时候你老大跑过来说要集成打包,小周在纳闷怎么做呢?google下小周找到了答案,
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//配置资源路径 方便测试环境,集成打包时不用到正式环境中
sourceSets {
main{
if(!isRelease){
println("我说组件化环境啊 大哥")
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
}else{
manifest.srcFile 'src/main/AndroidManifest.xml'
//release时 debug目录下文件不用合并到主工程中
java{
include '**/debug/**'
}
}
}
}
看看sourceSets下的配置记住它时和buildTypes是同级 ,personal模块修改的地方
如果isRelease为false就表示是组件化了,这个时候运行的是debug下的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.personal">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".debug.Debug_Personal_MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
就是Debug_Personal_MainActivity
这个就解决了组件化开发的临时代码 集成化打包动态隔离的问题
但是奇怪的是你personal module 这么做了,其他也要这么做,很麻烦,不然你集成跑不起来会报错
这是因为我有一个地方配置错了
buildConfigField("Boolean","isRelease",String.valueOf(isRelease))
应该是配置在defaultConfig节点下,而且子组件都要配置,