赞
踩
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
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.xxx.xxx</groupId> <artifactId>xxx-module</artifactId> <version>1.0.1-SNAPSHOT</version> <packaging>aar</packaging> <dependencies> <dependency> <groupId>com.android.support</groupId> <artifactId>appcompat-v7</artifactId> <version>28.0.0</version> </dependency> <dependency> <groupId>com.trello</groupId> <artifactId>rxlifecycle</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.trello</groupId> <artifactId>rxlifecycle-components</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>glide</artifactId> <version>3.7.0</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-rxjava</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-gson</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.10.0</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>logging-interceptor</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>org.greenrobot</groupId> <artifactId>greendao</artifactId> <version>3.2.2</version> </dependency> </dependencies> </project>
上传到仓库之后,可以检查一下对应的pom文件是否含有第三方库的依赖。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。