gradle统一管理版本号

更多方式可以查看 https://blog.csdn.net/gao_chun/article/details/58105089

1.首先第一步在根目录下新建一个.gradle文件,如config.gradle如下图所示

在文件中添加自己想要维护的各类版本号,分类可以根据自己喜好定义,举例如下

ext{

    android = [
            compileSdkVersion  : 28,
            applicationId : "doubles.test",
            minSdkVersion : 18,
            targetSdkVersion : 28,
            versionCode : 1,
            versionName : "1.0",
            testInstrumentationRunner : "android.support.test.runner.AndroidJUnitRunner",
    ]

    version = [
            androidSupportSdkVersion    : "28.0.0",
            constraintlayoutSdkVersion  : "1.1.3",
            junitSdkVersion             : "4.12",
            runnerSdkVersion            : "1.0.2",
            espressocoreSdkVersion      : "3.0.2"
    ]

    dependencies = [
//                implementation fileTree(include: ['*.jar'], dir: 'libs')
                "appcompat-v7"        : "com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}",
                "recyclerview-v7"     : "com.android.support:recyclerview-v7:${version["androidSupportSdkVersion"]}",
                "cardview-v7"         : "com.android.support:cardview-v7:${version["androidSupportSdkVersion"]}",
                "constraint-layout"   : "com.android.support.constraint:constraint-layout:${version["constraintlayoutSdkVersion"]}",
                "junit"               : "junit:junit:${version["junitSdkVersion"]}",
                "runner"              : "com.android.support.test:runner:${version["runnerSdkVersion"]}",
                "espresso-core"       : "com.android.support.test.espresso:espresso-core:${version["espressocoreSdkVersion"]}",
    ]
}

2.第二步在根目录下的build.gradle文件中引用创建的配置文件,如下图所示

在build.gradle文件中引用

3.第三步,如下所示,可以通过rootProject引用自己的定义的版本

android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]
    defaultConfig {
        applicationId rootProject.ext.android["applicationId"]
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]
        testInstrumentationRunner rootProject.ext.android["testInstrumentationRunner"]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation rootProject.ext.dependencies["appcompat-v7"]
    implementation rootProject.ext.dependencies["constraint-layout"]
    testImplementation rootProject.ext.dependencies["junit"]
    androidTestImplementation rootProject.ext.dependencies["runner"]
    androidTestImplementation rootProject.ext.dependencies["espresso-core"]
    implementation rootProject.ext.dependencies["cardview-v7"]
    implementation rootProject.ext.dependencies["recyclerview-v7"]
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值