文章 16
浏览 11957
Android项目依赖进行管理

Android项目依赖进行管理

一、使用Groovy ext扩展函数来管理

在项目目录下新建config.gradle文件

 1ext {
 2    android = [
 3            compileSdkVersion: 29,
 4            buildToolsVersion: "29",
 5            minSdkVersion    : 17,
 6            targetSdkVersion : 26,
 7            versionCode      : 102,
 8            versionName      : "1.0.2"
 9    ]
10
11    version = [
12            appcompatVersion       : "1.1.0",
13            coreKtxVersion         : "1.2.0",
14            supportLibraryVersion  : "28.0.0",
15            androidTestVersion     : "3.0.1",
16            junitVersion           : "4.12",
17            glideVersion           : "4.11.0",
18            okhttpVersion          : "3.11.0",
19            retrofitVersion        : "2.3.0",
20            constraintLayoutVersion: "1.1.3",
21            gsonVersion            : "2.7",
22            rxjavaVersion          : "2.2.2",
23            rxandroidVersion       : "2.1.0",
24            ..........省略...........
25    ]
26
27    dependencies = [
28            //base
29            "constraintLayout"      : "androidx.constraintlayout:constraintlayout:${version["constraintLayoutVersion"]}",
30            "appcompat"             : "androidx.appcompat:appcompat:${version["appcompatVersion"]}",
31            "coreKtx"               : "androidx.core:core-ktx:${version["coreKtxVersion"]}",
32            "material"              : "com.google.android.material:material:1.2.1",
33
34            //multidex
35            "multidex"              : "com.android.support:multidex:${version["multidexVersion"]}",
36
37            //okhttp
38            "okhttp"                : "com.squareup.okhttp3:okhttp:${version["okhttpVersion"]}",
39            "logging-interceptor"   : "com.squareup.okhttp3:logging-interceptor:${version["okhttpVersion"]}",
40
41            //retrofit
42            "retrofit"              : "com.squareup.retrofit2:retrofit:${version["retrofitVersion"]}",
43            "converter-gson"        : "com.squareup.retrofit2:converter-gson:${version["retrofitVersion"]}",
44            "adapter-rxjava2"       : "com.squareup.retrofit2:adapter-rxjava2:${version["retrofitVersion"]}",
45            "converter-scalars"     : "com.squareup.retrofit2:converter-scalars:${version["retrofitVersion"]}",
46			..........省略...........
47    ]
48}

然后在build.gradle添加
apply from:"config.gradle"
然后依赖使用

1dependencies {
2	...
3    // Retrofit + okhttp 相关的依赖包
4    api rootProject.ext.dependencies["retrofit"]
5    ...
6    }

二、使用buildSrc来管理

在项目目录下新建文件夹,名字一定要为buildSrc

rebuild一下

src--main--java--类s

build.gradle.kts

加入以下代码

1plugins {
2    `kotlin-dsl`
3}
4
5repositories {
6    mavenCentral()
7    google()
8}

如果遇到**’buildSrc’ cannot be used as a project name as it is a reserved name**错误

请在setting.gradle里面删除掉include'buildSrc'

然后在buildSrc里面新建各种管理类

最后在app build.gradle上import

 1object AndroidConfig {
 2    val compileSdkVersion = 30
 3    val buildToolsVersion = "30.0.3"
 4    val defaultConfig = DefaultConfig()
 5
 6    class DefaultConfig {
 7        val applicationId = "cn.udday.kaiyan"
 8        val minSdkVersion = 21
 9        val targetSdkVersion = 30
10        val versionCode = 1
11        val versionName = "1.0"
12    }
13}
1compileSdk AndroidConfig.compileSdkVersion
2    buildToolsVersion AndroidConfig.buildToolsVersion
3
4    defaultConfig {
5        applicationId AndroidConfig.defaultConfig.applicationId
6        minSdk AndroidConfig.defaultConfig.minSdkVersion
7        targetSdk AndroidConfig.defaultConfig.targetSdkVersion
8        versionCode AndroidConfig.defaultConfig.versionCode
9        versionName AndroidConfig.defaultConfig.versionName

使用Composing builds管理

新建module,名字为VersionPlugin(自起)
在新建的module下的build.gradle文件中,添加如下代码

 1buildscript {
 2    repositories {
 3        jcenter()
 4    }
 5    dependencies {
 6        // 因为使用的 Kotlin 需要需要添加 Kotlin 插件
 7        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
 8    }
 9}
10
11apply plugin: 'kotlin'
12apply plugin: 'java-gradle-plugin'
13
14repositories {
15    mavenCentral()
16    jcenter()
17    google()
18}
19
20dependencies {
21    implementation gradleApi()
22    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
23}
24
25compileKotlin {
26    kotlinOptions {
27        jvmTarget = "1.8"
28    }
29}
30compileTestKotlin {
31    kotlinOptions {
32        jvmTarget = "1.8"
33    }
34}
35
36gradlePlugin {
37    plugins {
38        version {
39            // 在 app 模块需要通过 id 引用这个插件
40            id = 'cn.udday.versionplugin'
41            // 实现这个插件的类的路径
42            implementationClass = 'cn.udday.versionplugin.VersionPlugin'
43        }
44    }
45}

VersionPlugin/src/main/java/包名/下新建DependencyManager.kt

 1package cn.udday.versionplugin
 2
 3object BuildVersion {
 4    const val compileSdkVersion = 30
 5    const val buildToolsVersion = "30.0.3"
 6    const val minSdkVersion = 25
 7    const val targetSdkVersion = 30
 8    const val versionCode = 1
 9    const val versionName = "1.0"
10}
11object BuildConfig {
12    //AndroidX
13    const val appcompat = "androidx.appcompat:appcompat:1.2.0"
14    const val coreKtx = "androidx.core:core-ktx:1.3.2"
15    const val material = "com.google.android.material:material:1.3.0"
16    const val constraintLayout = "androidx.constraintlayout:constraintlayout:2.0.4"
17    const val junittest = "androidx.test.ext:junit:1.1.2"
18    const val espressocore = "androidx.test.espresso:espresso-core:3.3.0"
19    const val junit = "junit:junit:4.13.2"
20    //Kotlin
21    const val kotlin_version = "1.5.10"
22    const val kotlinstdlib = "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
23}

VersionPlugin/src/main/java/包名/下新建VersionPlugin.kt
实现Plugin接口

 1package cn.udday.versionplugin
 2
 3import org.gradle.api.Plugin
 4import org.gradle.api.Project
 5
 6class VersionPlugin : Plugin<Project>{
 7    override fun apply(p0: Project) {
 8
 9    }
10
11    companion object{
12        
13    }
14}

module中的其他文件可以删除
保留.gradle build src .gitgnore build.gradle

settings.gradle文件删掉include'VersionPlugin',内添加includeBuild'VersionPlugin',然后rebuild一下

在需要的时候,在build.gradle首行添加

1plugins {
2    // 在之前定义的插件ID
3    id "cn.udday.versionplugin"
4}
5
6// 定义的依赖地址
7import cn.udday.versionplugin.*

使用

 1import cn.udday.versionplugin.*
 2
 3plugins {
 4    id 'com.android.application'
 5    id 'kotlin-android'
 6    id "cn.udday.versionplugin"
 7}
 8android {
 9    compileSdkVersion BuildVersion.compileSdkVersion
10    buildToolsVersion BuildVersion.buildToolsVersion
11
12    defaultConfig {
13        applicationId "cn.udday.mymodule"
14        minSdkVersion BuildVersion.minSdkVersion
15        targetSdkVersion BuildVersion.targetSdkVersion
16        versionCode BuildVersion.versionCode
17        versionName BuildVersion.versionName
18
19        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
20    }
21
22    buildTypes {
23        release {
24            minifyEnabled false
25            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26        }
27    }
28    compileOptions {
29        sourceCompatibility JavaVersion.VERSION_1_8
30        targetCompatibility JavaVersion.VERSION_1_8
31    }
32    kotlinOptions {
33        jvmTarget = '1.8'
34    }
35}
36
37dependencies {
38    implementation BuildConfig.kotlinstdlib
39    implementation BuildConfig.appcompat
40    implementation BuildConfig.coreKtx
41    implementation BuildConfig.material
42    implementation BuildConfig.constraintLayout
43    testImplementation BuildConfig.junit
44    androidTestImplementation BuildConfig.espressocore
45    androidTestImplementation BuildConfig.junittest
46 }

特点

1.使用groovy ext的方式,无法跟踪依赖关系,可读性差,不便维护
2.使用Kotlin + BuildSrc,支持双向跟踪,依赖更新时会重新构建整个项目
3.使用Composing builds,支持单向跟踪,依赖更新时不会构建整个项目


标题:Android项目依赖进行管理
作者:kuohai
地址:https://udday.cn/articles/2021/06/03/1622728098742.html

充实的一天

取消