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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值