关于config.gradle的使用

昨晚看书本文章,有提到config.gradle,这样一种配置,于是今天就找了个DEMO试了下,记录并总结了一下

这个图是目录结构

关于config.gradle的使用

 

config.gradle--project

ext {

    android = [
            compileSdkVersion: 28,
            buildToolsVersion: "28.0.3",
            applicationId    : "com.yangbin.footballnew",
            minSdkVersion    : 16,
            targetSdkVersion : 28,
            versionCode      : 1,
            versionName      : "1.0.0"
    ]
    dependencies = [
            "appcompat-v7"          : 'com.android.support:appcompat-v7:28.0.0',
            "support-design"        : 'com.android.support:design:+',

            //Rxjava
            "rxjava"                : "io.reactivex.rxjava2:rxjava:2.1.6",
            "rxandroid"             : "io.reactivex.rxjava2:rxandroid:2.0.1",
            "rxrelay"               : "com.jakewharton.rxrelay2:rxrelay:2.0.0",

            //Retrofit
            "retrofit"              : "com.squareup.retrofit2:retrofit:2.3.0",
            "retrofit-gson"         : "com.squareup.retrofit2:converter-gson:2.3.0",
            "retrofit-adapter"      : "com.squareup.retrofit2:adapter-rxjava2:2.3.0",
            "okhttp-log-interceptor": "com.squareup.okhttp3:logging-interceptor:3.8.0"

    ]
}

 

 

build.gradle--project

apply from: "config.gradle"
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

build.gradle--Module

apply plugin: 'com.android.application'
android {

    compileSdkVersion rootProject.ext.android["compileSdkVersion"]
    buildToolsVersion rootProject.ext.android["buildToolsVersion"]

    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
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
//        implementation project(':base')

    implementation rootProject.ext.dependencies["appcompat-v7"]
    implementation rootProject.ext.dependencies["support-design"]
    implementation rootProject.ext.dependencies["rxjava"]
    implementation rootProject.ext.dependencies["rxandroid"]
    implementation rootProject.ext.dependencies["rxrelay"]
    implementation rootProject.ext.dependencies["retrofit"]
    implementation rootProject.ext.dependencies["retrofit-gson"]
    implementation rootProject.ext.dependencies["retrofit-adapter"]
    implementation rootProject.ext.dependencies["okhttp-log-interceptor"]
}

 

 

使用这种方式的好处是当Android Support Repository有更新时 
可以直接在gradle文件中展现提示,又有提示,又能统一管理依赖版本号,一举两得

 注意:这里要在想使用这个config.gradle,我们要在project下的build.gradle中添加                         
  apply from : "config.gradle"
 这样在library,app的build.gradle中使用依赖时,就可以如上面的方式进行依赖。

 

目的:

避免在依赖包出新版本时,需要对每个module中的build.gradle文件都进行修改(如appcompat-v7包),使用这种方式即只需一次修改,方便维护,管理

上一篇:GreenDAO系列之(1)入门


下一篇:javascript – 使用jQuery在iframe中加载页面后,将CSS类添加到表行?