任何 Gradle 插件(这根本不是 AGC 特定的)只能在根项目级别加载,然后通常在模块级别应用。我刚刚尝试删除该buildscript
块(类似于问题),这确实导致:
Plugin [id: 'com.huawei.agconnect', version: '1.6.3.300', apply: false] was not found in any of the following sources:
maven(https://developer.huawei.com/repo/)
Plugin Repositories (could not resolve plugin artifact 'com.huawei.agconnect:com.huawei.agconnect.gradle.plugin:1.6.3.300')
插件依赖不会解决,而pluginManagement
不断添加.gradle.plugin
. 如果存储库知道完整的名称而不仅仅是简写的名称agcp
,这应该是开箱即用的(这实际上是默认的预期包名称,除非更改它):
com.huawei.agconnect:com.huawei.agconnect.gradle.plugin:1.6.3.300
这不匹配:
com.huawei.agconnect:agcp:1.6.3.30
可以pluginManagement.resolutionStrategy
用作临时解决方法...
settings.gradle
用于重写错误假定的包名称:
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
google()
maven { url 'https://developer.huawei.com/repo/' }
}
plugins {}
resolutionStrategy {
eachPlugin {
if (it.requested.id.getNamespace() == 'com.huawei.agconnect') {
println ">> ${it.requested.id.id}"
if (it.requested.id.id == 'com.huawei.agconnect.agcp') {
it.useModule('com.huawei.agconnect:agcp:1.6.3.300')
}
println ">> ${it.target}"
} else {
println "> ${it.target}"
}
}
}
}
plugins
必须定义在build.gradle
:
plugins {
id "com.android.application" version "7.1.2" apply false
id "com.android.library" version "7.1.2" apply false
id "com.huawei.agconnect.agcp" version "1.6.3.300" apply false
}
println
将更新的(假的)输出id
到artifact
映射it.target
:
[
id: 'com.huawei.agconnect.agcp',
version: '1.6.3.300',
artifact: 'com.huawei.agconnect:agcp:1.6.3.300',
apply: false
]
应用它时,仍然需要使用 real id
:
apply plugin: 'com.huawei.agconnect'
agcp { enableAPMS true }
只是(从版本 1.6.3.300 开始)APMSTransform
有一些检查到位,这需要明确地将 AGP 放在classpath
. 该buildscript
块“几乎”已过时,如果不是APMSTransform
错误地假设它是唯一可以加载 Android Gradle 插件的地方。
/** Still required due to AGCP plugin. */
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
}
}
它还需要检查以下任何一个插件:
plugins {
id "com.android.application" version "7.1.2" apply false
id "com.android.library" version "7.1.2" apply false
}
例如:
project.getPluginManager().hasPlugin('com.android.application') || project.getPluginManager().hasPlugin('com.android.library')
为了使这项工作完美无缺(没有resolutionStrategy
),这将需要更新检查,以免获取com.android.tools.build:gradle is no set in the build.gradle file
和 URL 重写,这将正确处理包名称的.gradle.plugin
后缀,从而com.huawei.agconnect.gradle.plugin
导致agcp
相同的包下载。resolutionStrategy
确实是解决方法,而不是答案。