zoukankan      html  css  js  c++  java
  • Gradle依赖的统一管理,解决依赖冲突

    看见别人在用implementation rootProject.ext.dependencies["xxxx"]不知道是什么意思,上网查了一下,原来是为了解决或者说预防gradle依赖冲突的问题。

    在项目开发中我们会经常引入多个Module,然而每个Module中又包含了V4、V7,为了升级新版本依赖包只用更改一次,我们决定采用Gradle依赖的统一管理,避免重复繁琐的劳动。

    记录get到的新知识,用法如下:
    1.在Project目录下新建config.gradle文件,文件名可自定义

    具体内容如下:

     1 ext {
     2 
     3     android = [
     4             compileSdkVersion       : 27,
     5             buildToolsVersion       : "27.0.0",
     6             minSdkVersion           : 21,
     7             targetSdkVersion        : 27,
     8             versionCode             : 6,
     9             versionName             : "1.2.2",
    10             renderscriptTargetApi   : 21
    11     ]
    12 
    13     version = [
    14             supportLibraryVersion   : "26.1.1",
    15             okhttpVersion           : "3.9.0",
    16             retrofitVersion         : "2.3.0",
    17             glideVersion            : "4.0.0",
    18             butterknifeVersion      : "8.8.1",
    19             fragmentationVersion    : "1.1.9",
    20     ]
    21 
    22     dependencies = [
    23             //base
    24             "appcompat-v7"                      : "com.android.support:appcompat-v7:${version["supportLibraryVersion"]}",
    25             "cardview-v7"                       : "com.android.support:cardview-v7:${version["supportLibraryVersion"]}",
    26             "design"                            : "com.android.support:design:${version["supportLibraryVersion"]}",
    27             "constraint-layout"                 : "com.android.support.constraint:constraint-layout:1.0.2",
    28 
    29             //net
    30             "gson"                              : "com.google.code.gson:gson:2.8.2",
    31             "okhttp"                            : "com.squareup.okhttp3:okhttp:${version["okhttpVersion"]}",
    32             "logging-interceptor"               : "com.squareup.okhttp3:logging-interceptor:${version["okhttpVersion"]}",
    33             "retrofit"                          : "com.squareup.retrofit2:retrofit:${version["retrofitVersion"]}",
    34             "converter-gson"                    : "com.squareup.retrofit2:converter-gson:${version["retrofitVersion"]}",
    35             "adapter-rxjava2"                   : "com.squareup.retrofit2:adapter-rxjava2:${version["retrofitVersion"]}",
    36 
    37             //dao
    38             "greendao"                          : "org.greenrobot:greendao:3.2.2",
    39 
    40             //rx
    41             "rxjava"                            : "io.reactivex.rxjava2:rxjava:2.1.5",
    42             "rxandroid"                         : "io.reactivex.rxjava2:rxandroid:2.0.1",
    43             "rxbinding"                         : "com.jakewharton.rxbinding2:rxbinding:2.1.0",
    44             "rxpermissions"                     : "com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar",
    45 
    46             //di
    47             "javax_annotation"                  : "org.glassfish:javax.annotation:10.0-b28",
    48             "butterknife"                       : "com.jakewharton:butterknife:${version["butterknifeVersion"]}",
    49             "butterknife-compiler"              : "com.jakewharton:butterknife-compiler:${version["butterknifeVersion"]}",
    50 
    51             //multidex
    52             "multidex"                          : "com.android.support:multidex:1.0.3",
    53 
    54             //kotlin
    55             "kotlin-stdlib"                     : "org.jetbrains.kotlin:kotlin-stdlib:1.2.10",
    56 
    57             //ui test
    58             "espresso-core"                     : "com.android.support.test.espresso:espresso-core:3.0.2",
    59             "espresso-idling-resource"          : "com.android.support.test.espresso:espresso-idling-resource:3.0.2",
    60 
    61             //unit test , 为了整合mockito和PowerMockito,mockito暂时最高只支持2.8.9
    62             "junit"                             : "junit:junit:4.12",
    63             "mockito"                           : "org.mockito:mockito-core:2.8.9",
    64             "powermock-module-junit4"           : "org.powermock:powermock-module-junit4:1.7.4"
    65 
    66     ]
    67 
    68 }

    2.在Project的build.gradle中添加

    apply from: "config.gradle"

    3.在modle的build.gradle中添加引用

     1 apply plugin: 'com.android.application'
     2 
     3 android {
     4     compileSdkVersion rootProject.ext.android["compileSdkVersion"]
     5     buildToolsVersion rootProject.ext.android["buildToolsVersion"]
     6 
     7     defaultConfig {
     8         applicationId "json.chao.com.wanandroid"
     9         minSdkVersion rootProject.ext.android["minSdkVersion"]
    10         targetSdkVersion rootProject.ext.android["targetSdkVersion"]
    11         versionCode rootProject.ext.android["versionCode"]
    12         versionName rootProject.ext.android["versionName"]
    13         //AndroidJunitRunner必须要显示指定在defaultConfig中,使用Gradle依赖管理无法使其生效
    14         testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    15         renderscriptTargetApi rootProject.ext.android["renderscriptTargetApi"]
    16         renderscriptSupportModeEnabled true    // Enable RS support
    17         multiDexEnabled true
    18         vectorDrawables.useSupportLibrary = true
    19     }
    20 }
    21 
    22 
    23     dependencies {
    24         implementation fileTree(include: ['*.jar'], dir: 'libs')
    25 
    26         //base
    27         implementation rootProject.ext.dependencies["appcompat-v7"]
    28         implementation rootProject.ext.dependencies["cardview-v7"]
    29         implementation rootProject.ext.dependencies["design"]
    30         implementation rootProject.ext.dependencies["constraint-layout"]
    31 
    32         //net
    33         implementation rootProject.ext.dependencies["gson"]
    34         implementation rootProject.ext.dependencies["okhttp"]
    35         implementation rootProject.ext.dependencies["retrofit"]
    36         implementation rootProject.ext.dependencies["converter-gson"]
    37         implementation rootProject.ext.dependencies["adapter-rxjava2"]
    38 
    39         //dao
    40         implementation rootProject.ext.dependencies["greendao"]
    41 
    42         //rx
    43         implementation rootProject.ext.dependencies["rxjava"]
    44         implementation rootProject.ext.dependencies["rxandroid"]
    45         implementation rootProject.ext.dependencies["rxbinding"]
    46         implementation rootProject.ext.dependencies["rxpermissions"]
    47 
    48         //UI测试
    49         androidTestImplementation (rootProject.ext.dependencies["espresso-core"]) {
    50             exclude group: 'com.android.support', module: 'support-annotations'
    51         }
    52         implementation (rootProject.ext.dependencies["espresso-idling-resource"]) {
    53             exclude module: 'support-annotations'
    54         }
    55 }

    (两个文件中有不对应的依赖方法,that's ok, 只是粘贴代码的时候删除了一些,知道用法就行了)

  • 相关阅读:
    第11组 Beta冲刺(1/5)
    第11组 Alpha事后诸葛亮
    第11组 Alpha冲刺(6/6)
    第11组 Alpha冲刺(5/6)
    第11组 Alpha冲刺(4/6)
    第11组 Alpha冲刺(3/6)
    毕设笔记
    软工实践个人总结
    第01组 Beta版本演示
    第01组 Beta冲刺(5/5)
  • 原文地址:https://www.cnblogs.com/Sharley/p/9625839.html
Copyright © 2011-2022 走看看