当前位置:   article > 正文

gradle打包问题总结_fataar

fataar
1.解析pom文件,添加到指定pom中

apply plugin: 'maven'
apply plugin: 'maven-publish'

task sourcesJar(type: Jar) {
    classifier 'sources'
    from 'src/main/java'
}

def libDIr = "${rootProject.getRootDir()}/repo/com/xx/xx/core/0.3.0"

def getPropertyFromLocalProperties(key) {
    File file = project.rootProject.file('local.properties');
    //兼容WmPlugin的发布
    if(!file.exists()){
        file = project.rootProject.file('../local.properties');
    }
    if (file.exists()) {
        Properties properties = new Properties()
        properties.load(file.newDataInputStream())
        return properties.getProperty(key)
    }
}

def parseXml() {

}

afterEvaluate { project ->
    publishing {
        publications {
            debug(MavenPublication) {
                groupId = 'com.xx.xx'//公司域名
                artifactId = 'core'//该aar包的名称
                version = "0.1.0" //版本号

                artifact "${libDIr}/core-0.3.0.aar"


                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')

                    println "withXml path =  $libDIr"

                    def xmlSlurper = new XmlSlurper()
                    def pomTxt = xmlSlurper.parse("${libDIr}/core-0.3.0.pom")

                    println "pomTxt dependency size =  ${pomTxt.children().size()}"

                    pomTxt."dependencies".children().each { dependency ->

                        println "dependency: ${dependency.groupId} ${dependency.artifactId} ${dependency.version}"

                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dependency.groupId)
                        dependencyNode.appendNode('artifactId', dependency.artifactId)
                        dependencyNode.appendNode('version', dependency.version)

                        println "dependencyNode: ${dependencyNode}"
                    }
                }
            }
        }

        repositories {
            def user = getPropertyFromLocalProperties("username")
            def pwd = getPropertyFromLocalProperties("password")
            def repoUrl = getPropertyFromLocalProperties("repo_url")
            def repoName = getPropertyFromLocalProperties("repo_name")

            maven {
                name = repoName
                url = repoUrl
                credentials {
                    username user
                    password pwd
                }
            }
        }
    }
}
  • 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
2.子module依赖aar,父module直接调用子aar类的问题

有如下依赖关系:AModule -> BModule(包含xx.aar),
A的build.gradle

    api project(path: ':BModule')
  • 1

B的build.gradle(写法一):

    
android {
    repositories {
        flatDir {
            dirs 'libs','provideLibs' //this way we can find the .aar file in libs folder
        }
    }
}

dependencies {
  api(name: 'base-1.0.9', ext: 'aar')
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

B的build.gradle(写法二):

dependencies {
  //lib目录下都是必须的
  api fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
  api fileTree(include: ['*.jar', '*.aar'], dir: 'provideLibs')
}
  • 1
  • 2
  • 3
  • 4
  • 5

经过实验发现,只有写法二,AModule才可以引用到BModule中的aar的类,但是在写法一的基础上在AModule中也配置flatDir {dirs} 参数,这个参数对应着BModule的lib路径,这样AModule也可以引用到了。

原因猜测是,第二种写法,在依赖项目配置完成后,api方法会自动将依赖的aar转化成可以引用到的绝对路径,这样sdk引用了BModule之后,可以通过绝对路径找到aar。
但是第一种方式,只相对于BModule自己设置了相对路径,flatDir {dirs}配置也只是BModule可见,AModule只通过相对路径是找不到aar的。

也可以在根build.gradle中,配置configuration.all参数配置所有的lib的路径集合,这样所有module都可以找到了。

3.fataar的使用

子module打包成aar的时候,然后将aar上传到maven仓库
但是子module打包成aar只会包含jar,源码,pom文件,jat会和源码进行合并,但是子module中依赖的aar是不会合并到源码中的,所有这种情况处理方式有两种。

1.使用fataar工具,将所有的依赖aar和子module的源码进行合并打包操作。
2.使用maven插件,将aar上传到maven服务器,然后子module进行pom依赖。
fataar地址

4.国内镜像
    maven仓库:
    maven{ url 'https://maven.aliyun.com/repository/public'}
    maven { url 'https://maven.aliyun.com/repository/google' }
    maven { url 'https://maven.aliyun.com/repository/jcenter' }
    maven { url 'https://maven.aliyun.com/repository/central' }

	android sdk资源下载dns:
	220.181.174.33 dl.google.com dl-ssl.google.com
	
	github dns:
	140.82.113.4 github.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
5.代理问题

有时候,明明可以使用架包的地址在网站上下载,但是在android studio内使用gradle下载确下载不下来,原因在于gradle配置了http proxy,
在下面设置填写了代理;一旦代理网站失效,那么整个gradle下载都是失败的,问题会比较严重。
在这里插入图片描述
我们可以在user/.gradle/… ,目录下找到gradle文件夹下面的gradle.properties文件,将里面的代理配置删除。
在这里插入图片描述
问题在这里找到的:https://issuetracker.google.com/issues/239471023

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

闽ICP备14008679号