如果模塊化開發(fā)中遇到
多模塊的AndroidManifest.xml沒有合并
or
多模塊的資源文件沒有合并
or
模塊A include了模塊B咒吐,而無法使用模塊B內(nèi)依賴的其他aar包中的類的時候
or
提示Support包版本不一致
這篇文章可能就是你要的解決方案~
舉個栗子:
比如我們現(xiàn)在有一個App模塊設計為:
主工程: app
模塊: ui , framework
引入模塊的方式:在settings.gradle中顷帖,指定正確的模塊路徑
include ':app', ':framework', ':ui'
project(':framework').projectDir = new File('../framework')
project(':ui').projectDir = new File('../ui')
如果現(xiàn)在framework引入了一些依賴庫,假設如下:
// Retrofit 網(wǎng)絡框架依賴
implementation "com.squareup.retrofit2:retrofit:2.5.0"
// Gson 依賴
implementation 'com.google.code.gson:gson:2.8.5'
// ARouter解耦框架
implementation 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
如果這樣寫的話渤滞,主工程app中將無法調(diào)用到這些依賴庫中的類。
因為implementation聲明的依賴只能在本module模塊內(nèi)使用,跨module使用就要使用api聲明(其實就是曾經(jīng)的compile榴嗅,但是如果設置成compile妄呕,AS又要給你叫叫叫)!嗽测!改成如下即可
// Retrofit 網(wǎng)絡框架依賴
api "com.squareup.retrofit2:retrofit:2.5.0"
// Gson 依賴
api 'com.google.code.gson:gson:2.8.5'
// ARouter解耦框架
api 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
同理绪励,這是模塊里引用別的aar肿孵,這樣配置完了,那么緊接著需要在依賴framework的app模塊中疏魏,
在build.gradle的依賴處加入對應的引入哈停做。
api project(":framework")
當然如果全部做完,由于很多aar里使用了不一樣的support版本大莫,一定會提示版本不兼容了(其他aar不兼容的解決方案一樣哈)蛉腌,在主工程app的build.gradle中指定:
android {
.....
.....
//指定jdk版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// 如果提示多個重復文件,加這屬性
packagingOptions {
exclude 'META-INF/proguard/androidx-annotations.pro'
}
}
// 用于指定所有aar引用同樣版本的support包
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'
}
}
}
}