编程知识 cdmana.com

Android项目Gradle配置管理

Android Studio采用Gradle进行项目构建,Gradle是一个开源构建自动化工具。Gradle构建脚本是使用Groovy和Kotlin DSL编写的。

这篇文章主要介绍Android项目中build.gradle配置以及如何定义自己项目的build.gradle内容。

目录

build.gradle配置说明

config.gradle统一配置管理

检查新版本


build.gradle配置说明

config.gradle统一配置管理

(1)通常一个项目有多个模块(moudle),每个模块的build.gradle配置类似,为了统一管理引入第三方库版本等内容,引入config.gradle文件,在根目录下创建config.gradle文件

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)根目录build.gradle顶部增加 apply from: 'config.gradle'

(3)在moudle模块中引入,比较适合单个引用

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)成组引用依赖,这种适合一类比如网络库等

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

检查新版本

引入第三方库不定期会更新,不需要都官方网站查看版本更新情况,在android studio右键项目,选择Open Module Settings,选择Dependencies选项

单个查看版本信息访问https://mvnrepository.com/,比如查看appcompat

版权声明
本文为[osc_n1x6m26g]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4305397/blog/4939436

Scroll to Top