libs.versions.toml文件用来抽离依赖来加载,文件由 4 个主要部分组成:

  • [versions]部分用于声明可以被依赖项引用的版本

  • [libraries]部分用于声明坐标的别名

  • [bundles]部分用于声明依赖包

  • [plugins]部分用于声明插件

[versions]
groovy = "3.0.5"
checkstyle = "8.37"

[libraries]
groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer="3.9" } }

[bundles]
groovy = ["groovy-core", "groovy-json", "groovy-nio"]

[plugins]
versions = { id = "com.github.ben-manes.versions", version = "0.45.0" }

使用:

dependencies {
    implementation libs.appcompat
    implementation libs.material
    implementation libs.constraintlayout
}

通过Android Studio 自动生成的可能会报错:

java.lang.RuntimeException: org.gradle.api.InvalidUserDataException: Invalid catalog definition:
  - Problem: In version catalog libs, invalid library alias 'XXpermissions'.
Library aliases must match the following regular expression: [a-z]([a-zA-Z0-9_.\-])+.
    
    Possible solution: Make sure the alias matches the [a-z]([a-zA-Z0-9_.\-])+ regular expression.

注意:不要使用驼峰命名方式,单词使用 - 分割,例如:

[versions]
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"

[libraries]
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐