博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
优雅的项目配置--常用库和版本管理
阅读量:4074 次
发布时间:2019-05-25

本文共 7207 字,大约阅读时间需要 24 分钟。

欢迎Follow我的, 关注我的.

最近经常有人问我, 如何管理gradle版本, 我来统一讲解这些小技巧.

随着开发的成熟, 模块越来越多, 为了开发稳定的程序, 引入的库也随之增加, 如何确保所有项目使用相同的编译版本he库版本呢?

当然, Gradle的参数配置可以帮我们实现这些.

Gradle

主要 
(1) 常用库的展示与配置. 
(2) 统一管理项目和库的版本. 
(3) 设置项目的私有参数.


1. 常用库

编程三剑客, 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}" // 头像视图}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

项目版本: 
def cfg = rootProject.ext.configuration 
cfg.compileVersion 
库版本: 
def libs = rootProject.ext.libraries 
${libs.retrofit}


2. 参数管理

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

android {    defaultConfig {        buildConfigField "String", "MARVEL_PUBLIC_KEY", "\"${marvel_public_key}\""        buildConfigField "String", "MARVEL_PRIVATE_KEY", "\"${marvel_private_key}\""    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

marvel_public_key   = 74129ef99c9fd5f7692608f17abb88f9marvel_private_key  = 281eb4f077e191f7863a11620fa1865f2940ebeb
  • 1
  • 2
  • 1
  • 2

未指定路径, 默认是配置在gradle.properties中. 
两个地方可以配置参数, 一个是项目的build.gradle, 一个是gradle.properties.

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

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

3. 版本管理

版本管理配置在项目的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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

通过这样的方式管理Android项目, 可以便捷的更改版本号, 所有模块统一.

OK, that’s all! Enjoy it!

你可能感兴趣的文章
一些最最基本的几何图形公式
查看>>
Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
查看>>
理解四元数的一些文章
查看>>
Unity Shader入门
查看>>
片元着色器(Fragment Shader)被称为像素着色器(Pixel Shader),但
查看>>
UNITY自带的3D object没有三角形?
查看>>
Lambert(朗伯)光照模型 和Half Lambert的区别
查看>>
float4数据类型
查看>>
【Unity Shaders】学习笔记
查看>>
Holographic Remoting Player
查看>>
unity之LOD
查看>>
UNITY 移动到指定位置的写法
查看>>
Unity中关于作用力方式ForceMode的功能注解
查看>>
UNITY实现FLASH中的setTimeout
查看>>
HOLOLENS 扫描特效 及得出扫描结果(SurfacePlane)
查看>>
矩形旋转一定角度后,四个点的新坐标
查看>>
Unity - RectTransform详解
查看>>
UNITY和图片像素的换算
查看>>
Resources.Load加载文件返回null的原因
查看>>
Introducing Holographic Emulation
查看>>