Gardle实现版本号统一管理

1 首先新建一个config.gradle

as切换到project模式下,项目上右击选在新建file,文件名填写config.gradle,要确保config.gradle文件和项目根build.gradle是在同一个层级。

2 配置config.gradle

以我的demo为例,config.gradle中填写如下配置信息

ext {
    //app及sdk版本
    android=[
            applicationId:"com.example.xxx",
            compileSdkVersion:29,
            buildToolsVersion:"29.0.2",
            minSdkVersion:15,
            targetSdkVersion:29,
            versionCode:1,
            versionName:"1.0",

            testInstrumentationRunner:"androidx.test.runner.AndroidJUnitRunner",
            consumerProguardFiles :"consumer-rules.pro"
    ]
    //三方开源库版本
    openlib=[
            butterknifeVersion : "10.0.0",
            butterknife_compiler : "10.0.0",
            retrofitVersion :"2.6.1",
            converterGsonVersion : "2.6.1",
            eventbusVersion :"3.1.1"

    ]
    //系统支持库版本
    support=[
            appcompat:"androidx.appcompat:appcompat:1.0.2",
            junit:"junit:junit:4.12",
            runner:"androidx.test:runner:1.2.0",
            espresso:"androidx.test.espresso:espresso-core:3.2.0"
    ]

    depend=[
            "butterknife":"com.jakewharton:butterknife:${openlib['butterknifeVersion']}",
            "butterknife_compiler":"com.jakewharton:butterknife-compiler:${openlib['butterknife_compiler']}",
            "retrofit":"com.squareup.retrofit2:retrofit:${openlib['retrofitVersion']}",
            "converterGson":"com.squareup.retrofit2:converter-gson:${openlib['converterGsonVersion']}",
            "eventbus":"org.greenrobot:eventbus:${openlib['eventbusVersion']}",
            "appcompat":"${support["appcompat"]}",
            "junit":"${support['junit']}",
            "runner":"${support['runner']}",
            "espresso":"${support['espresso']}"
    ]

}

3 module中引用config.gradle中的内容

首先在项目根gradle文件头部导入config.gradle

apply from : "config.gradle"

然后在module中使用

def cfg=rootProject.ext.android
def dependencie=rootProject.ext.depend

android {
    compileSdkVersion cfg.compileSdkVersion
    buildToolsVersion cfg.buildToolsVersion
    defaultConfig {
        applicationId cfg.applicationId
        minSdkVersion cfg.minSdkVersion
        targetSdkVersion cfg.targetSdkVersion
        versionCode cfg.versionCode
        versionName cfg.versionName

        testInstrumentationRunner cfg.testInstrumentationRunner
        consumerProguardFiles cfg.consumerProguardFiles
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':lib_common')

    //import openlib & supportlib
    implementation dependencie['appcompat']
    testImplementation dependencie['junit']
    androidTestImplementation dependencie['runner']
    androidTestImplementation dependencie['espresso']


    implementation dependencie['butterknife']
    annotationProcessor dependencie['butterknife_compiler']
    implementation dependencie['retrofit']
    implementation dependencie['converterGson']
    implementation dependencie['eventbus']
}

这样就可以实现版本库的统一管理,多module的情况下只需修改config配置文件就可以实现对各个module的版本管理了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值