当前位置:   article > 正文

maven publish aar 打包第三依赖方法_aar打包第三方依赖库

aar打包第三方依赖库

问题描述

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'
}
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178

在执行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>
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

上传到仓库之后,可以检查一下对应的pom文件是否含有第三方库的依赖。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/331390
推荐阅读
  

闽ICP备14008679号