如果模块化开发中遇到
多模块的AndroidManifest.xml没有合并
or
多模块的资源文件没有合并
or
模块A include了模块B,而无法使用模块B内依赖的其他aar包中的类的时候
or
提示Support包版本不一致
这篇文章可能就是你要的解决方案~
举个栗子:
比如我们现在有一个App模块设计为:
主工程: app
模块: ui , framework
引入模块的方式:在settings.gradle中,指定正确的模块路径
include ':app', ':framework', ':ui'
project(':framework').projectDir = new File('../framework')
project(':ui').projectDir = new File('../ui')
- 1
- 2
- 3
如果现在framework引入了一些依赖库,假设如下:
// Retrofit 网络框架依赖 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'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
如果这样写的话,主工程app中将无法调用到这些依赖库中的类。
因为implementation声明的依赖只能在本module模块内使用,跨module使用就要使用api声明(其实就是曾经的compile,但是如果设置成compile,AS又要给你叫叫叫)!!改成如下即可
// Retrofit 网络框架依赖 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'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
同理,这是模块里引用别的aar,这样配置完了,那么紧接着需要在依赖framework的app模块中,
在build.gradle的依赖处加入对应的引入哈。
api project(":framework")
- 1
当然如果全部做完,由于很多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’
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
</div>
本文针对Android模块化开发中多模块的AndroidManifest.xml和资源文件未合并、模块无法使用依赖的aar包中的类、Support包版本不一致等问题,给出解决方案。如跨module使用依赖要用api声明,还需在依赖模块的build.gradle中加入对应引入,并指定所有aar引用同样版本的support包。
2534

被折叠的 条评论
为什么被折叠?



