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. Python学习笔记——正则表达式入门

    # 本文对正则知识不做详细解释,仅作入门级的正则知识目录. 正则表达式的强大早有耳闻,大一时参加一次选拔考试,题目就是用做个HTML解析器,正则的优势表现得淋漓尽致.题外话不多讲,直接上干货: 1. ...

  2. LED汽车前大灯

    一.LED汽车前大灯遇到问题.分析和解决 问题1: 当电源电压增大时,LED等闪烁,而且电源电压增大的越多闪烁的频率越低. 原因分析: 电源电压从12V升高到24V过程中,开关MOS管的Vds增大,Q ...

  3. react + iscroll5

    react + iscroll5 经过几天的反复折腾,总算做出一个体验还不错的列表页了,主要支持了下拉刷新,上拉加载两个功能. 一开始直接采用了react-iscroll插件,它是基于iscroll插 ...

  4. bzoj 1070: [SCOI2007]修车 费用流

    1070: [SCOI2007]修车 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2785  Solved: 1110[Submit][Status] ...

  5. 一个简单的DDraw应用程序

        阅读排行榜 1. C/C++ 笔试.面试题目大汇总(72915) 2. [STL]list基础(21718) 3. COM笔记-CoCreateInstance(14842) 4. C/C++ ...

  6. 如何实现Android重启应用程序代码 ?

    Intent i = getBaseContext().getPackageManager()  .getLaunchIntentForPackage(getBaseContext().getPack ...

  7. 【CF】283D Tennis Game

    枚举t加二分判断当前t是否可行,同时求出s.注意不能说|a[n]| <= |3-a[n]|就证明无解,开始就是wa在这儿了.可以简单想象成每当a[n]赢的时候,两人都打的难解难分(仅多赢一轮): ...

  8. Python正则匹配多行,多个数据

    最近用Python做一个crawler工具的时候,发现用一个正则表达式可以匹配到个数据的时候用match.group()只能打印出第一个数据,其它数据不能打印出来.最后找到解决方法,现在记录一下,直接 ...

  9. _GUN_SOURCE宏

    问题描述:在编译程序时,提示一个错误和一个警告. error:storage size of tz isn’t know: 其中tz是struct timezone类型的变量. warning:imp ...

  10. 比较详细的利用虚拟机对SD卡FAT32+EXT4+Ext4分区图解教程

    如果大家嫌虚拟机复杂,我这里提供一个我没用虚拟机之前的分区方法:这个方法实际是可行的 我在没有用虚拟机之前,我是这样操作的1.首先在分区软件分好fat32+ext2+ext22.然后用recovery ...