Android学习

关于组件化开发使用 gradle统一配置版本,依赖

2017-06-10  本文已影响31人  前行的乌龟

目前组件化,模块化发展如火如荼,基本上现在大伙都在用了,那么不可避免的会有很多 library,module,那么版本配置,调试配置怎么才能做到统一配置

对于版本号,编译tools 版本等一些 app 基本配置,我们有种比较通用的方案。在app 项目根目录中新建一个 config.gradle 文件,写入一种通用配置信息,子 moudle 和 library 是可以访问到项目根路径的这个 config.gradle 文件 的。

Snip20170610_1.png

下面我们看看如何将所有配置版本信息统一处理。

Snip20170610_2.png
apply from: "config.gradle"

②:在app与libs的build.gradle 引用我们的的路径配置。如图:
这里写图片描述

Snip20170610_3.png

至此,大功告成,我们所有的libs均可采用此种配置,一次配置,终身享受。


更好一点的版本,依赖统一写法

也是在 根目录下 新建一个 gradle 脚本 : config.gradle ,期中写法如下,分的比较好,各部分都很清楚

ext {

    android = [
            compileSdkVersion      : 25,
            buildToolsVersion      : "25.0.2",
            applicationId          : "com.will.weiyue",
            minSdkVersion          : 19,
            targetSdkVersion       : 25,
            versionCode            : 1,
            versionName            : "1.0"
    ]

    dependVersion = [
            support: "25.3.1"
    ]

    dependencies = [
            //android-support
            "support-v4"            : "com.android.support:support-v4:${dependVersion.support}",
            "appcompat-v7"          : "com.android.support:appcompat-v7:${dependVersion.support}",
            "design"                : "com.android.support:design:${dependVersion.support}",
            "recyclerview"          : "com.android.support:recyclerview-v7:${dependVersion.support}",
            "cardview"              : "com.android.support:cardview-v7:${dependVersion.support}",

            //http
            "retrofit2"             : "com.squareup.retrofit2:retrofit:2.1.0",
            "converter-scalars"     : "com.squareup.retrofit2:converter-scalars:2.1.0",
            "converter-gson"        : "com.squareup.retrofit2:converter-gson:2.1.0",
            "adapter-rxjava2"       : "com.squareup.retrofit2:adapter-rxjava2:2.2.0",

            //httpcache
            "rxCache"               : "com.github.VictorAlbertos.RxCache:runtime:1.8.0-2.x",
            "Jolyglot-gson"         : "com.github.VictorAlbertos.Jolyglot:gson:0.0.3",

            //rxjava
            "rxjava"                : "io.reactivex.rxjava2:rxjava:2.0.6",
            "rxandroid"             : "io.reactivex.rxjava2:rxandroid:2.0.1",
            "rxbinding2"            : "com.jakewharton.rxbinding2:rxbinding:2.0.0",
            rxlifecycle2            : "com.trello.rxlifecycle2:rxlifecycle:2.1.0",
            rxlifecomponents        : "com.trello.rxlifecycle2:rxlifecycle-components:2.1.0",

            //dagger2
            "dagger"                : "com.google.dagger:dagger:2.7",
            "dagger-compiler"       : "com.google.dagger:dagger-compiler:2.7",

            //ui
            "constraint-layout"     : "com.android.support.constraint:constraint-layout:1.0.2",
            "fragmentation"         : "me.yokeyword:fragmentation:1.0.0",
            "butterknife"           : "com.jakewharton:butterknife:8.8.1",
            "butterknife-compiler"  : "com.jakewharton:butterknife-compiler:8.8.1",
            "ultra-ptr"             : "in.srain.cube:ultra-ptr:1.0.11",
            "bottom-bar"            : "com.roughike:bottom-bar:2.3.1",
            "BaseAdapterHelper"     : "com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.24",
            "glide"                 : "com.github.bumptech.glide:glide:4.0.0-RC1",
            "banner"                : "com.youth.banner:banner:1.4.9",
            "viewanimator"          : "com.github.florent37:viewanimator:1.0.5",
            "tablayout"             : "com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar",
            "FlycoDialog"           : "com.flyco.dialog:FlycoDialog_Lib:1.3.2@aar",
            "richtext"              : "com.zzhoujay.richtext:richtext:2.5.2",
            "transformations"       : "jp.wasabeef:glide-transformations:2.0.2",
            "PhotoView"             : "com.github.chrisbanes:PhotoView:2.1.2",
            "videoplayer"           : "fm.jiecao:jiecaovideoplayer:5.7",
            "lottie"                : "com.airbnb.android:lottie:2.1.0",
            "progressbar"           : "me.zhanghai.android.materialprogressbar:library:1.4.1",
            "transitionhelper"      : "me.immortalz:transitionhelper:1.0.6",
            "swipebacklayout"       : "cn.bingoogolapple:bga-swipebacklayout:1.1.0@aar",
            "gif-drawable"          : "pl.droidsonroids.gif:android-gif-drawable:1.2.8",

            //Event
            "androideventbus"       : "org.simple:androideventbus:1.0.5.1",

            //db
            "litepal"               : "org.litepal.android:core:1.5.1",
            "retrolambda"           : "me.tatarka:gradle-retrolambda:3.7.0",

    ]
}

具体 module 的 gradle 脚本如下:

apply plugin: 'com.android.application'
//apply plugin: 'me.tatarka.retrolambda'

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

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

//    compileOptions {
//        sourceCompatibility JavaVersion.VERSION_1_8
//        targetCompatibility JavaVersion.VERSION_1_8
//    }

    buildToolsVersion '26.0.2'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile rootProject.ext.dependencies["appcompat-v7"]
    compile rootProject.ext.dependencies["support-v4"]
    compile rootProject.ext.dependencies["cardview"]
    compile rootProject.ext.dependencies["design"]
    compile rootProject.ext.dependencies["recyclerview"]
    compile rootProject.ext.dependencies["retrofit2"]
    compile rootProject.ext.dependencies["converter-scalars"]
    compile rootProject.ext.dependencies["converter-gson"]
    compile rootProject.ext.dependencies["adapter-rxjava2"]
    compile rootProject.ext.dependencies.rxlifecycle2
    compile rootProject.ext.dependencies.rxlifecomponents
    compile rootProject.ext.dependencies["dagger"]
    annotationProcessor rootProject.ext.dependencies["dagger-compiler"]

    compile rootProject.ext.dependencies["rxjava"]
    compile rootProject.ext.dependencies["rxandroid"]
    compile rootProject.ext.dependencies["rxbinding2"]
    compile rootProject.ext.dependencies["constraint-layout"]
    compile rootProject.ext.dependencies["ultra-ptr"]
    compile rootProject.ext.dependencies["fragmentation"]
    compile rootProject.ext.dependencies["butterknife"]
    annotationProcessor rootProject.ext.dependencies["butterknife-compiler"]
    compile rootProject.ext.dependencies["BaseAdapterHelper"]
    compile rootProject.ext.dependencies["glide"]
    compile rootProject.ext.dependencies["banner"]
    compile rootProject.ext.dependencies["viewanimator"]
    compile rootProject.ext.dependencies["FlycoDialog"]
    compile rootProject.ext.dependencies["PhotoView"]
    compile rootProject.ext.dependencies["videoplayer"]
    compile rootProject.ext.dependencies["lottie"]
    compile rootProject.ext.dependencies["progressbar"]
    compile rootProject.ext.dependencies["swipebacklayout"]
    compile rootProject.ext.dependencies["tablayout"]
    compile rootProject.ext.dependencies["androideventbus"]
    compile rootProject.ext.dependencies["gif-drawable"]

    compile rootProject.ext.dependencies["litepal"]
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}
repositories {
    mavenCentral()
}

添加 aar 依赖

在跟目录的 build.gradle 的 gradle编译脚本里添加本地依赖库地址,我的aar 放在 aar 文件夹里,aar 是只能当做本地依赖库来添加的,和 jar 不是一回事

allprojects {

    repositories {
        jcenter()
        flatDir {
            dirs 'aar'
        }
    }
}

使用依赖

compile(name: 'sweet-alert-dialog', ext: 'aar')

替换配置文件中的参数

gradle中写法

  manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yingyongbao"]

manifest 中的声明

  <meta-data
            android:name="UMENG_CHANNEL"
            android:value="${UMENG_CHANNEL_VALUE}"/>

gradle 中声明参数

// 注意字符串单引号里面要套双引号
buildConfigField 'String', 'app_name', '"HHHHHH"'

// 在 values 中声明参数
resValue "color", "flav_color", "#00ffff"

java 代码获取参数

Log.d("test", BuildConfig.app_name);

tx_info.setTextColor(getResources().getColor(R.color.flav_color));
上一篇下一篇

猜你喜欢

热点阅读