aar打包到maven库之后,引用的时候发现报错,具体原因是aar包中的第三方库的类没有找到。
通过maven库引用aar包,之所以可以引用第三方依赖库,是因为同时打包生成的pom文件中包含了对第三方库的依赖,如果pom文件中没有对应依赖,则在使用方会报ClassNotFound异常。
既然知道了造成问题的原因,方案就很简单了,在生成pom文件的时候同时打包对应的依赖就好了,核心代码就是pom.withXml(),这段代码的原理就是在pom.xml中插入dependencies中的引用。
demo.gradle
publishing {
repositories {
maven {
url mavenConfig.snapshots.url
credentials {
username = mavenConfig.snapshots.username
password = mavenConfig.snapshots.password
}
}
}
publications {
xxx_module(MavenPublication) {
artifact "build/outputs/aar/xxx_module-release.aar"
groupId mavenConfig.groupId// groupId
artifactId xxxModule.artifactId // artifactId
version xxxModule.versionName
description xxxModule.descrp
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each {
println 'it.name='+it.name+" it.group="+it.group
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
println 'it.name='+it.name
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
configurations.api.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
configurations.implementation.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
if (getGradle().startParameter.toString().contains("assembleDebug")) {
configurations.debugCompile.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
configurations.debugApi.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
configurations.debugImplementation.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
} else {
configurations.releaseCompile.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
configurations.releaseApi.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
configurations.releaseImplementation.allDependencies.each {
if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.group == groupId && project.getRootProject().aarMap.contains(it.name)) {
dependencyNode.appendNode('type', 'aar')
}
}
}
}
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation ('com.android.support:appcompat-v7:28.0.0'){
exclude group: 'com.android.support'
}
//rx-android-java
implementation 'com.trello:rxlifecycle:1.0'
implementation 'com.trello:rxlifecycle-components:1.0'
/*rotrofit*/
//gson
implementation 'com.google.code.gson:gson:2.8.2'
//glide
implementation 'com.github.bumptech.glide:glide:3.7.0'
//retrofit
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
//okhttp
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.1.2'
//greendao
implementation 'org.greenrobot:greendao:3.2.2'
}
在执行Task,generatePomFileForXXX_modulePublication之后,在build/publications/xxx_module/文件夹中,会生成pom_default.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0modelVersion>
<groupId>com.xxx.xxxgroupId>
<artifactId>xxx-moduleartifactId>
<version>1.0.1-SNAPSHOTversion>
<packaging>aarpackaging>
<dependencies>
<dependency>
<groupId>com.android.supportgroupId>
<artifactId>appcompat-v7artifactId>
<version>28.0.0version>
dependency>
<dependency>
<groupId>com.trellogroupId>
<artifactId>rxlifecycleartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>com.trellogroupId>
<artifactId>rxlifecycle-componentsartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>com.github.bumptech.glidegroupId>
<artifactId>glideartifactId>
<version>3.7.0version>
dependency>
<dependency>
<groupId>com.squareup.retrofit2groupId>
<artifactId>adapter-rxjavaartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>com.squareup.retrofit2groupId>
<artifactId>retrofitartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>com.squareup.retrofit2groupId>
<artifactId>converter-gsonartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>okhttpartifactId>
<version>3.10.0version>
dependency>
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>logging-interceptorartifactId>
<version>3.1.2version>
dependency>
<dependency>
<groupId>org.greenrobotgroupId>
<artifactId>greendaoartifactId>
<version>3.2.2version>
dependency>
dependencies>
project>
上传到仓库之后,可以检查一下对应的pom文件是否含有第三方库的依赖。