android - com.huawei.agconnect 插件未找到

标签 android gradle huawei-mobile-services huawei-developers appgallery-connect

所以我正在尝试使用新的 Kotlin DSL 语法将 AppGallery 连接 gradle 插件添加到我的 android 项目中。但我收到这样的错误:

org.gradle.internal.exceptions.LocationAwareException: Build file 'D:\#KERJAAN\devbase\sample\build.gradle.kts' line: 3
Plugin [id: 'com.huawei.agconnect', version: '1.6.3.300'] 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.huawei.agconnect:com.huawei.agconnect.gradle.plugin:1.6.3.300')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    Google
    MavenRepo
    maven(https://developer.huawei.com/repo/)
我所做的是在 settings.gradle.kts 中为这样的插件添加存储库:
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven { setUrl("https://developer.huawei.com/repo/") }
    }
}
并在应用程序的 build.gradle.kts 中添加这样的插件:
plugins {
   id("com.huawei.agconnect") version "1.6.3.300"
}
有趣的是,如果使用根目录下的类路径,它可以工作 build.gradle.kts .有谁知道为什么?

最佳答案

任何 Gradle 插件(这根本不是 AGC 特定的)只能在根项目级别加载,然后通常在模块级别应用。我刚刚尝试删除 buildscript block (类似于问题),这确实导致:

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将输出更新的(假的)idartifact测绘it.target :
[
    id: 'com.huawei.agconnect.agcp',
    version: '1.6.3.300',
    artifact: 'com.huawei.agconnect:agcp:1.6.3.300',
    apply: false
]
应用的时候还是要用真实的id :
apply plugin: 'com.huawei.agconnect'
agcp { enableAPMS true }

就是这样(从版本 1.6.3.300 开始)APMSTransform有一些检查,这需要明确地将 AGP 放在 classpath 上. buildscript block “几乎”过时,如果不是 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 build.gradle 中没有设置文件和
也是一个 URL 重写,它将处理包名的 .gradle.plugin后缀正确,以便 com.huawei.agconnect.gradle.pluginagcp将导致相同的包下载。 resolutionStrategy确实是解决方法,而不是答案。

关于android - com.huawei.agconnect 插件未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71121109/

相关文章:

android - 如何使用文件提供程序将视频从一个应用程序共享到另一个应用程序?

flutter - 如何使用Flutter华为Push Kit插件获取推送通知点击事件?

android - 有没有华为p30的模拟器

java - 增量并将增量保存在 SharedPreferences 中

android - "Phone"app如何显示不在通讯录中的联系人信息?

android - 任务 ':app:buildNative' 执行失败

android - 使用Gradle更新已创建的问题

java - 如何在 Gradle 中发布以编程方式操作(例如检测)的 jar?

javascript - 如何针对华为特定的用户代理

android - 这是即使在低内存压力下也能保持服务 Activity 的正确技巧吗?