编程人 cdmana.com

Gradle configuration management of Android project

Android Studio use Gradle Build the project ,Gradle It's an open source build automation tool .Gradle Build scripts using Groovy and Kotlin DSL Compiling .

This article mainly introduces Android In the project build.gradle Configuration and how to define your own project build.gradle Content .

Catalog

build.gradle Configuration instructions

config.gradle Unified configuration management

Check for new version


build.gradle Configuration instructions

config.gradle Unified configuration management

(1) Usually a project has multiple modules (moudle), For each module build.gradle Configuration is similar to , In order to unify the management, the third-party library version is introduced , introduce config.gradle file , Create in the root directory config.gradle file

ext {

    android = [
            compileSdkVersion: 30,
            buildToolsVersion: "30.0.3",
            minSdkVersion    : 16,
            targetSdkVersion : 30,
            applicationId    : "com.wolve.yfb",
            versionCode      : 1,
            versionName      : "1.0"
    ]

    androidXVersion = [
            core_ktx         : "1.3.0",
            appcompat        : "1.2.0",
            constraint_layout: "2.0.4",
    ]

    version = [
            gradle        : "4.1.1",
            kotlin_version: "1.4.21",
            material      : "1.1.0",

            junit         : "4.13.1",
            ext_junit     : "1.1.1",
            espresso_core : "3.2.0"
    ]

    dependencies = [
            "gradle"           : "com.android.tools.build:gradle:${version.gradle}",
            //kotlin
            "kotlin-stdlib"    : "org.jetbrains.kotlin:kotlin-stdlib:${version.kotlin_version}",
            "core-ktx"         : "androidx.core:core-ktx:${androidXVersion.core_ktx}",
            //support
            "appcompat"        : "androidx.appcompat:appcompat:${androidXVersion.appcompat}",
            "material"         : "com.google.android.material:material:${version.material}",
            //view
            "constraint-layout": "androidx.constraintlayout:constraintlayout:${androidXVersion.constraint_layout}",
            //test
            "junit"            : "junit:junit:${version.junit}",
            "ext-junit"        : "androidx.test.ext:junit:${version.ext_junit}",
            "espresso-core"    : "androidx.test.espresso:espresso-core:${version.espresso_core}",
    ]
}

(2) root directory build.gradle Top increase apply from: 'config.gradle'

(3) stay moudle Introduced in the module , Better for a single reference

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    def android = rootProject.ext.android
    compileSdkVersion android.compileSdkVersion
    buildToolsVersion android.buildToolsVersion

    defaultConfig {
        applicationId android.applicationId
        minSdkVersion android.minSdkVersion
        targetSdkVersion android.targetSdkVersion
        versionCode android.versionCode
        versionName android.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    def dependencies = rootProject.ext.dependencies
    implementation dependencies["kotlin-stdlib"]
    implementation dependencies["core-ktx"]
    implementation dependencies["appcompat"]
    implementation dependencies["material"]
    implementation dependencies["constraint-layout"]

    testImplementation dependencies["junit"]
    androidTestImplementation dependencies["ext-junit"]
    androidTestImplementation dependencies["espresso-core"]
}

(4) Group references depend on , This is suitable for a kind of network library, etc

androidx = [
            //kotlin
            "kotlin-stdlib"    : "org.jetbrains.kotlin:kotlin-stdlib:${version.kotlin_version}",
            "core-ktx"         : "androidx.core:core-ktx:${androidXVersion.core_ktx}",
            //support
            "appcompat"        : "androidx.appcompat:appcompat:${androidXVersion.appcompat}",
            "material"         : "com.google.android.material:material:${version.material}",
            "startup-runtime"  : "androidx.startup:startup-runtime:${androidXVersion.startup_runtime}",
            //view
            "constraint-layout": "androidx.constraintlayout:constraintlayout:${androidXVersion.constraint_layout}",
    ]

    test = [
            "junit"        : "junit:junit:${version.junit}",
            "ext-junit"    : "androidx.test.ext:junit:${version.ext_junit}",
            "espresso-core": "androidx.test.espresso:espresso-core:${version.espresso_core}",
    ]

    test_deps = test.values()
    androidx_deps = androidx.values()
implementation rootProject.ext.androidx_deps
implementation rootProject.ext.test_deps

Check for new version

Third party libraries are introduced and updated from time to time , You don't need to check the official website for version updates , stay android studio Right click on the item , choice Open Module Settings, choice Dependencies Options

Single view version information access https://mvnrepository.com/, For example. appcompat

Scroll to Top