Android Studio统一依赖管理Composing builds,2021最新网易Android面经

本文介绍了如何利用Composing builds特性来统一管理Android Studio的依赖,从而实现单向跟踪、自动补全,并避免整个项目的重新构建。具体步骤包括创建VersionPlugin模块,定义依赖配置,实现Plugin接口,然后在项目中应用该插件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 支持Android Studio的代码补全,以下演示示例图来自于网络

在这里插入图片描述

使用方式可参考:Kotlin + buildSrc for Better Gradle Dependency Management

缺点:buildSrc 依赖更新将重新构建整个项目,项目越大,重新构建的时间就越长,造成不必要的时间浪费。

三、Composing builds


Composing builds:A composite build is simply a build that includes other builds. In many ways a composite build is similar to a Gradle multi-project build, except that instead of including single projects, complete builds are included.(Composing builds只是包含其他构建的构建。 在许多方面,复合构建类似于Gradle多项目构建,不同之处在于,它包括完整的构建,而不是包括单个项目。)

使用这种方式的优点有:

1.支持单向跟踪

2.自动补全

3.依赖更新时,不会重新构建整个项目

使用方式

1.新建module,名为VersionPlugin(自起)

2.在新建的module下的build.gradle文件中,添加如下代码:

buildscript {

repositories {

jcenter()

}

dependencies {

// 因为使用的 Kotlin 需要需要添加 Kotlin 插件

classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10”

}

}

apply plugin: ‘kotlin’

apply plugin: ‘java-gradle-plugin’

repositories {

// 需要添加 jcenter 否则会提示找不到 gradlePlugin

jcenter()

google()

}

dependencies {

implementation gradleApi()

implementation “org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10”

}

compileKotlin {

kotlinOptions {

jvmTarget = “1.8”

}

}

compileTestKotlin {

kotlinOptions {

jvmTarget = “1.8”

}

}

gradlePlugin {

plugins {

version {

// 在 app 模块需要通过 id 引用这个插件

id = ‘com.controler.versionplugin’

// 实现这个插件的类的路径

implementationClass = ‘com.controler.versionplugin.VersionPlugin’

}

}

}

3.在 VersionPlugin/src/main/java/包名/ 目录下新建 DependencyManager.kt 文件,添加你的依赖配置,如:

package com.controler.versionplugin

/**

  • 配置和 build相关的

*/

object BuildVersion {

const val compileSdkVersion = 29

const val buildToolsVersion = “29.0.2”

const val minSdkVersion = 17

const val targetSdkVersion = 26

const val versionCode = 102

const val versionName = “1.0.2”

}

/**

  • 项目相关配置

*/

object BuildConfig {

//AndroidX

const val appcompat = “androidx.appcompat:appcompat:1.2.0”

const val constraintLayout = “androidx.constraintlayout:constraintlayout:2.0.4”

const val coreKtx = “androidx.core:core-ktx:1.3.2”

const val material = “com.google.android.material:material:1.2.1”

const val junittest = “androidx.test.ext:junit:1.1.2”

const val swiperefreshlayout = “androidx.swiperefreshlayout:swiperefreshlayout:1.1.0”

const val recyclerview = “androidx.recyclerview:recyclerview:1.1.0”

const val cardview = “androidx.cardview:cardview:1.0.0”

//Depend

const val junit = “junit:junit:4.12”

const val espresso_core = “com.android.support.test.espresso:espresso-core:3.0.2”

const val guava = “com.google.guava:guava:24.1-jre”

const val commons = “org.apache.commons:commons-lang3:3.6”

const val zxing = “com.google.zxing:core:3.3.2”

//leakcanary

const val leakcanary = “com.squareup.leakcanary:leakcanary-android:2.4”

//jetPack

const val room_runtime = “androidx.room:room-runtime:2.2.5”

const val room_compiler = “androidx.room:room-compiler:2.2.5”

const val room_rxjava2 = “androidx.room:room-rxjava2:2.2.5”

const val lifecycle_extensions = “android.arch.lifecycle:extensions:1.1.1”

const val lifecycle_compiler = “android.arch.lifecycle:compiler:1.1.1”

const val rxlifecycle = “com.trello.rxlifecycle3:rxlifecycle:3.1.0”

const val rxlifecycle_components = “com.trello.rxlifecycle3:rxlifecycle-components:3.1.0”

//Kotlin

const val kotlinx_coroutines_core = “org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7”

}

4.在 VersionPlugin/src/main/java/包名/ 目录下新建 VersionPlugin.kt,

实现Plugin接口,如下:

package com.controler.versionplugin

import org.gradle.api.Plugin

import org.gradle.api.Project

class VersionPlugin : Plugin{

override fun apply(p0: Project) {

}

companion object{

}

}

注:新建VersionPlugin module时,生成的其他无用资源可以删去,只保留需要的,如下:

在这里插入图片描述

5.在 settings.gradle 文件内添加 includeBuild ("VersionPlugin")',注意是includeBuild 哦~,Rebuild项目

6.后面就可以在需要使用的gradle文件中使用了,如
app下的build.gradle,在首行添加以下内容:

plugins {

// 在第二部定义的插件ID

id “com.controler.versionplugin”

}

// 定义的依赖地址

import com.controler.versionplugin.*

使用如下

在这里插入图片描述
r.versionplugin"

}

// 定义的依赖地址

import com.controler.versionplugin.*

使用如下

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值