在Android组件化的时候,需要把一个多module的库提交到maven,遇到了这个问题,记录一下方便以后查询
MuliteModuleLibDemo后面会放上去
问题
公共基础库提交到Maven的时候使用会遇到这个bug
Error:Failed to resolve:liba:unspecified
场景
/project
|
|--/lib-a
| |-- build.gradle
| |-- compile project(":lib-b")
| |-- apply from: "pushmaven.gradle"
|
|--/lib-b
| |-- build.gradle
|
|-- build.gradle
|--
然后我提交lib-a
./gradlew uploadArchives
然后去使用compile 'com.xxx.xxxx.lib-a'的时候就提示
Error:Failed to resolve:lib-b:unspecified
原因是因为maven没有把lib-b的代码一起打包进去
解决
- 把lib-b作为单独库上传
- 把lib-b打包到lib-a里面上传
方案一
- 在每个依赖库都引入
- apply from: "pom-evaluator.gradle"
- apply from: "pushmaven.gradle"
- 在根目录的gradle.properties控制groupid和version
- 在每个module的gradle.properties控制artifactId
备注: 如果觉得每个module都要写artifactId比较麻烦,可以不写,maven会自己使用module name,不过这要求你以后不改module name,不然会有问题
pom-evaluator.gradle:
把所有compile project的库都替换成groupid和version
afterEvaluate {
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = project.POM_GROUP_ID
pom.version = project.VERSION_NAME
pom.whenConfigured { pom ->
pom.dependencies.findAll { dep -> dep.groupId == rootProject.name }.collect { dep ->
dep.groupId = pom.groupId = project.POM_GROUP_ID
dep.version = pom.version = project.VERSION_NAME
}
}
}
}
}
}
如果module比较多,可以在根目录的build.gradle加入
configure(subprojects.findAll { !it.name.startsWith('app') }) {
apply from: "${rootDir}/pushmaven.gradle"
apply from: "${rootDir}/pom-evaluator.gradle"
}
优点:
- 对于原来的项目结构没变化
- 每个module都有版本控制
- 每个module都是相同的版本号
缺点:
每个module都有版本控制了,但是有些module我只是作为module拆分,不是用来作为基础库的
方案二
fat-aar会把每个moduel的资源、manifest、java文件等都合并到lib-a中
- 在你想要提交maven的依赖库引入
- apply from: 'https://raw.githubusercontent.com/adwiv/android-fat-aar/master/fat-aar.gradle'
- apply from: "../aar-evaluator.gradle"
- 把你不需要提交到maven的module这样改
compile project(":lib-b")
改成
embedded project(":lib-b")
aar-evaluator.gradle:
是把不要导入的module从pom去除,把它compile的库加入pom
afterEvaluate {
uploadArchives {
repositories {
mavenDeployer {
pom.whenConfigured { pomp ->
pomp.dependencies.findAll { dep -> dep.groupId == rootProject.name }.collect { dep ->
pomp.dependencies.remove(dep)
}
}
List<String> embedList = new ArrayList<>();
Map<String, ResolvedDependency> depList = new LinkedHashMap<>();
//List all embedded dependencies
configurations.embedded.allDependencies.each {
def depName = String.format("%s:%s", it.group, it.name)
embedList.add(depName);
}
//Collect all first level dependencies except embedded ones
configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each {
ResolvedDependency dep ->
def depName = String.format("%s:%s", dep.moduleGroup, dep.moduleName)
if (!embedList.contains(depName) && !depList.containsKey(depName)) {
depList.put(depName, dep)
}
}
//Collect all second level dependencies of embedded ones
configurations.embedded.resolvedConfiguration.firstLevelModuleDependencies.each {
//Second Level Depenencies
it.children.each {
ResolvedDependency dep ->
def depName = String.format("%s:%s", dep.moduleGroup, dep.moduleName)
if (!embedList.contains(depName) && !depList.containsKey(depName)) {
depList.put(depName, dep)
}
}
}
//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
def pomDefinition = asNode()
def dependenciesNode = pomDefinition.dependencies[0]
//Iterate over the compile dependencies, adding a <dependency> node for each
depList.values().each {
ResolvedDependency dep ->
def hasGroup = dep.moduleGroup != null
def hasName = (dep.moduleName != null || "unspecified".equals(dep.moduleName))
def hasVersion = dep.moduleVersion != null
if (hasGroup && hasName && hasVersion) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.moduleGroup)
dependencyNode.appendNode('artifactId', dep.moduleName)
dependencyNode.appendNode('version', dep.moduleVersion)
}
}
}
}
}
}
}
优点:
- 不需要把所有module都提交到maven
- 只需要管理一个依赖库
缺点
- fat-aar.gradle在com.android.tools.build:gradle:2.3.1下面有兼容问题
- 需要把所有compile project都改成embedded project
- fat-aar.gradle接入有风险
综上所述
个人项目可以使用方案二,但是企业项目我建议使用方案一pom-evaluator.gradle更保险安全
网友评论