Gradle buildSrc 和 buildscript

标签 gradle android-gradle-plugin

我们有一个 Gradle 构建,其中包括 buildSrc带有一些自定义插件。这些插件还适用于其他插件。例如,我们的插件适用于 com.android.tools.build:gradle .对于注释处理,该库需要在编译期间位于 Gradle 的类路径上。所以,把它放在我们的主要 build.gradle 中作品:

buildscript {
    repositories {
        google()
    }
    dependencies {
       classpath "com.android.tools.build:gradle:$gToolsVersion"
    }
}

但是,这意味着用户要应用此插件,他们必须 (1) 应用我们的插件和 (2) 添加 buildscript样板。好像应该没有这个必要。我们还可以添加 project.buildscript阻止在我们的插件中,但这似乎也是不必要的,并且由于这个错误是有问题的:https://developer.android.com/studio/build/gradle-plugin-3-0-0.html?utm_source=android-studio#known_issues .

我添加了 com.android.tools.build:gradle依赖于 buildSrc/build.gradle作为 runtime依赖性。看起来这应该可行:我认为这告诉 Gradle 为了运行我的插件,库(及其依赖项)需要在类路径上。然而,gradle buildEnvironment (以及我们的构建失败的事实)清楚地表明情况并非如此。

所以,问题:
  • runtime 和有什么区别在 buildSrc/build.gradle 中指定的依赖项和一个 classpath buildscript 中指定的依赖项在常规 build.gradle 中阻止?
  • 我如何安排事情以便用户可以应用来自 buildSrc 的插件并且不必还添加 buildscript阻止他们的 build.gradle ?
  • 最佳答案

    我遇到了一个稍微不同的问题,并找到了一个可接受的解决方案,可能有助于解决您的第二个问题:我想在 buildSrc/build.gradle 中应用相同的存储库。和两次在根 build.gradle .
    repositories.gradle在项目根目录中:

    repositories {
        if (project.hasProperty('nexus')) {
            maven {
                url 'http://localhost:8081/repository/JCenter/'
            }
            maven {
                url 'http://localhost:8081/repository/Maven_Google/'
            }
        } else {
            jcenter()
            google()
        }
    }
    
    ext {
        androidGradleBuildToolsDependency = 'com.android.tools.build:gradle:3.1.3'
    }
    
    buildSrc/build.gradle :
    buildscript {
        apply from: '../repositories.gradle'
    }
    
    allprojects {
        apply from: '../repositories.gradle'
    }
    
    dependencies {
        // androidGradleBuildToolsDependency is defined in repositories.gradle
        implementation androidGradleBuildToolsDependency
    }
    

    build.gradle :
    buildscript {
        apply from: 'repositories.gradle'
    }
    
    allprojects {
        // this line will also be executed from the build.gradles in subprojects, so the working
        // directory isn't always the same, so we use the absolute path here
        apply from: "${rootProject.projectDir}/repositories.gradle"
    }
    

    请注意,您不需要 classpath buildscript 内部的依赖关系根块 build.gradle像往常一样。 implementation repositories.gradle 中的依赖项似乎自动应用它。

    build.gradle 出现时,我的解决方案可能不起作用s 通过依赖项提供。

    关于Gradle buildSrc 和 buildscript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47644319/

    相关文章:

    caching - 有没有一种简单的方法可以从本地 gradle 缓存中删除一个依赖项?

    java - Gradle 同步失败 : A problem occurred configuring project ':lib'

    android - 未知的 'LibraryVariants' 属性 - Gradle 不会同步

    gradle - 如何抑制 gradle 的所有输出?

    gradle - SonarQube使用Gradle分析项目依赖性

    android - 如何使用 gradle 替换 Google Maps API key ?

    android - 使用 teamcity 使用 gradle 构建 android 项目时出错

    android - 无法在 Android Studio 中通过 ozodrukh 添加 CircularReveal 库

    gradle - 我怎样才能覆盖gradle中的源目录,但仅限于这个烦人的子项目

    android - 找不到检测目标软件包Firebase错误