组件化gradle build优化
- 关于组件化,大家可以看我之前的文章,Android组件化开发实战
- 以前的library和application之间切换的通常写法是我们会创建一个common.gradle,之后去创建一个boolean类型的开关
if (rootProject.ext.isApplication) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
if (rootProject.ext.isApplication) {
applicationId "com.peakmain.modulea"
} else {
}
}
sourceSets {
main {
//需要在目录main目录下新建两个目录分别为debug和release
if (rootProject.ext.isApplication) {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/release/AndroidManifest.xml'
java {
//model的时候移除无用的包
exclude 'debug/**'
}
}
}
}
}
dependencies {
..
}
- 组件与组件之间跳转:使用Router
上面的代码有毛病吗?没有毛病,如果有多个model每个都这么设置,有问题吗?也没有问题。但是当我们的model超过十几个的时候,每个都需要去这么设置就很很折磨人了。那如果我们所有module都只需要一行代码是不是很舒服?
common.gradle代码
这里方便大家借鉴,我直接贴代码
project.ext {
// 模块是否单独运行调试
isModuleDebug = false
minSdkVersion = 28
targetSdkVersion = 30
versionCode = 1
versionName = "1.0"
compileSdkVersion = 30
applicationId = "com.peakmain.basiclibrary"
libs = [
"appcompat" : "androidx.appcompat:appcompat:1.2.0",
"kotlin-stdlib" : "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version",
"ktx" : "androidx.core:core-ktx:1.3.1",
"material" : "com.google.android.material:material:1.2.1",
"constraintlayout": "androidx.constraintlayout:constraintlayout:2.0.1",
"arouter" : "com.alibaba:arouter-api:1.5.1"
]
apts = [
"arouter_compiler": "com.alibaba:arouter-compiler:1.5.1",
]
// 专门用来设置application module的
setAppDefaultConfig = {
project.apply plugin: "com.android.application"
project.apply plugin: "kotlin-android"
project.apply plugin: 'kotlin-kapt'
setAndroidConfig project.android
setDependencies project.dependencies
}
// 专门为library情况下进行配置
setLibDefalutConfig = {
project.apply plugin: "com.android.library"
project.apply plugin: "kotlin-android"
project.apply plugin: 'kotlin-kapt'
setAndroidConfig project.android
setDependencies project.dependencies
}
setAndroidConfig = {
android ->
android.compileSdkVersion project.compileSdkVersion
android.defaultConfig {
minSdkVersion project.minSdkVersion
targetSdkVersion project.targetSdkVersion
versionCode project.versionCode
versionName project.versionName
if (project.name == 'app') {
applicationId project.applicationId
} else if (project.isModuleDebug) {
applicationId project.applicationId
applicationIdSuffix = project.name
}
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//java项目
/* javaCompileOptions {
annotationProcessorOptions {
// 路由每个模块的名称
arguments += [AROUTER_MODULE_NAME: project.name]
}
}*/
//kotlin项目
kapt {
arguments {
arg("AROUTER_MODULE_NAME", project.getName())
}
}
}
android.buildTypes {
release {
minifyEnabled false
proguardFiles android.getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
android.compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
android.kotlinOptions {
jvmTarget = '1.8'
}
android.sourceSets {
main {
if (project.name != 'app') {
if (project.isModuleDebug) {
manifest.srcFile "src/main/debug/AndroidManifest.xml"
} else {
manifest.srcFile "src/main/release/AndroidManifest.xml"
}
}
}
}
}
setDependencies = {
dependencies ->
delegate = dependencies
project.libs.each {
k, v -> implementation v
}
project.apts.each {
k, v ->
kapt v
// annotationProcessor v
}
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
if (project.name == 'app' && !project.isModuleDebug) {
implementation project(':loginmodel')
implementation project(':library')
}
}
}
if (project.name == 'app' || isModuleDebug) {
project.setAppDefaultConfig project
} else {
project.setLibDefalutConfig project
}
之后使用,大家只需要直接一行代码就可以了,比如app的model
apply from:"${rootProject.rootDir}/common.gradle"
而对于添加model,大家可以去common.gradle,如果自己model个性化的,可以直接在自己model按以前设置即可,是不是很方便呢
注意:如果大家在使用arouter的时候,提示以下错误
请在gradle.properties中添加以下代码
android.enableJetifier=true