gradle中統一配置版本的小技巧。

gradle中統一配置版本的小技巧。

在Project/build.gradle中定義,在module/build.gradle中使用

一、直接在Project/build.gradle中定義和引用:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.compileSdkVersion = 26
    ext.targetSdkVersion = 26
    ext.support_appcompat_v7 = '26.1.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

...
複製代碼

數字引用:javascript

compileSdkVersion rootProject.ext.compileSdkVersion
複製代碼

三方庫版本號引用:java

implementation "com.android.support:appcompat-v7:$support_appcompat_v7"
複製代碼

二、單獨在xxx.gradle中定義和引用:

①在Project 層級下新建config.build文件(這裏的config能夠替換爲任何你喜歡的名字),在裏面書寫配置信息:android

ext {//定義全部project公用參數

    android = [
            compileSdk: 27,
            buildTools: "27.0.3",
            minSdk    : 19,
            minLimitSdk: 19,//限制低版本用戶安裝
            targetSdk : 27,
    ]

    dependencies = [
            // App dependencies
            junit                : '4.12',
            espresso             : '2.2.2',
            supportLibraryVersion: '27.1.1',
            supportPercentVersion: '25.3.1',
            butterknife          : '8.8.1',
            gson                 : '2.7',
            retrofit             : '2.4.0',
            rxjava               : '1.1.6',
            rxandroid            : '1.2.1',
            loggingInterceptor   : '3.1.0',
            stetho               : '1.4.2',
            guavaVersion         : '18.0',
            leakcanary           : '1.5.4'
    ]
}
複製代碼

②在Project/build.gradle中引用剛纔定義好的config.gradle文件:api

apply from: "config.gradle"
複製代碼

③數字引用:app

compileSdkVersion rootProject.ext.android.compileSdk
複製代碼

④三方庫版本號引用:gradle

api "com.android.support:appcompat-v7:$rootProject.ext.dependencies.supportLibraryVersion"
複製代碼

在Project/gradle.properties中配置,在mudule/build.gradle中使用.

①在Project/gradle.properties中定義:ui

COMPILE_SDK_VERSON = 26
BUILD_TOOLS_VERSION = 25.0.2

SUPPORTV7_VERSON=25.0.1
複製代碼

②引用到的變量默認是String類型,若是須要in類型,須要在後面添加 as int 聲明google

compileSdkVersion COMPILE_SDK_VERSON as int
buildToolsVersion BUILD_TOOLS_VERSION

compile "com.android.support:appcompat-v7:${SUPPORTV7_VERSON}"
複製代碼
相關文章
相關標籤/搜索