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. ADO.NET学习小结【1】正在更新...

    小弟正在学习ADO.net有误的地方还请大大们批评指出,小弟在此谢过了 一.ADO.net简述: 以前我们写程序尤其是写和数据库有关的应用程序时,你我都得要了解Microsoft ADO COM对象才 ...

  2. [HDOJ 5212] [BestCoder Round#39] Code 【0.0】

    题目链接:HDOJ - 5212 题目分析 首先的思路是,考虑每个数对最终答案的贡献. 那么我们就要求出:对于每个数,以它为 gcd 的数对有多少对. 显然,对于一个数 x ,以它为 gcd 的两个数 ...

  3. 练习PYTHON协程之GREENLET

    STACKLESS就算了,了解一下原理即可. GREENLET,GEVENT,EVENTLET这些,比较好测试,还是都 撸一次,得个印象. 测试代码都是网上的大路货. from greenlet im ...

  4. String 类型的相关转换

    题目: what is the result of the expression 5.4+"3.2"? 答案: 当一个运算数为原始数据类型,另外一个为字符串时,则基本数据类型会转化 ...

  5. 【HDU 2855】 Fibonacci Check-up (矩阵乘法)

    Fibonacci Check-up Problem Description Every ALPC has his own alpc-number just like alpc12, alpc55, ...

  6. 带你了解世界最先进的手势识别技术 -- 微软,凌感,Leap...

    转载 今天为大家解释一下现有的几种主要的手势识别技术,为你揭开手势识别技术的神秘面纱. 概述 谈起手势识别技术,由简单粗略的到复杂精细的,大致可以分为三个等级:二维手型识别.二维手势识别.三维手势识别 ...

  7. 我新买的红米手机,新浪和360浏览器都能进,也能看电视,就是不能上手机QQ和微信

    1.请您在桌面下.点击手菜单键-全局搜索,输入网络助手,点击流量排行,点击批量联网控制,查看该软件下(不能上网的应用)wifi和流量2G/3G下方的选项是否都勾选的.如果没有勾选,请您勾选. 2:仍然 ...

  8. VC模拟发送数据包-百度关键词查找

    VC模拟发送数据包-百度关键词查找 逗比汪星人2009-09-06上传   VC模拟发送数据包-百度关键词abcdef查找 详情 http://blog.csdn.net/wangningyu htt ...

  9. linux svn服务器搭建、客户端操作、备份与恢复

    Subversion(SVN)是一个开源的版本控制系統,管理着随时间改变的数据.这些数据放置在一个中央资料档案库中,这个档案库很像一个普通的文件服务器,它会记住每一次文件的变动,这样就可以把档案恢复到 ...

  10. Datetime中yyyy-MM-dd-hh-mm-ss的格式

    namespace yyyy_MM_dd_hh_mm{    class Program    {        static void Main(string[] args)        { wh ...