我有一个Gradle项目,我已经把它转移到了一个没有互联网接入的网络上.为了解决我的所有依赖项,我抓取了~/.gradle/caches/modules-2/files-2.1个文件,并将它们推送到我的网络的ArtiFactory.

然而,当我运行./gradlew build时,我得到错误

* What went wrong
Plugin [id: 'com.android.application', version: '8.2.1', 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.android.application:com.android.application.gradle.plugin:8.2.1')
  Searched in the following repositories:
    my-repo(https://my.artifactory/my-repo/gradle)

my-repo人中,我有以下几点:

gradle/
  com.android.application/
    com.android.application.gradle.plugin/
      8.2.1/
        <some hash>/
          com.android.application.gradle.plugin-8.2.1.pom

我怎么才能让Gradle找到插件呢?

推荐答案

You do already mirror dependencies into your Artifactory, but Gradle needs to be explicitly told to look in your Artifactory for plugins.
That should be possible by adding a pluginManagement block in your settings.gradle file:

pluginManagement {
    repositories {
        maven {
            url 'https://my.artifactory/my-repo/gradle'
        }
        gradlePluginPortal() // Optional: If you still want to resolve from Gradle Plugin Portal when online
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == 'com.android.application') {
                useModule("com.android.application:com.android.application.gradle.plugin:${requested.version}")
            }
        }
    }
}

有关Kotlin DSL(settings.gradle.kts),请参阅this example:

验证ArtiFactory中的构件的 struct 是否正确,以及是否存在所有必要的元数据,包括.pom个文件和任何.jar.aar个文件.Gradle依赖于这些元数据来解析依赖关系.

有关Kotlin DSL(settings.gradle.kts),请参见this example.

验证ArtiFactory中的构件的 struct 是否正确,以及是否存在所有必要的元数据,包括.pom个文件和任何.jar.aar个文件.Gradle依赖于这些元数据来解析依赖关系.

在运行构建时,您可以强制Gradle从本地缓存或您配置的存储库中解析依赖项,而无需try 使用--offline标志访问Internet:

./gradlew build --offline

有趣的是,当我以--scan运行时,它给出了一个不同的错误.也就是说,它现在找不到com.gradle.enterprise(不在ArtiFactory中).

The error related to com.gradle.enterprise not being found in Artifactory suggests that there might be additional plugins or dependencies not mirrored in your Artifactory. That plugin (the com.gradle.enterprise one) is often used for Gradle Enterprise or build scans, which helps in diagnosing build issues but also needs to be available in your repository if your build configuration requires it. You might need to add this plugin to your Artifactory or adjust your build configuration to not require it for offline builds.
If com.gradle.enterprise is essential for your build, apply a similar pluginManagement strategy to resolve this plugin from your Artifactory, similar to how you have done for com.android.application.
If the com.gradle.enterprise plugin is not relevant for your offline builds, consider conditionally applying this plugin based on whether an internet connection is available or not. That can be achieved with logic in your build.gradle or settings.gradle files to check for connectivity before applying the plugin.

我的目录 struct 正确吗?还是应该是com.android/application

在Maven存储库(ArtiFactory模拟的)中存储构件的约定通常使用正斜杠(/)来分隔路径中的组ID、构件ID和版本.然而,Gradle's plugin management系统对插件使用了略有不同的符号,这就是为什么您在构建脚本中使用类似com.android.application的ID来指定插件,但实际的构件可能存储在类似com/android/application/com.android.application.gradle.plugin/8.2.1/的路径中.重要的是要确保ArtiFactory反映Gradle所期望的 struct ,这通常意味着将插件ID转换为与Maven兼容的路径 struct .


所以,我在我原来的帖子中列出了目录 struct ?这就是Gradle所期望的吗?

您为ArtiFactory中的插件列出的目录 struct 是:

gradle/
  com.android.application/
    com.android.application.gradle.plugin/
      8.2.1/
        <some hash>/
          com.android.application.gradle.plugin-8.2.1.pom

它与Gradle在解析插件和依赖项时所期望的传统Maven存储库布局不一致.

Gradle在解析插件时需要一个类似Maven的 struct ,其中指向特定构件的路径是根据其组ID、构件ID和版本构建的.您描述的 struct 似乎是Gradle缓存 struct 的直接传输,这是不同的.

对于Gradle插件,预期的Maven存储库布局通常如下所示:

com/android/application/<version>/com.android.application-<version>.jar

或者,对于Gradle插件标记构件,Gradle使用该构件将插件ID映射到实际构件:

com/android/application/com.android.application.gradle.plugin/<version>/com.android.application.gradle.plugin-<version>.pom

.pom文件应该定义对实际插件JAR构件的依赖关系,然后将其存储在与第一个示例类似的路径中.

根据错误消息和您的描述,Gradle似乎无法解析插件,因为它正在寻找遵循此 struct 的标记构件,但在指定的存储库中找不到它.相反,ArtiFactory中的 struct 应该反映Maven布局,因为Gradle插件是以与Maven存储库兼容的方式发布的.

您可能需要重新组织Artifactory中的插件工件以匹配Maven struct .对于com.android.application插件版本8.2.1,Artifactory中的 struct 应该更像这样:

com/android/application/com.android.application.gradle.plugin/8.2.1/com.android.application.gradle.plugin-8.2.1.pom

并确保.pom文件指向正确的JAR文件,该文件也应该在您的ArtiFactory中插件的组和构件ID路径下可用.这可能需要手动将插件JAR及其.pom文件上传到ArtiFactory的正确位置,如果您还没有这样做的话.

请记住,实际插件的JAR文件也应该存在于存储库中,并被.pom文件正确引用,这可能需要在com/android/application/下为插件的JAR构件添加另一个条目,遵循Maven约定.

Android相关问答推荐

如何正确增加LazyStream中的变量

如何将Hilt添加到Android Studio中的Kotlin项目中?

处理Room数据库中的嵌套实体

无法在Android Gradle中同步Chaquopy版本

格雷德的两个星号是什么意思?非路径

Android:微调:在代码中设置ArrayAdapter不希望调用On ItemSelected,仅当用户单击微调时调用

保护所有程序包文件和类

Modifer.Align()不适用于行/列/框中的文本.未解决的作用域实例错误

触发PurchasesUpdatedListener回调时,billingClient.launchBillingFlow之前设置的成员变量丢失

从片段导航回来

Kotlin - 在继续之前如何等待这个协程完成?

具有数据库和升级潜力的移动应用程序开发(Android)供朋友使用

发布 MAUI 应用程序时出现遇到重复程序集错误

判断 AAR 元数据时发现 Android 问题:androidx.core:core:1.12.0-alpha01 和 androidx.core:core-ktx:1.12.0-alpha01

react 从输入中找到路径'lib/arm64-v8a/libfbjni.so'的本机2个文件

try 使用 ViewPager2 实现滑动视图时出现类型不匹配错误

为什么我在 Jetpack Compose 中被警告可选修饰符参数应该具有默认值修饰符?

在 jetpack compose 中使用 viewmodel 的最佳实践

当我更改 ViewModel var 时,Kotlin + Compose 中的 Composable 不会更新

即使我在单选按钮上明确设置了选中状态,RecyclerView 中的单选按钮也会随机取消选中