关于config.gradle 配置和使用

配置

新建config.gradle (和项目的build.gradle同级).

内容大体:

ext{

    android = [
            compileSdkVersion       : 28,
            buildToolsVersion       : "28.0.3",
            minSdkVersion           : 14,
            targetSdkVersion        : 28,
            versionCode             : 11,
            versionName             : "1.0"
    ]

    version = [
            retrofitSdkVersion      : "2.4.0",
            okhttpVersion              : "3.11.0"
    ]

    dependencies = [
            //rx
            "rxjava"        : "io.reactivex.rxjava2:rxjava:2.1.5",
            "rxandroid"     : "io.reactivex.rxjava2:rxandroid:2.0.1",

            //network
            "retrofit"                      : "com.squareup.retrofit2:retrofit:${version["retrofitSdkVersion"]}",
            "retrofit-converter-gson"       : "com.squareup.retrofit2:converter-gson:${version["retrofitSdkVersion"]}",
            "retrofit-converter-scalars"    : "com.squareup.retrofit2:converter-scalars:${version["retrofitSdkVersion"]}",
            "retrofit-adapter-rxjava"      : "com.squareup.retrofit2:adapter-rxjava:${version["retrofitSdkVersion"]}",
            "retrofit-adapter-rxjava2"     : "com.squareup.retrofit2:adapter-rxjava2:${version["retrofitSdkVersion"]}",
            "okhttp3"                       : "com.squareup.okhttp3:okhttp:${version["okhttpVersion"]}",
            "logging-interceptor"          : "com.squareup.okhttp3:logging-interceptor:${version["okhttpVersion"]}",

    ]
}

在项目的build.gradle文件中引入

apply from: "config.gradle"

使用

在module的build.gradle中:

apply plugin: 'com.android.application'

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // 这部分还没有配置,时间问题,就不配置了
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //net
    implementation rootProject.ext.dependencies["rxjava"]
    implementation rootProject.ext.dependencies["rxandroid"]
    implementation rootProject.ext.dependencies["retrofit"]
    implementation rootProject.ext.dependencies["retrofit-converter-gson"]
    implementation rootProject.ext.dependencies["retrofit-converter-scalars"]
    implementation rootProject.ext.dependencies["retrofit-adapter-rxjava"]
    implementation rootProject.ext.dependencies["retrofit-adapter-rxjava2"]
    implementation rootProject.ext.dependencies["okhttp3"]
    implementation rootProject.ext.dependencies["logging-interceptor"]

}

 

你可能感兴趣的:(Android进阶笔记)