# AndroidVersionCatalogsSample **Repository Path**: hljdrl/android-version-catalogs-sample ## Basic Information - **Project Name**: AndroidVersionCatalogsSample - **Description**: Android VersionCatalogs Sample 使用范例 gradle versionCatalogs sample 范例 gradle toml 范例 - **Primary Language**: Android - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://gitee.com/hljdrl - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-02 - **Last Updated**: 2024-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: versionCatalogs, Catalogs, gradleVersionCatalogs, Toml ## README # android gradle versionCatalogs项目高级应用 1. versionCatalogs基本使用,toml文件创建和引用:/gradle/catalogs/ toml文件 2. versionCatalogs在项目中引用示例:/gradle/catalogs/lib.versions.toml 3. versionCatalogs在项目中基于配置使用示例:/gradle/catalogs/config.versions.toml 和 app.versions.toml 4. versionCatalogs在项目中强制指定引用库版本: /gradle/catalogs/force.versions.toml 5. versionCatalogs在项目中强制过滤指定库: /gradle/catalogs/filter.versions.toml #versionCatalogs对项目中gradle版本有要求【切记踩坑】 ### settings.gradle ```gradle pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } versionCatalogs { for (file in fileTree("./gradle/catalogs").matching { include("**/*.toml") }) { println("fileItem--->" + file.toString()) String fileName = file.getName() println("fileName: " + fileName) String[] split = fileName.split("\\.") String name = split[0] println("create name : " + name) create(name) { from(files(file)) } } } } rootProject.name = "VersionCatalogs" include ':app' ``` ### app/build.gradle ```gradle plugins { id 'com.android.application' } android { namespace app.versions.appid.get() compileSdk config.versions.compilesdk.get().toInteger() defaultConfig { applicationId app.versions.appid.get() minSdk config.versions.minsdk.get().toInteger() targetSdk config.versions.targetsdk.get().toInteger() versionCode app.versions.appversion.code.get().toInteger() versionName app.versions.appversion.name.get() } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildFeatures { viewBinding true } } println(">") println(">") //TODO 遍历当前项目的TOML文件,所有的 def catalogNames = extensions.getByType(VersionCatalogsExtension).getCatalogNames() for (final def itemCateName in catalogNames) { println("> itemCateName > " + itemCateName) } println(">") println(">") //TODO 强制过滤 def filterCatalog = extensions.getByType(VersionCatalogsExtension).named("filter") //TODO 强制降级 def forceCatalog = extensions.getByType(VersionCatalogsExtension).named("force") configurations { configuration { println(">") println("> all*.exclude start") def aliases = filterCatalog.libraryAliases for (def itemIt in aliases) { filterCatalog.findLibrary(itemIt).ifPresent { def moduleItem = it.get().module if (DEBUG_LOG) { println("> all*.exclude item > " + moduleItem.group + ":" + moduleItem.name) } if (moduleItem.name != "0") { all*.exclude group: (moduleItem.group), module: (moduleItem.name) } else { all*.exclude group: (moduleItem.group) } } } println("> all*.exclude end") println(">") } println(">") println(">") all { def aliases = forceCatalog.libraryAliases for (def itemIt in aliases) { forceCatalog.findLibrary(itemIt).ifPresent { def moduleItem = it.get().module if (DEBUG_LOG) { println("> force library item > " + moduleItem.group + ":" + moduleItem.name + ":" + it.get().versionConstraint.requiredVersion) } resolutionStrategy.force(it) } } } println(">") } def mLibVersionCatalogs = extensions.getByType(VersionCatalogsExtension).named("lib") dependencies { //TODO 遍历指定TOML文件名引入 for (def itemIt in mLibVersionCatalogs.libraryAliases) { mLibVersionCatalogs.findLibrary(itemIt).ifPresent { def moduleItem = it.get().module println("> library item > " + moduleItem.group + ":" + moduleItem.name + ":" + it.get().versionConstraint.requiredVersion) implementation(it) } } implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.20") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.20") } ```