About the use of config.gradle

Keywords: Android Gradle Retrofit OkHttp

Last night, I read this article and mentioned config.gradle, such a configuration, so today I found a DEMO to try, record and summarize it.

This diagram is a directory structure

 

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"]
}

 

 

The advantage of using this method is that when the Android Support Repository is updated,
Prompt can be displayed directly in gradle file, with prompt and unified management of dependent version number, achieving two goals with one stone

Note: to use this config.gradle here, we need to add 「 in build.gradle under project.
  apply from : "config.gradle"
In this way, when using dependencies in library and app build.gradle, you can rely as above.

 

Objective:

To avoid the need to modify the build.gradle file in each module (such as appcompat-v7 package) when relying on the package to produce a new version, this method can be used only once to facilitate maintenance and management.

Posted by maya28 on Thu, 31 Oct 2019 08:50:07 -0700