我们有一个 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/