Gradle實現統一依賴管理

目的:

在版本迭代過程當中,常常遇到一種狀況:一旦android SDK、android support包、gradle build tool以及一些第三方包一旦推出新版本,爲了保持對新特性的支持,咱們須要對每一個module中的build.gradle文件都進行一次手動修改(如appcompat-v7包),費時又費力。那麼有沒有辦法對這些依賴進行統一管理呢,答案是確定的。java

方法一:分類屬性配置

  • 在項目的根目錄建立一個gradle配置文件config.gradle,項目中全部的依賴只要在這個文件中統一配置便可。格式以下(內容根據須要進行修改): 
ext {
    android = [
            compileSdkVersion: 24,
            buildToolsVersion: "24.0.2",
            applicationId    : "com.carme.carmerchant",
            minSdkVersion    : 15,
            targetSdkVersion : 22,
            versionCode      : 3,
            versionName      : "1.0.3"
    ]

    dependencies = [
            "test"                  : "junit:junit:4.12",
            "appcompat-v7"          : "com.android.support:appcompat-v7:25.0.0",
            "support-v4"            : "com.android.support:support-v4:25.0.0",
            "support_design"        : "com.android.support:design:25.0.0",
            "rxjava"                : "io.reactivex:rxjava:1.2.0",
            "rxandroid"             : "io.reactivex:rxandroid:1.2.1",
            "retrofit"              : "com.squareup.retrofit2:retrofit:2.1.0",
            "converter-gson"        : "com.squareup.retrofit2:converter-gson:2.1.0",
            "adapter-rxjava"        : "com.squareup.retrofit2:adapter-rxjava:2.1.0",
            "logging-interceptor"   : "com.squareup.okhttp3:logging-interceptor:3.4.1",
            "pullToRefresh"         : "com.carme.module:pullToRefresh:1.0.13",
            "glide"                 : "com.github.bumptech.glide:glide:3.7.0",
            "glide-okhttp3"         : "com.github.bumptech.glide:okhttp3-integration:1.4.0@aar",
            "recyclerview"          : "com.android.support:recyclerview-v7:25.0.0",
            "bugly"                 : "com.tencent.bugly:nativecrashreport:3.0",
            "bugly-upgrade"         : "com.tencent.bugly:crashreport_upgrade:1.1.7",
            "jpush"                 : "cn.jiguang:jpush:2.2.0",
            "hotfix"                : "com.taobao.android:alisdk-hotfix:1.4.0",
            "rxlifecycle"           : "com.trello:rxlifecycle:0.8.0",
            "rxlifecycle-components": "com.trello:rxlifecycle-components:0.8.0",
            "butterknife"           : "com.jakewharton:butterknife:8.4.0",
            "butterknife-compiler"  : "com.jakewharton:butterknife-compiler:8.4.0",
            "otto"                  : "com.squareup:otto:1.3.8",
            "mpandroidchart"        : "com.github.PhilJay:MPAndroidChart:v3.0.1",
            "multidex"              : "com.android.support:multidex:1.0.1"
    ]
}
  • 其次在根目錄的build.gradle文件中添加內容(apply from:」config.gradle」),全部的module均可以從這個(config.gradle)配置文件裏讀取公共參數。如圖:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config.gradle"
buildscript {
    repositories {
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath 'com.tencent.bugly:symtabfileuploader:2.0.4'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()

        maven {
            url "https://jitpack.io"
        }

        maven {
            url 'http://192.168.51.199:8081/nexus/content/repositories/Android/'
        }

        maven {
            url "http://repo.baichuan-android.taobao.com/content/groups/BaichuanRepositories"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • 在各個module目錄下的build.gradle文件中使用以下:
android {  
          compileSdkVersion rootProject .ext.android.compileSdkVersion
          buildToolsVersion rootProject .ext.android.buildToolsVersion
          defaultConfig {
              applicationId rootProject .ext.android.applicationId
              minSdkVersion rootProject .ext.android.minSdkVersion
              targetSdkVersion rootProject .ext.android.targetSdkVersion
              versionCode rootProject .ext.android.versionCode
              versionName rootProject .ext.android.versionName
          }
          ...
      }  
      dependencies {
          ...
          compile rootProject .ext.dependencies[ "design"]
          compile rootProject .ext.dependencies[ "appcompat-v7"]
          compile rootProject .ext.dependencies[ "recyclerview-v7"]
          ...
      }

 

方法二:常量屬性配置

  • 步驟1,2同方法一一致,在項目的根目錄建立一個gradle配置文件config.gradle,而後在根目錄build.gradle中引入。須要注意的一點是在文件中配置的是依賴常量。格式以下: 
ext {
    //Version of compile sdk
    COMPILE_SDK_VERSION = 23
    //Version of Android build tool
    BUILD_TOOLS_VERSION = "23.0.3"
    //Min version of Android sdk
    MIN_SDK_VERSION = 9
    //Version of target Android sdk
    TARGET_SDK_VERSION = 23
    //Use progurad or not
    MINIFY_ENABLED = true
    MINIFY_DISABLED = false
    //Version of "com.android.support:appcompat-v7"
    APPCOMPAT_VERSION = '23.2.0'
    //Version of "junit":
    JUNIT_VERSION= '4.12'
}
  • 在module中的build.gradle中使用狀況以下:
apply plugin : 'com.android.library'
android {
    compileSdkVersion COMPILE_SDK_VERSION
    buildToolsVersion BUILD_TOOLS_VERSION
    defaultConfig {
        minSdkVersion MIN_SDK_VERSION
        targetSdkVersion TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0.0"
    }
    buildTypes {
        release {
            minifyEnabled MINIFY_DISABLED
            proguardFiles getDefaultProguardFile('proguard-android.txt' ), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir : 'libs', include: ['*.jar' ])
    testCompile "junit:junit: ${JUNIT_VERSION} "
    compile "com.android.support:appcompat-v7: ${APPCOMPAT_VERSION} "
}

 

方法三:在gradle.properties中配置依賴常量

  • 如:在gradle.properties中配置相應的版本號(能夠理解爲定義一個常量參數)

  • 在單個module中的build.gradle中使用以下:

相關文章
相關標籤/搜索