Version catalog 依赖管理定义第三方插件问题记录

问题描述
使用version catalog管理依赖时遇到定义第三方插件时发现在libs.versions.toml里定义的插件无法使用,如定义以下plugin

[plugins]
drouter = { id = "com.didi.drouter",version="1.4.0" }

然后在project build.gradle.kts使用时发现找不到插件提示,如下

Plugin [id: 'com.didi.drouter', version: '1.4.0', apply: false] was not found in any of the following sources:

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'com.didi.drouter', version: '1.4.0', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.didi.drouter:com.didi.drouter.gradle.plugin:1.4.0')

即使引入buildscript 也无效

buildscript{
    repositories {
        mavenCentral()
    }
    dependencies{
        classpath(libs.drouter.plugin)
    }
}

解决方式
project settings.gradle.kts pluginManagement{}里加配置

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    resolutionStrategy {
        eachPlugin {
            println(requested)
            if (requested.id.id == "com.didi.drouter") {
                useModule("io.github.didi:drouter-plugin:${requested.version}")
            }
        }
    }
}

说明
根据官方解释是,当插件没发布到gradlePlugin插件中心时,无法通过id直接下载,这时需要我们替换为依赖的形式去下载,即useModule("io.github.didi:drouter-plugin:${requested.version}") }

官网文档说明

你可能感兴趣的:(android,gradle,versioncatalog,plugin)