美文网首页@程序猿androidAndroid技术知识
使用config.gradle统一管理项目的依赖库

使用config.gradle统一管理项目的依赖库

作者: 葛糖糖 | 来源:发表于2017-06-27 11:29 被阅读179次

    当你看着你module中build.gradle躺着各种乱七八糟的依赖库时候是不是顿生无力感,想整理又不知道怎么有条有序,整理好又好怕再添加新的库的时候不知道放到哪里,好吧,这就是我的想法,


    by gxj

    所以见着build.gradle的时候我都是绕着走的...昨天正好又添加依赖库,我终于忍不了了,gg后发现一个叫config.gradle的东西貌似能解决这个问题,那还等什么,干它...


    by gxj

    新建config.gradle文件

    右击工程名称选择new->file->config.gradle,新建的config.gradle与你的module在同级目录中:


    by gxj

    这样使用config中的配置就可以管理所有module中的配置了.


    by gxj
    然后在项目的build.gradle文件中引入(注意是项目的build.gradle,而不是module的build.gradle)
      apply from: "config.gradle"
    
    (注意:如果如果工程项目下有使用plugins{}的话,要把apply from: "config.gradle"放在plugins{}后面,如果放在前面会报以下错误

    因为我的项目中使用了

      plugins {
          id "com.jfrog.bintray" version "1.7"
      }
    

    这样就不会报错了.)


    by gxj

    配置config.gradle

    没啥好说就是把用到库归类命名以下:

    ext {
    android = [
            compileSdkVersion: 23,
            buildToolsVersion: '25.0.1',
            applicationId    : "com.hsl.stock",
            minSdkVersion    : 15,
            targetSdkVersion : 23,
            versionCode      : 146,
            versionName      : "1.9.2.6.0"
    ]
    
    def dependVersion = [
            support    : "25.0.1",
            retrofit   : "2.2.0",
            okhttp     : "3.6.0",
            dagger2    : "2.0.2",
            butterknife: "7.0.1",
            blockcanary: "1.2.1",
            leakcanary : "1.4-beta2"
    ]
    
    dependencies = [
            //base
            "appcompat-v7"              : "com.android.support:appcompat-v7:${dependVersion.support}",
            "cardview-v7"               : "com.android.support:cardview-v7:${dependVersion.support}",
            "recyclerview-v7"           : "com.android.support:recyclerview-v7:${dependVersion.support}",
            "design"                    : "com.android.support:design:${dependVersion.support}",
            "support-annotations"       : "com.android.support:support-annotations:${dependVersion.support}",
    
            //ui
            "PhotoView"                 : "com.commit451:PhotoView:1.2.4",
            "datetimepicker"            : "com.github.flavienlaurent.datetimepicker:library:0.0.2",
            "pickerview"                : "com.bigkoo:pickerview:1.0.2",
            "ctiao_dfm"                 : "com.github.ctiao:dfm:0.4.4",
            "BaseRecyclerViewAdapter"   : "com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22",
            "convenientbanner"          : "com.bigkoo:convenientbanner:2.0.5",
            "diegocarloslima_fgelv"     : "com.diegocarloslima:fgelv:0.1.+@aar",
    
            //rx
            "rxjava"                    : "io.reactivex.rxjava2:rxjava:2.0.7",
            "rxandroid"                 : "io.reactivex.rxjava2:rxandroid:2.0.1",
    
            //network
            "retrofit"                  : "com.squareup.retrofit2:retrofit:${dependVersion.retrofit}",
            "retrofit-converter-gson"   : "com.squareup.retrofit2:converter-gson:${dependVersion.retrofit}",
            "retrofit-adapter-rxjava2"  : "com.squareup.retrofit2:adapter-rxjava2:${dependVersion.retrofit}",
            "okhttp"                    : "com.squareup.okhttp3:okhttp:${dependVersion.okhttp}",
            "okhttp-logging-interceptor": "com.squareup.okhttp3:logging-interceptor:${dependVersion.okhttp}",
            "glide"                     : "com.github.bumptech.glide:glide:3.7.0",
            "glide-okhttp3-integration" : "com.github.bumptech.glide:okhttp3-integration:1.4.0@aar",
    
            //di
            "butterknife"               : "com.jakewharton:butterknife:${dependVersion.butterknife}",
            "butterknife-compiler"      : "com.jakewharton:butterknife-compiler:${dependVersion.butterknife}",
    
            //other
            "multidex"                  : "com.android.support:multidex:1.0.1",
            "logger"                    : "com.orhanobut:logger:1.15",
            "fragmentation"             : "me.yokeyword:fragmentation:0.10.7",
            "umeng_analytics"           : "com.umeng.analytics:analytics:latest.integration",
    
            //canary
            "blockcanary-android"       : "com.github.moduth:blockcanary-android:${dependVersion.blockcanary}",
            "blockcanary-no-op"         : "com.github.moduth:blockcanary-no-op:${dependVersion.blockcanary}",
            "leakcanary-android"        : "com.squareup.leakcanary:leakcanary-android:${dependVersion.leakcanary}",
            "leakcanary-android-no-op"  : "com.squareup.leakcanary:leakcanary-android-no-op:${dependVersion.leakcanary}"
    ]
    }
    

    在module的build.gradle中使用:

    1.编译版本
    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
        ndk {
            abiFilters "armeabi", "armeabi-v7a"
        }
    }
    
    2.依赖库
    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    /* base */
    compile rootProject.ext.dependencies["appcompat-v7"]
    compile rootProject.ext.dependencies["design"]
    compile rootProject.ext.dependencies["recyclerview-v7"]
    compile rootProject.ext.dependencies["cardview-v7"]
    compile rootProject.ext.dependencies["support-annotations"]
    
    /* ui */
    compile rootProject.ext.dependencies["PhotoView"]
    compile rootProject.ext.dependencies["datetimepicker"]
    compile rootProject.ext.dependencies["pickerview"]
    compile rootProject.ext.dependencies["ctiao_dfm"]
    compile rootProject.ext.dependencies["BaseRecyclerViewAdapter"]
    compile rootProject.ext.dependencies["convenientbanner"]                       
    compile rootProject.ext.dependencies["diegocarloslima_fgelv"]                 
    
    /* network */
    compile rootProject.ext.dependencies["glide"]
    
    /* di */
    compile rootProject.ext.dependencies["butterknife"]
    
    /* other */
    compile rootProject.ext.dependencies["multidex"]
    compile rootProject.ext.dependencies["logger"]
    compile rootProject.ext.dependencies["fragmentation"]
    compile rootProject.ext.dependencies["umeng_analytics"]
    }
    

    整个世界清净了,再也不烦了.哈哈....

    by gxj
    by gxj
    by gxj
    by gxj
    by gxj

    相关文章

      网友评论

        本文标题:使用config.gradle统一管理项目的依赖库

        本文链接:https://www.haomeiwen.com/subject/oqoscxtx.html