模块化开发是为了实现解耦,加速打包,模块调试等
那怎么解决各模块都有application的问题
可以在项目根路径下建一个config.gradle 进行一些通用化配置
ext {
// 控制是否是组件模式
isComponent = false
// 控制是否是开发模式,用于区分各功能模块是集成aar包,还是本地代码
isDev = true
appId = "driver"
ndkVersion = "21.1.6352462"
compileSdkVersion = 30
minSdkVersion = 19
targetSdkVersion = 30
buildToolsVersion = "30.0.3"
// 组件信息
componentsInfo = [
"tocXXX" : [
appId : "com.example.component.tocxxx",
versionCode: 1,
versionName: "1.0.0",
prefix : "tocxxx_"
]
]
//依赖信息
dependencies = [
"appcompat-v7" : 'androidx.appcompat:appcompat:1.2.0',
"cardview-v7" : 'androidx.cardview:cardview:1.0.0',
"annotations" : 'androidx.annotation:annotation:1.1.0',
"constraint" : 'androidx.constraintlayout:constraintlayout:2.0.4',
"work-runtime" : 'androidx.work:work-runtime:2.4.0',
"multidex" : "androidx.multidex:multidex:2.0.0",
"gson" : "com.google.code.gson:gson:2.7",
"flexbox" : "com.google.android:flexbox:1.0.0",
]
}
依赖信息根据自己的需要添加,在应用的build.gradle中进行配置
各模块的build.gradle根据config.gradle中的参数进行一些统一设置,
并根据isComponent判断该大致如下,如果是library就不会有application了 ,打整包的时候需要是library。如果是
if (rootProject.ext.isComponent) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
def componentInfo = rootProject.ext.componentsInfo['tocXXX']
android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode componentInfo.versionCode
versionName componentInfo.versionName
multiDexEnabled true
// 设置资源前缀,避免资源冲突问题
resourcePrefix componentInfo.prefix
// 引入ARouter 的 Annotation处理器
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
packagingOptions {
exclude 'META-INF/proguard/androidx-annotations.pro'
exclude 'META-INF/kotlinx-serialization-json.kotlin_module'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// implementation project(':BuResLib')
annotationProcessor rootProject.ext.dependencies["arouter-compiler"]
implementation rootProject.ext.dependencies["flexbox"]
if (rootProject.ext.isDev) {
api project(':SlBaseLib')
// implementation project(':FaceComponent')
} else {
api rootProject.ext.dependencies["sf-tbp-slbase-lib"]
implementation rootProject.ext.dependencies["sf-tbp-face"]
}
}
把项目初始化要完成的工作工具类沉在功能组件中,提供各sdk等初始化工作的调用方法。
完成各类初始化后要在调用方法里面把application设置进去
Init.init(application);
在应用入口的application类中设置
在manifest中设置