Skip to content
统一管理android studio中的gradle编译脚本
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
images
README.md
application.gradle
bintray.gradle
config.gradle
library.gradle

README.md

build.gradle

统一管理android studio中的gradle编译脚本

目的

当工程集成很多Modules时,每个Module都有一个build.gradle,并且带有如下重复的代码;对每个build.gradle修改很麻烦,因此统一管理build.gradle文件是必要的

// 重复代码
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:24.2.1'
}
  • 优化代码
  • 使用相同的编译配置
  • 统一管理远程依赖
  • 减少sync project次数

本地配置

  • 在项目根目录下创建config.gradle文件,作为管理配置文件

     import java.text.SimpleDateFormat
     import java.util.regex.Matcher
     import java.util.regex.Pattern
     
     ext {
         // 上传bintray的配置
         bintray = [
                 groupId        : "com.excellence",
                 publishVersion : "1.0.0",
                 website        : "https://github.com/VeiZhang",
                 licences       : ['Apache-2.0']
         ]
         
         // 插件
         plugins = [
                 application       : "com.android.application",
                 library           : "com.android.library",
                 maven             : "com.github.dcendents.android-maven",
                 bintray           : "com.jfrog.bintray",
                 novoda            : "com.novoda.bintray-release",
                 greendao          : "org.greenrobot.greendao",
                 "greendao-gradle" : "org.greenrobot:greendao-gradle-plugin:3.2.2"
         ]
     
         // 配置
         android = [
                 /*************************原生配置*************************/
                 compileSdkVersion       : 25,
                 buildToolsVersion       : "25.0.0",
                 minSdkVersion           : 15,
                 targetSdkVersion        : 19,
                 versionCode             : getVersionCode(),
                 versionName             : getVersionName(),
     
                 /*************************自定义配置*************************/
                 androidSupportSdkVersion: "23.0.0"
         ]
     
         // 依赖
         dependencies = [
                 /*************************原生依赖*************************/
                 "appcompat-v7"      : "com.android.support:appcompat-v7:${android["androidSupportSdkVersion"]}",
                 "support-v4"        : "com.android.support:support-v4:${android["androidSupportSdkVersion"]}",
                 "cardview-v7"       : "com.android.support:cardview-v7:${android["androidSupportSdkVersion"]}",
                 "recyclerview-v7"   : "com.android.support:recyclerview-v7:${android["androidSupportSdkVersion"]}",
                 "design"            : "com.android.support:design:${android["androidSupportSdkVersion"]}",
                 "annotations"       : "com.android.support:support-annotations:${android["androidSupportSdkVersion"]}",
                 "gridlayout-v7"     : "com.android.support:gridlayout-v7:${android["androidSupportSdkVersion"]}",
                 "constraint-layout" : "com.android.support.constraint:constraint-layout:1.1.2",
    
                 /*************************第三方依赖*************************/
                 // https://github.com/square/retrofit
                 "retrofit2"           : "com.squareup.retrofit2:retrofit:2.5.0",
                 "converter-scalars"   : "com.squareup.retrofit2:converter-scalars:2.5.0",
                 "converter-gson"      : "com.squareup.retrofit2:converter-gson:2.5.0",
                 "adapter-rxjava"      : "com.squareup.retrofit2:adapter-rxjava:2.5.0",
                 "adapter-rxjava2"     : "com.squareup.retrofit2:adapter-rxjava2:2.5.0",
                 // https://github.com/square/okhttp
                 "okhttp"              : "com.squareup.okhttp3:okhttp:3.14.2",
                 // https://github.com/greenrobot/greenDAO
                 "greendao"            : "org.greenrobot:greendao:3.2.2",
                 // https://github.com/yuweiguocn/GreenDaoUpgradeHelper
                 "greendao-helper"     : "io.github.yuweiguocn:GreenDaoUpgradeHelper:2.2.1",
                 // https://github.com/bumptech/glide
                 "glide"               : "com.github.bumptech.glide:glide:4.9.0",
                 // https://github.com/square/picasso
                 "picasso"             : "com.squareup.picasso:picasso:2.71828",
                 // https://github.com/facebook/fresco
                 "fresco"              : "com.facebook.fresco:fresco:2.0.0",
                 // https://github.com/greenrobot/EventBus
                 "eventbus"            : "org.greenrobot:eventbus:3.1.1",
                 // https://github.com/BuglyDevTeam/Bugly-Android
                 "bugly"               : "com.tencent.bugly:crashreport:3.0.0",
                 "bugly-native"        : "com.tencent.bugly:nativecrashreport:3.7.1",
                 // https://bintray.com/android/android-utils/com.android.volley.volley
                 "volley"              : "com.android.volley:volley:1.1.1",
                 // https://github.com/ReactiveX/RxJava
                 "rxjava"              : "io.reactivex:rxjava:1.3.8",
                 "rxjava2"             : "io.reactivex.rxjava2:rxjava:2.2.9",
                 "rxandroid"           : "io.reactivex:rxandroid:2.1.1",
                 "rxandroid2"          : 'io.reactivex.rxjava2:rxandroid:2.2.0',
                 // https://github.com/JakeWharton/RxBinding
                 "rxbinding"           : 'com.jakewharton.rxbinding2:rxbinding:2.2.0',
                 // https://github.com/google/gson
                 "gson"                : "com.google.code.gson:gson:2.8.5",
                 // https://github.com/alibaba/fastjson
                 "fastjson"            : "com.alibaba:fastjson:1.1.70.android",
                 // https://github.com/apache/commons-lang
                 "commons-lang3"       : "org.apache.commons:commons-lang3:3.9",
                 // https://github.com/square/leakcanary
                 "leakcanary"          : "com.squareup.leakcanary:leakcanary-android:1.6.3",
                 "leakcanary-release"  : "com.squareup.leakcanary:leakcanary-android-no-op:1.6.3",
                 "leakcanary-fragment" : "com.squareup.leakcanary:leakcanary-support-fragment:1.6.3",
                 // https://github.com/YoKeyword/Fragmentation
                 "fragmentation"       : "me.yokeyword:fragmentation:1.3.7",
                 // https://github.com/airbnb/lottie-android,2.8.0+是为了兼容AndroidX,没有用到AndroidX,则使用版本2.7.0,否则莫名其妙会报错
                 "lottie"              : "com.airbnb.android:lottie:2.7.0",
    
                 /*************************个人依赖*************************/
                 // https://github.com/VeiZhang/BaseToolsLibrary
                 "basetools"           : "com.excellence:basetools:1.2.6",
                 // https://github.com/VeiZhang/Permission
                 "permission"          : "com.excellence:permission:1.0.2",
                 // https://github.com/VeiZhang/RetrofitClient
                 "retrofit-client"     : "com.excellence:retrofit:1.0.5",
                 "retrofit-client2"    : "com.excellence.retrofit:retrofit2:2.0.0",
                 // https://github.com/VeiZhang/QSkinLoader
                 "skinloader"          : "com.excellence:skinloader:1.2.2",
                 // https://github.com/VeiZhang/ToastKit
                 "toast"               : "com.excellence:toast:1.1.0",
                 // https://github.com/VeiZhang/MailSender
                 "mailsender"          : "com.excellence:mailsender:1.0.0",
                 // https://github.com/VeiZhang/Downloader
                 "downloader"          : "com.excellence:downloader:1.2.0",
                 // https://github.com/VeiZhang/AppStatistics
                 "app-statistics"      : "com.excellence:app-statistics:1.0.1",
                 // https://github.com/VeiZhang/AndroidExec
                 "exec"                : "com.excellence:exec:1.1.1",
                 // https://github.com/VeiZhang/AndroidFFmpeg
                 "ffmpeg"              : "com.excellence:ffmpeg:1.2.1",
                 "ffmpeg-java"         : "com.excellence:ffmpeg-java:1.2.1",
                 "ffmpeg-armeabi"      : "com.excellence:ffmpeg-armeabi:1.2.1",
                 "ffmpeg-armv7a"       : "com.excellence:ffmpeg-armv7a:1.2.1",
                 "ffmpeg-x86"          : "com.excellence:ffmpeg-x86:1.2.1",
                 // https://github.com/VeiZhang/ImageLoader
                 "imageloader"         : "com.excellence:imageloader:1.0.0",
                 "imageloader-fresco"  : "com.excellence:imageloader-fresco:1.0.0",
                 "imageloader-picasso" : "com.excellence:imageloader-picasso:1.0.0",
                 "imageloader-glide"   : "com.excellence:imageloader-glide:1.0.0"
                 // https://github.com/VeiZhang/DIY-EpgView
                 "epgview"             : "com.excellence:diy-epgview:1.0.0"
             ]
     }
     
     /***********************APP版本控制的通用方法***********************/
     /**
      * svn
      * 直接读取svn版本号作为版本控制
      */
     /**
      * git
      *
      * git tag作为版本名称
      * git 版本号有两种方法
      *  ①一种是把提交次数作为versionCode
      *  ②另外一种是把tag的数量作为versionCode
      */
     
     /**
      * 获取版本
      * @return
      */
     def getVersionName() {
         def date = getDate()
         def version = getSvnVersionCode()
         if (version != 0) {
             return "1.0.${version} [${date}]"
         }
         version = getGitTag()
         if (version != 0) {
             return "${version} [${date}]"
         }
         if (version == 0) {
             version = 1
         }
         /**
          * 错误的版本信息,请检查
          */
         return "0.${version} [${date}]"
     }
     
     /**
      * 获取版本号
      * @return
      */
     def getVersionCode() {
         def versionCode = getSvnVersionCode()
         if (versionCode == 0) {
             versionCode = getGitVersionCode()
         }
         if (versionCode == 0) {
             versionCode = 1
         }
         return versionCode
     }
     
     /***********************读取Git信息***********************/
     
     /**
      * 读取当前分支最近的一次git tag
      * @return tag
      */
     def getGitTag() {
         try {
             def process = ("git describe --abbrev=0 --tags").execute()
             def tag = process.text.trim()
             return tag
         } catch (e) {
             println e.getMessage()
         }
         return 0
     }
     
     /**
      * 以git提交的数量作为其版本号
      * @return tag的数量
      */
     def getGitVersionCode() {
         try {
             def process = ("git rev-list --all --count").execute()
             def version = process.text.trim().toInteger()
             return version
         } catch (e) {
             println e.getMessage()
         }
         return 0
     }
     
     /***********************读取SVN信息***********************/
     
     /**
      * 根据svn提交版本生成版本号
      * @return
      */
     def getSvnVersionCode() {
         try {
             def process = ("svnversion -c " + getBuildDir().parent).execute()
             process.waitFor()
             def version = process.in.text
             Pattern pattern = Pattern.compile("(\\d+:)?(\\d+)\\D")
             Matcher matcher = pattern.matcher(version)
             if (matcher.find()) {
                 version = matcher.group(matcher.groupCount())
             }
             return Integer.parseInt(version)
         } catch (e) {
             println e.getMessage()
         }
         return 0
     }
     
     /**
      * 获取日期
      * @return
      */
     def getDate() {
         String date = new SimpleDateFormat("MMddyyyy").format(new Date())
         return date
     }
    

    config.gradle

  • 在项目根目录的build.gradle中引用,供其他的Module使用

    注意:

    1. 如果想使用ext的值,则只能在项目根目录的build.gradle中引用
    2. 想让单独的Module使用,则在该Module的build.gradle里引入,但是此时不能使用ext的值,否则会提示无法找到 "Error:Cannot get property 'xxx' on extra properties extension as it does not exist"
    3. 如果根目录的build.gradle引入了config.gradle,则子目录不需要引入,否则可能会出现一些异常:如上传Bintray时报错
     apply from: "config.gradle"
    

    config引用

  • 在Module目录的build.gradle中使用变量

    config使用

远程配置

远程配置配置其他步骤与本地配置是一样,不同的是引用的方式,导入的不是路径里的文件,而是一个文件链接

apply from: "https://github.com/VeiZhang/build.gradle/blob/master/config.gradle?raw=true"

远程配置引用

继承方式

本地、远程的配置都只是保存了变量,但是如果想引用gradle文件里面的函数,其实很简单,与上面是相同的做法,必须注意的是,apply from: 放置的顺序很重要!

注意:引入位置在文件的开头、中间、结尾等处使用。因为gradle脚本编译是顺序执行的,如果父脚本与子脚本有相同的方法,此时父脚本引入的顺序就非常重要,不同的位置,执行先后不一样。

继承方式

示例

第三方依赖版本

依赖 版本
retrofit2 Download
converter-scalars Download
converter-gson Download
adapter-rxjava Download
adapter-rxjava2 Download
okhttp Download
logging-interceptor Download
greendao Download
greendao-helper Download
glide Download
picasso Download
fresco Download
eventbus Download
bugly Download
bugly-native Download
volley Download
rxjava Download
rxjava2 Download
rxandroid Download
rxbinding Download
gson Download
fastjson Download
commons-lang3 Download
leakcanary Download
leakcanary-release Download
leakcanary-fragment Download
fragmentation Download
lottie Download
basetools Download
permission Download
retrofit-client Download
retrofit-client2 Download
skinloader Download
toast Download
mailsender Download
downloader Download
app-statistics Download
exec Download
ffmpeg Download
imageloader Download
diy-epgview Download
You can’t perform that action at this time.