从0到1打造一款安卓app之3-AndroidStudio-gradle统一依赖版本管理

178 阅读1分钟

AndroidStudio-gradle统一依赖版本管理

1.在项目根目录,新建versions.gradle

复制内容,并且按需要修改增加相应的版本

// Define versions in a single place
ext {
    // Sdk and tools
    // Support library and architecture components support minSdk 14 and above.
    minSdkVersion = 19
    targetSdkVersion = 31
    compileSdkVersion = 31

    coreKtxVersion = '1.7.0'
    utilcodexVersion = '1.31.0'

    androidXAppcompatVersion = '1.4.1'
    materialVersion = '1.5.0'
    junitVersion = '4.13.2'
    androidXTestExtJunitVersion = '1.1.3'
    androidXTestEspressoCoreVersion = '3.4.0'
    androidXConstraintlayoutVersion = '2.0.4'
}

2.versions.gradle引入到 项目根 build.gradle里

apply from: "versions.gradle"
println ext.properties
// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

println("root:build.gradle")
apply from: "versions.gradle"
println ext.properties

task clean(type: Delete) {
    delete rootProject.buildDir
}

在dependencies里引用

dependencies {
    implementation "androidx.core:core-ktx:${coreKtxVersion}"
    implementation "androidx.appcompat:appcompat:${androidXAppcompatVersion}"
    implementation "com.google.android.material:material:${materialVersion}"
    implementation "androidx.constraintlayout:constraintlayout:${randroidXConstraintlayoutVersion}"
    implementation "androidx.work:work-runtime-ktx:${androidXWorkVersion}"
    testImplementation "junit:junit:${junitVersion}"
    androidTestImplementation "androidx.test.ext:junit:${androidXTestExtJunitVersion}"
    androidTestImplementation "androidx.test.espresso:espresso-core:${androidXTestEspressoCoreVersion}"
}