Gradle unified version number configuration

Basic knowledge and common configuration of Gradle

1, def definition variable

def versionMajor = 1
def v7Version = ‘2.0.3.RELEASE’

problem:

Why can't def method refer to def variable in gradle

2. Define variables in the gradle.properties file in the root directory

The gradle.properties file configuration in the project root directory:

# 应用版本名称
VERSION_NAME=1.0.0
# 应用版本号
VERSION_CODE=100
# 支持库版本
SUPPORT_LIBRARY=24.2.1

Use format:

  • project.xxxx
  • ${xxxx}
android {
    defaultConfig {
        applicationId project.APPLICATION_ID // lib项目不需要配置这一项
        versionCode project.VERSION_CODE as int
        versionName project.VERSION_NAME
    }
}
 
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //这里注意是双引号
    compile "com.android.support:appcompat-v7:${SUPPORT_LIBRARY}"
}

3. Define variables in the build.gradle file in the root directory

ext{
    // Sdk and tools
    minSdkVersion = 15
    targetSdkVersion = 26
    compileSdkVersion = 26 
    
    junitVersion = '4.12'
}

Use format:

  • rootProject.ext.xxxx
  • $rootProject.xxxx

Note: Use single quotation marks when declaring variables; but when quoting in dependencies, you must use double quotation marks; otherwise there is a warning.

defaultConfig {
    applicationId "com.example.test"
    minSdkVersion rootProject.ext.minSdkVersion        
}
        
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:appcompat-v7:$rootProject.v7Version"  
    testImplementation "junit:junit:$rootProject.junitVersion"
}

4. The alias of the specified group

When you declare ext {…} in the build.gradle file in the root directory, add a variable var , such as:


ext {
    var = [
            minSdkVersion        : 14,
            targetSdkVersion     : 25,
            supportLibraryVersion: "25.2.0"
    ]
}

Reference format:

  • var.xxxx
  • $var.xxxx
android {
    defaultConfig {
        versionCode 1
        versionName var.version
    }
    ......
}
 
dependencies {
    compile "com.android.support:appcompat-v7:$var.supportLibraryVersion"
    
}

5. Use an independent config.gradle configuration file

Or define a gradle configuration separately in the root directory,

ext {
    // 用于编译的SDK版本
    COMPILE_SDK_VERSION = 23
 
    // 用于Gradle编译项目的工具版本
    BUILD_TOOLS_VERSION = "24.0.2"
 
    // 目标版本
    APPCOMPAT_VERSION = 23
}

Then import the configuration file in the build.gradle file in the root directory:

apply from: "config.gradle"

It can also be directly referenced in the module (the path is different):

apply from : '../config.gradle'

Use format:

  • rootProject.xxxx
  • ${xxxx}
android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile "com.android.support:cardview-v7:${APPCOMPAT_VERSION}"
}

Note: Use single quotation marks when declaring variables; but when quoting in dependencies, you must use double quotation marks; otherwise there is a warning.

5. Reference link

https://blog.csdn.net/gao_chun/article/details/58105089 (recommended)

https://blog.csdn.net/u012982629/article/details/81121717

Published 45 original articles · Like 24 · Visits 50,000+

Guess you like

Origin blog.csdn.net/zhijiandedaima/article/details/90897018