44App依赖库版本统一管理

Posted 清风百草

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了44App依赖库版本统一管理相关的知识,希望对你有一定的参考价值。

App依赖库版本统一管理

1.场景

(1)项目存在多个module的场景,确保各个module所依赖的库版本一致,编译环境等一致。
(2)之前写了一篇引用gradle.properties文件中定义的版本号的文章,以下内容是引用自建的config.gradle文件中配置的版本号。

2.实现

2.1新建版本号的统一配置文件

(1)在项目根目录下新建一个叫config.gradle的文件.
(2)配置android节点相关版本信息
(3)配置dependencies依赖库相关版本信息

//1.版本号的统一配置
ext{
    //1.1android节点
    android = [
            //(1)应用程序包名
            applicationId:"com.gdc.mykotlin",
            //(2)用于编译的SDK版本
            compileSdkVersion:30,
            //(3)用于Gradle编译项目的工具版本
            buildToolsVersion:"30.0.3",
            //(4)最低支持Android版本
            minSdkVersion:14,
            //(5)目标版本
            targetSdkVersion:30,
            //(6)版本编号
            versionCode:1,
            //(7)版本编号描述
            versionName:"1.0",
            //(8)设置是否使用混淆
            minifyEnable : true,
            minifyDisabled : false,
            //(9)Java版本
            targetCompatibility : JavaVersion.VERSION_1_8,
            sourceCompatibility : JavaVersion.VERSION_1_8

    ]

    //1.1依赖库配置
    dependVersion = [
            //============1.Android核心依赖库=================
            //(1)core核心
            core_ktx:"1.5.0",
            //(2)appcompat兼容库
            appcompact:"1.3.0",
            //(3)约束布局
            constraint:"2.0.4",


            //============2.项目第三方依赖库=================

            //(1)网络请求
            retrofit:"2.3.0",
            okhttp:"3.9.0",


            //(2)RxJava
            rxjava:"2.1.3",
            rxandroid:"2.0.1",

            //(2.1)RxLifecycle解决RxJava内存泄漏
            rxlifecycle:"2.2.0",

            //(3)测试依赖库
            junit:"4.12",
            extjunit:"1.1.1",
            espresso_core:"3.3.0",

            //(4)material控件库
            material:"1.2.0",

            //(5)navgation库
            navigation:"2.3.5",

            //(6)Kotlin协程
            kotlinx_coroutines:"1.3.2",

            //(7)Facebook的Stetho开源调试平台库
            stetho:"1.4.2",
            stetho_okhttp3:"1.4.2",

            //(8)图片库
            glide:"4.11.0",

            //(9)room数据库
            room:"2.2.5",

            //(10)分包
            multidex:"2.0.1"
    ]

    dependencies = [
            //(1)Android核心依赖库
            core:"androidx.core:core-ktx:$dependVersion.core_ktx",
            appcompact:"androidx.appcompat:appcompat:$dependVersion.appcompact",
            constraint:"androidx.constraintlayout:constraintlayout:$dependVersion.constraint",


            //(2)网络请求
            retrofit:"com.squareup.retrofit2:retrofit:$dependVersion.retrofit",
            okhttp_logging_interceptor:"com.squareup.okhttp3:logging-interceptor:$dependVersion.okhttp",
            adapter_rxjava:"com.squareup.retrofit2:adapter-rxjava2:$dependVersion.retrofit",
            converter_gson:"com.squareup.retrofit2:converter-gson:$dependVersion.retrofit",

            stetho:"com.facebook.stetho:stetho:$dependVersion.stetho",
            okhttp:"com.facebook.stetho:stetho-okhttp3:$dependVersion.stetho_okhttp3",

            //(3)RxJava
            rxjava:"io.reactivex.rxjava2:rxjava:$dependVersion.rxjava",
            rxandroid:"io.reactivex.rxjava2:rxandroid:$dependVersion.rxandroid",

            //(4)RxLifecycle解决RxJava内存泄漏
            rxlifecycle_kotlin:"com.trello.rxlifecycle2:rxlifecycle-kotlin:$dependVersion.rxlifecycle",
            rxlifecycle_components:"com.trello.rxlifecycle2:rxlifecycle-components:$dependVersion.rxlifecycle",

            //(5)图片库
            glide:"com.github.bumptech.glide:glide:$dependVersion.glide",
            glide_compiler:"com.github.bumptech.glide:compiler:$dependVersion.glide",

            //(6)room数据库
            room_runtime:"androidx.room:room-runtime:$dependVersion.room",
            room_compiler:"androidx.room:room-compiler:$dependVersion.room",


            //(7)material控件库
            material:"com.google.android.material:material:$dependVersion.material",

            //(8)navigation库
            navigation_fragment_ktx:"androidx.navigation:navigation-fragment-ktx:$dependVersion.navigation",
            navigation_ui_ktx:"androidx.navigation:navigation-ui-ktx:$dependVersion.navigation",

            //(9)测试库
            junit:"junit:junit:$dependVersion.junit",
            extjunit:"androidx.test.ext:junit:$dependVersion.extjunit",
            espresso:"androidx.test.espresso:espresso-core:$dependVersion.espresso_core",

            //(10)kotlin协程
            kotlinx_coroutines_android:"org.jetbrains.kotlinx:kotlinx-coroutines-android:$dependVersion.kotlinx_coroutines",
            kotlinx_coroutines_core:"org.jetbrains.kotlinx:kotlinx-coroutines-core:$dependVersion.kotlinx_coroutines",

            //(11)multidex分包
            multidex:"androidx.multidex:multidex:$dependVersion.multidex"
    ]
}

2.2在project中的build.gradle中添加引用

// Top-level build file where you can add configuration options common to all sub-projects/modules.
//引入统一配置的版本号
apply from:"config.gradle"

buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

2.3在各个module的xxx.gradle文件中引用

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

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 "androidx.test.runner.AndroidJUnitRunner"
    }

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

    compileOptions {
        sourceCompatibility rootProject.ext.android.sourceCompatibility
        targetCompatibility rootProject.ext.android.targetCompatibility
    }

    /**
     * 分包的时候有可能会出现内存溢出错误,在此处指定内存,可以解决内存溢出问题
     */
    dexOptions {
        javaMaxHeapSize "2g"
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    implementation rootProject.ext.dependencies.core
    implementation rootProject.ext.dependencies.appcompact
    implementation rootProject.ext.dependencies.constraint
    implementation rootProject.ext.dependencies.navigation_fragment_ktx
    implementation rootProject.ext.dependencies.navigation_ui_ktx

    testImplementation rootProject.ext.dependencies.junit
    androidTestImplementation rootProject.ext.dependencies.extjunit
    androidTestImplementation rootProject.ext.dependencies.espresso

    //1.kotlin协程
    implementation rootProject.ext.dependencies.kotlinx_coroutines_android
    implementation rootProject.ext.dependencies.kotlinx_coroutines_core

    //2.stetho
    implementation rootProject.ext.dependencies.stetho
    implementation rootProject.ext.dependencies.okhttp


    //3.网络
    implementation([
            rootProject.ext.dependencies.okhttp_logging_interceptor,
            rootProject.ext.dependencies.retrofit,
            rootProject.ext.dependencies.adapter_rxjava,
            rootProject.ext.dependencies.converter_gson
    ])

    //4.RxJava
    implementation([
            rootProject.ext.dependencies.rxjava,
            rootProject.ext.dependencies.rxandroid
    ])

    //5.RxLifecycle解决RxJava内存泄漏
    implementation([
            rootProject.ext.dependencies.rxlifecycle_kotlin,
            rootProject.ext.dependencies.rxlifecycle_components
    ])

    //6.图片库
    implementation rootProject.ext.dependencies.glide
    implementation rootProject.ext.dependencies.glide_compiler

    //7.room数据库
    implementation rootProject.ext.dependencies.room_runtime
    annotationProcessor rootProject.ext.dependencies.room_compiler
    //Kotlin语言开发的Android应用,添加时需要在文件顶部加上添加插件语句apply plugin: 'kotlin-kapt'
    kapt rootProject.ext.dependencies.room_compiler

    //8.分包
    implementation rootProject.ext.dependencies.multidex

}

以上是关于44App依赖库版本统一管理的主要内容,如果未能解决你的问题,请参考以下文章

go包管理之glide

ruby gem 添加环境依赖库

Maven统一管理pom依赖版本的方法

如何管理在每个 git 版本中添加私有代码片段?

在不同BOM版本共存的情况下,如何做好BOM管理,防止文件混乱。

Android 重构 | 统一管理 Gradle 依赖版本