dependency-analysis-gradle-plugin
dependency-analysis-gradle-plugin copied to clipboard
Use Gradle Version Catalogs if present
Is your feature request related to a problem? Please describe.
In a large project which makes use of version catalogs, it would be ideal if instead of suggestions like "add implementation 'androidx.activity:activity:1.4.0'" the plugin was able to resolve the dependency name from the gradle version catalog and instead report "add implementation libs.androidx.activity".
Describe the solution you'd like
If there is a version catalog present, instead of reporting the raw dependency, look for a match in the catalog's items. There are methods off VersionCatalog
which might be useful here.
Describe alternatives you've considered
The alternative would be not doing this, which is reasonable - and perhaps there is not the API support from Gradle itself to do this reverse lookup. It'd be unfortunate though, as this lookup would make the suggestions and automatic fixes much more usable for projects using version catalogs.
The plugin currently has an API that would let you do this yourself. See here.
Alternatively, PRs are welcome (ideally leveraging the above-mentioned API). I don't have any immediate plans to implement this myself.
For anyone interested, this is how you can add the dependencies in the version catalog:
dependencyAnalysis {
dependencies {
// Adds the defined aliases in the version catalog to be used when printing advice and rewriting build scripts.
def versionCatalogName = "libs"
def versionCatalog = project.extensions.getByType(VersionCatalogsExtension).named(versionCatalogName)
versionCatalog.getLibraryAliases().forEach({ String alias ->
map.put(versionCatalog.findLibrary(alias).get().get().toString(), "${versionCatalogName}.${alias}")
})
}
}