1. 程式人生 > >build.gradle動態配置依賴版本

build.gradle動態配置依賴版本

在這裡插入圖片描述
專案根目錄中建立一個gradle檔案,名字隨便起,也可以不建立直接寫在專案的build.gradle檔案中。

1.在gradle檔案中定義變數並賦值:

例如我在專案根目錄建立globalconfig.gradle檔案,內容如下:

ext {
//簽名檔案目錄
  storageFilePath = [
      releaseSignStoreFile: '/Users/***/tools/keystore/***/***',
      zhangsanSignStoreFile     : '/Users/***/tools/keystore/lsjk/lsjkkeystore',
      lisiSignStoreFile   : '/Users/***/tools/keystore/lejianbang/lejianbangkeystore',
      wangwuSignStoreFile   : '/Users/***/tools/keystore/xingyi/xingyikeystore',
  ]
  //打包版本配置
  isTest = false
  isRelease = false

  // Sdk and tools
  buildToolsVersion = '23.0.3'
  compileSdkVersion = 24
  minSdkVersion = 16
  targetSdkVersion = 22

//版本號
  versionCode = 61
  versionName = "2.5.7"

  javaVersion = JavaVersion.VERSION_1_7

  // App dependencies version
  supportLibraryVersion = "24.2.1"
  constraintVersion = "1.0.2"
  multidexVersion = "1.0.1"

  //第三方庫版本
  arouterApiVersion = "1.3.1"
  annotationProcessor = "1.1.4"
  xutilsVersion = "3.5.0"
  glideVersion = "3.7.0"
  gsonVersion = "2.8.2"
  fragmentationVersion = "1.1.9"
  loggerVersion = "2.1.1"
  rxjavaVersion = '1.1.0'
  rxandroidVersion = '1.1.0'
  okhttpVersion = '3.9.1'
  okhttploggingVersion = '3.8.1'
  retrofitVersion = '2.3.0'
}

2.在app module的build.gradle中使用變數:

 appVersionCode = rootProject.ext.versionCode
 appVersionName = rootProject.ext.versionName

 compileSdkVersion rootProject.ext.compileSdkVersion
 buildToolsVersion rootProject.ext.buildToolsVersion
  
 signingConfigs {
    releaseSignStoreFile{
      keyAlias 'release'
      keyPassword '12334'
      storeFile file(rootProject.ext.storageFilePath.releaseSignStoreFile)
      storePassword '12345'
    }
    zhangsanSignStoreFile     {
      keyAlias 'zhangsan'
      keyPassword '11111'
      storeFile file(rootProject.ext.storageFilePath.zhangsanSignStoreFile     )
      storePassword '22222'
    }
    lisiSignStoreFile     {
      keyAlias 'lisi'
      keyPassword '212323'
      storeFile file(rootProject.ext.storageFilePath.lisiSignStoreFile)
      storePassword '123123123'
    }
    wangwuSignStoreFile      {
      keyAlias 'wangwu'
      keyPassword '3234'
      storeFile file(rootProject.ext.storageFilePath.wangwuSignStoreFile      )
      storePassword '3243423'
    }
  }
  
 dependencies {
	  compile "io.reactivex:rxandroid:${rootProject.ext.rxandroidVersion}"
	  compile "io.reactivex:rxjava:${rootProject.ext.rxjavaVersion}"
  }

也可以省去rootProject,在build.gradle中增加如下程式碼匯入配置即可:

apply from: '../globalconfig.gradle'

如果上述變數直接定義在app的build.gradle中這句匯入也不需要了。
在這裡插入圖片描述