Gradle For Android(三):依赖管理

依赖仓库

预定义依赖仓库

Gradle预定义了三个Maven仓库,默认情况没有为项目添加任何依赖仓库,需要手动添加至repositories代码块。一个依赖由groupnameversion组成。

repositories {
    jcenter()//为第二个的超集
    mavenCentral()
    mavenLocal()
}

本地Maven仓库为已使用的依赖的本地缓存,位置在~/.m2.

远程仓库

当依赖在自有地Maven或Ivy服务器上时,应添加仓库url

repositories {
    maven{
        url "http://repo.abc.com/maven2"
        credential{
            username 'abc'
            password '123'//凭证会纳入版本控制系统,应使用单独的Gradle属性文件配置
    }
    ivy{
        url "http://repo.abc.com/ivy"
    }
}
本地仓库

当在自己的硬盘或网络驱动器上运行Maven和Ivy时,配置一个相对或绝对的URL即可

repositories {
    maven{
        url "./repo"
    }
}

当使用SDK manager安装Google repositories时, 会创建两个Maven仓库,ANDROID_SDK/extras/google/ m2repositoryANDROID_SDK/extras/android/m2repository
也可以添加文件夹作为仓库

repositories {
    flatDir {
        dirs 'aars'
    }
}

本地依赖

文件依赖
dependencies {
    compile files('libs/domoarigato.jar')
}
dependencies {
    compile fileTree('libs')
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
依赖项目
  • 当作一个模块
    模块中使用Android插件
apply plugin: 'com.android.library'

settings.gradle中添加该模块

include ':app', ':library'

添加依赖

dependencies {
    compile project(':library')
}
  • 使用aar文件
    添加仓库
repositories {
    flatDir {
        dirs 'aars'
    }
}

添加依赖

dependencies {
    compile(name:'libraryname', ext:'aar')
}

依赖概念

  • compile
  • apk
  • provided
  • testCompile
  • androidTestCompile
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 依赖管理是Gradle的一个亮点。在最好的情况下,你只需要在构建文件中添加一行代码,Gradle就可以从远程仓库下...
    sollian阅读 5,232评论 0 3
  • 依赖管理 依赖管理是Gradle最闪耀的地方,最好的情景是,你仅仅只需添加一行代码在你的build文件,Gradl...
    木木00阅读 467评论 1 6
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,644评论 25 709
  • 一.榜单介绍 排行榜包括四大类: 单一框架:仅提供路由、网络层、UI层、通信层或其他单一功能的框架 混合开发框架:...
    伟子男阅读 5,279评论 0 161
  • 我从前不懂什么叫悲伤 以为哭得撕心裂肺是最悲伤 后来我才晓得 悲伤就是 你看着他 你还会笑 你还会温柔以待 你还会...
    彭浩男阅读 233评论 0 0