当前位置:   article > 正文

Gradle依赖本地aar包_gradle aar

gradle aar

一.本地仓库的方式(推荐)

1.生成本地aar仓库

step1.在项目Module build.gradle中添加gradle代码

例如:library/build.gradle

apply plugin: 'com.android.library'
apply plugin: 'maven'

android{...}

dependencies{...}

group = 'com.pm.qrcode'
version = '0.1.1'

uploadArchives {
    repositories {
        mavenDeployer {
            //提交到远程服务器:
            /* repository(url: "http://www.xxx.com/repos") {
                authentication(userName: "admin", password: "admin")
             }*/
            //本地的Maven地址
            repository(url: uri('../repositories'))
//            pom.groupId = group
            pom.version  = version
            pom.artifactId = 'qrcode'
        }
    }
}

  • 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

step2. Android Studio 执行uploadArchives Task生成本地仓库

2.依赖本地aar仓库

step1. 在需要使用的项目根目录build.gradle中添加本地仓库地址

buildscript {
    repositories {
        maven {
            url("file://" + project.rootDir.absolutePath + "/repositories")
        }
        google()
        jcenter()
    }
}

allprojects {
        maven {
            url("file://" + project.rootDir.absolutePath + "/repositories")
        }
        google()
        jcenter()
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

step2. 在需要使用的Moudle build.gradle中

    implementation 'com.pm.qrcode:qrcode:0.1.1'

  • 1
  • 2

注意:有时候遇到依赖需要传递的时候,需要transitive = true 如下:

implementation('com.pm.hybridsdk:hybridsdk:0.0.1') {
        transitive = true
    }
  • 1
  • 2
  • 3

二.编译生成aar包的方式

step1.在项目Moudle 节点执行build Task生成xxx-debug.aar 和 xxx-release.aar包

step2.拷贝aar包到使用的项目中libs下,在build.gradle中

android{...}
repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies{
    implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

或者拷贝aar包到项目根目录repos下,在项目根目录build.gradle中声明仓库

buildscript {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs '../repos'
        }
    }
}

allprojects {
        google()
        jcenter()
        flatDir {
            dirs '../repos'
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

step3. 在需要使用的Moudle build.gradle中声明依赖

implementation(name: 'hybridsdk-release', ext: 'aar')

  • 1
  • 2

总结:两种方式对比,第二种方式有弊端,如果我们的aar包中又有依赖三方的库,比如:Rxjava,EventBus等。这时候三方的依赖不会被编译进我们的aar包中,那么在使用aar包就会有问题。这种情况我们只能采用第一种方式(本地仓库的方式)。如果我们的aar包完全没有依赖三方的库,那么我们可以采用第二种方式(编译生成aar包的方式),简单快捷。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号