http://www.tuicool.com/articles/vqQZBrm

大型项目 Gradle 的常用库和版本管理

随着Android开发的成熟, 模块越来越多, 引入库也随之增加, 需要统一管理这些库和版本号. 根据自己的开发经验, 本文介绍使用Gradle参数配置实现库的规范管理.

主要

(1) 常用库的展示与配置.

(2) 统一管理项目和库的版本.

(3) 设置项目的私有参数.

常用库

编程三剑客, RxJava+Retrofit+Dagger.

常用: ButterKnife依赖注解, Glide/Picasso图片处理.

使用根项目(rootProject)的参数管理子项目的版本.

apply plugin: 'me.tatarka.retrolambda'      // Lambda表达式
apply plugin: 'com.android.application' // Android应用
apply plugin: 'com.neenbedankt.android-apt' // 编译时类
apply plugin: 'com.android.databinding' // 数据绑定 def cfg = rootProject.ext.configuration // 配置
def libs = rootProject.ext.libraries // 库 android {
compileSdkVersion cfg.compileVersion
buildToolsVersion cfg.buildToolsVersion defaultConfig {
applicationId cfg.package
minSdkVersion cfg.minSdk
targetSdkVersion cfg.targetSdk
versionCode cfg.version_code
versionName cfg.version_name buildConfigField "String", "MARVEL_PUBLIC_KEY", "\"${marvel_public_key}\""
buildConfigField "String", "MARVEL_PRIVATE_KEY", "\"${marvel_private_key}\""
} buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
} compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
} // 注释冲突
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
} dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12' // Android
compile "com.android.support:design:${libs.supportVersion}"
compile "com.android.support:appcompat-v7:${libs.supportVersion}"
compile "com.android.support:cardview-v7:${libs.supportVersion}"
compile "com.android.support:recyclerview-v7:${libs.supportVersion}"
compile "com.android.support:palette-v7:${libs.supportVersion}" // Retrofit
compile "com.squareup.retrofit:retrofit:${libs.retrofit}"
compile "com.squareup.retrofit:converter-gson:${libs.retrofit}"
compile "com.squareup.retrofit:adapter-rxjava:${libs.retrofit}" // ReactiveX
compile "io.reactivex:rxjava:${libs.rxandroid}"
compile "io.reactivex:rxandroid:${libs.rxandroid}" // Dagger
compile "com.google.dagger:dagger:${libs.dagger}"
apt "com.google.dagger:dagger-compiler:${libs.dagger}"
compile "org.glassfish:javax.annotation:${libs.javax_annotation}" // Others
compile "com.jakewharton:butterknife:${libs.butterknife}" // 资源注入
compile "com.github.bumptech.glide:glide:${libs.glide}" // 图片处理
compile "jp.wasabeef:recyclerview-animators:${libs.recycler_animators}" // Recycler动画
compile "de.hdodenhof:circleimageview:${libs.circleimageview}" // 头像视图
}

项目版本:

def cfg = rootProject.ext.configuration

cfg.compileVersion

库版本:

def libs = rootProject.ext.libraries

${libs.retrofit}

参数管理

buildConfigField管理私有参数, 配置在gradle.properties里面.

android {
defaultConfig {
buildConfigField "String", "MARVEL_PUBLIC_KEY", "\"${marvel_public_key}\""
buildConfigField "String", "MARVEL_PRIVATE_KEY", "\"${marvel_private_key}\""
}
}

设置参数的 类型\变量名\位置 三个部分.

marvel_public_key   = 74129ef99c9fd5f7692608f17abb88f9
marvel_private_key = 281eb4f077e191f7863a11620fa1865f2940ebeb

未指定路径, 默认是配置在 gradle.properties 中.

两个地方可以配置参数, 一个是项目的build.gradle, 一个是gradle.properties.

项目中使用 BuildConfig.xxx 引入参数.

MarvelSigningIterceptor signingIterceptor = new MarvelSigningIterceptor(
BuildConfig.MARVEL_PUBLIC_KEY, BuildConfig.MARVEL_PRIVATE_KEY);

版本管理

版本管理配置在项目的build.gradle中, 包含两个部分, 一个是项目的版本, 一个是库的版本. 把常用参数设置成为变量. 子项目使用 rootProject.ext.xxx 的形式引入.

ext {
configuration = [
package : "me.chunyu.spike.springrainnews",
buildToolsVersion: "23.0.1",
compileVersion : 23,
minSdk : 14,
targetSdk : 23,
version_code : 1,
version_name : "0.0.1",
] libraries = [
supportVersion : "23.1.1",
retrofit : "2.0.0-beta2",
rxandroid : "1.1.0",
dagger : "2.0",
javax_annotation : "10.0-b28",
butterknife : "7.0.1",
glide : "3.6.1",
recycler_animators: "2.1.0",
circleimageview : "2.0.0"
]
} buildscript {
repositories {
jcenter()
} dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha5'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'me.tatarka:gradle-retrolambda:3.2.4'
classpath 'com.android.databinding:dataBinder:1.0-rc4'
}
} allprojects {
repositories {
jcenter()
}
} task clean(type: Delete) {
delete rootProject.buildDir
}

补充

Retrolambda的最新配置方式

plugins {
id "me.tatarka.retrolambda" version "3.2.5"
} android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

通过这些方式设置Android项目的Gradle配置, 可以便捷地修改库的版本号, 统一管理并控制风险.

OK, that’s all! Enjoy it!

大型项目 Gradle 的常用库和版本管理[转]的更多相关文章

  1. python常用库

    本文由 伯乐在线 - 艾凌风 翻译,Namco 校稿.未经许可,禁止转载!英文出处:vinta.欢迎加入翻译组. Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 ...

  2. 大型项目使用Automake/Autoconf完成编译配置

    http://www.cnblogs.com/xf-linux-arm-java-android/p/3590770.htmlhttp://blog.csdn.net/zengraoli/articl ...

  3. 在大型项目上,Python 是个烂语言吗

    Robert Love, Google Software Engineer and Manager on Web Search. Upvoted by Kah Seng Tay, I was the ...

  4. Python常用库大全

    环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. v ...

  5. Python常用库大全,看看有没有你需要的

    作者:史豹链接:https://www.zhihu.com/question/20501628/answer/223340838来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  6. 大型项目使用Automake/Autoconf完成编译配置(标准的编译过程已经变成了简单的三部曲:configure/make/make install,)

    使用过开源C/C++项目的同学们都知道,标准的编译过程已经变成了简单的三部曲:configure/make/make install, 使用起来很方便,不像平时自己写代码,要手写一堆复杂的Makefi ...

  7. [C++] C++中的常用库

    转载自:C++常用库 C++ 资源大全 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包 ...

  8. Python的常用库

    读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...

  9. 废弃fastjson!大型项目迁移Gson保姆级攻略

    前言 大家好,又双叒叕见面了,我是天天放大家鸽子的蛮三刀. 在被大家取关之前,我立下一个"远大的理想",一定要在这周更新文章.现在看来,flag有用了... 本篇文章是我这一个多月 ...

随机推荐

  1. 【 java版坦克大战--事件处理】 键盘控制小球上下左右移动

    上一节已经学习了事件处理,这一节需要完成通过键盘的上下左右键控制小球移动. 然后再通过应用到我们绘制的坦克上. /** * 加深对事件处理机制的理解 * 通过光标的上下左右键,控制小球的左右上下移动. ...

  2. Presto: 可以处理PB级别数据的分布式SQL查询引擎

    2012年秋季Facebook启动了Presto,Presto的目的是在几百PB级别数据量上面进行准实时分析.在摒弃了一些外部项目以后,Facebook准备开发他们自己的分布式查询引擎.Presto的 ...

  3. Javascript 备忘

    1遍历所有属性 var person={fname:"John",lname:"Doe",age:25}; for (x in person) { txt=tx ...

  4. Android 系统功能设置菜单 LinearLayout与relativeLayout的实现

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  5. asp.net 163邮件发送

    <table id="> <tr> <td style="width: 393px"> 收信:<asp:TextBox ID=. ...

  6. 运行所选代码生成器时出错:无效指针(异常来自HRESULT:0x80004003(E_POINTER))

    这个是在使用了VS2015 update1学MVC的时候,在controllers的方法添加view时报的一个错误,中文基本搜不到解决方法,然后无奈转到成英文,还好G家的搜索提示补全能力拯救了我的渣英 ...

  7. 读书笔记-----Java并发编程实战(二)对象的共享

    public class NoVisibility{ private static boolean ready; private static int number; private static c ...

  8. 转:二十七、Java图形化界面设计——容器(JFrame)

    转:http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计——容器(JFrame) 程序是为了方便用户使用的, ...

  9. bzoj1449

    竞赛图一般是把每场比赛当作一个点,然后和相应球队连边每场比赛一赢一输对两支球队都有影响看起来不好搞实际上我们可以先假设参加后面后面所有球队都输(每场比赛双输)然后对每场比赛我们选择一支球队赢计算增加的 ...

  10. Visual Studio统计有效代码行数

    在网上看到别人用的方法: 按CTRL+SHIFT+F (Find in files),勾上支持正则表达式,然后输入搜索内容: ^:b*[^:b#/]+.*$ 以上表达式的统计可做到:#开头和/开头或者 ...