2

所以我正在尝试使用新的 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"
}

有趣的是,如果从 root 使用类路径,它就可以工作build.gradle.kts。有谁知道为什么?

4

2 回答 2

2

任何 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将更新的(假的)输出idartifact映射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确实是解决方法,而不是答案。

于 2022-02-16T03:17:07.197 回答
0

此问题是由 AppGallery Connect 的集成配置引起的。不知道你集成了HMS Core的哪个服务,但是配置AppGallery Connect和集成HMS Core SDK的过程是类似的。

Account Kit为例,可以参考这个文档或者这个文档进行集成。

以下部分需要特别注意: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

您也可以参考github demo中集成 AGCP Kotlin 的示例代码。

在此处输入图像描述

在此处输入图像描述

请参考文档的每个步骤重新集成。如果您在此过程中有任何问题,请随时与我们联系。

于 2022-02-15T10:29:03.927 回答