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. Function Pointer in Delpni

    program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TVoice = function(): Stri ...

  2. 一个matlab数字图像处理程序的解释

    clc; %clc是清除command window里的内容 clear all; %clear是清除workspace里的变量 close all; %close all来关闭所有已经打开的图像窗口 ...

  3. php自学笔记1

    PHP(Personal Home Page的缩写,现已更名Hypertext Preprocessor“超文本预处理器”)静态web开发:html,内容一成不变动态web开发: php(Zend). ...

  4. Html DOM 常用属性和方法

    Node对象的节点类型***************************************************接口 nodeType常量 nodeType值 备注Element Node ...

  5. 如何使用Github仓库创建网站

    官方文档:https://help.github.com/categories/github-pages-basics/ 1.创建一个仓库 2.额外建立一个gh-pages分支 3.添加CNAME文件 ...

  6. Java多线程初学者指南(4):线程的生命周期

    与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. // 开始线程 ...

  7. App小样在手机运行了一下

    外包公司把App小样的安装包发过来了,我在安卓手机上试了一把,虽然还只有几个静态页面,但安装那一刻还是小激动了一把. 在某美术系MM的帮助下,我基本掌握了原型软件azure. 事实证明,很多东西都是逼 ...

  8. Lua和Javascript差异对比

    Lua模拟器js方案 1.语法级模拟lua与js语言差异 1.1注释 js 为//,lua为--. 1.2变量js利用val来声明全局变量不存在局部变量,lua则不需要直接定位则为全局变量,local ...

  9. 【HDOJ】3047 Zjnu Stadium

    带权并查集. /* 3047 */ #include <iostream> #include <string> #include <map> #include &l ...

  10. 【HDOJ】2721 Persistent Bits

    题目有点长,但是题意说的很清楚.位操作. #include <stdio.h> ]; int main() { int a, b, c, s; int i, j, k, n, tmp, m ...