如何从另一个插件应用Gradle插件? [英] How to apply a Gradle plugin from another plugin?
问题描述
评估根项目myproj时发生问题。
>无法应用插件[id'com.mycomp.build']
>无法应用插件[id'android-library']
>没有找到id为'android-library'的插件。
这是我在自己的插件实现中应用android插件的方式:
// build.gradle
apply plugin:'groovy'
version ='1.0'
group ='com.mycomp'
依赖关系{
编译gradleApi()
编译localGroovy()
}
// Build。 groovy
package com.mycomp
import org.gradle.api.Plugin
import org.gradle.api.Project
class Build implements Plugin<项目> {
void apply(Project project){
println'Hello from com.mycomp.Build'
project.beforeEvaluate {
buildscript.configurations.classpath + =
' com.android.tools.build:gradle:1.0.0-rc1'
}
project.configure(project){
buildscript.repositories.mavenCentral()
apply plugin:'android-library'
}
}
}
出于某种原因,一个类路径没有正确加载,我在做什么错了?
时间您想添加构建脚本的插件依赖项已经解决,因此它不会以这种方式工作。您需要指定您希望作为脚本依赖项应用的插件。
它将以这种方式工作:
buildscript {
repositories {
mavenCentral()
}
依赖关系{
classpath'com .android.tools.build:gradle:1.0.0-rc1'
}
}
应用插件:'groovy'
apply plugin: Build
version ='1.0'
group ='com.mycomp'
依赖项{
编译gradleApi()
编译localGroovy( )
}
import org.gradle.api.Plugin
import org.gradle.api.Project
class Build implements Plugin< Project> {
void apply(Project project){
project.configure(project){
apply plugin:'android-library'
}
}
}
现在,找到 android-plugin
它因为 groovy
插件之前已经被应用并且存在冲突的事实而失败。
I'm trying to incapsulate android plugin in my own plugin, but when I'm trying to apply my plugin build fails with an exception:
A problem occurred evaluating root project 'myproj'.
> Failed to apply plugin [id 'com.mycomp.build']
> Failed to apply plugin [id 'android-library']
> Plugin with id 'android-library' not found.
Here is how I'm applying android plugin inside my own plugin's implementation:
// build.gradle
apply plugin: 'groovy'
version = '1.0'
group = 'com.mycomp'
dependencies {
compile gradleApi()
compile localGroovy()
}
// Build.groovy
package com.mycomp
import org.gradle.api.Plugin
import org.gradle.api.Project
class Build implements Plugin<Project> {
void apply(Project project) {
println 'Hello from com.mycomp.Build'
project.beforeEvaluate {
buildscript.configurations.classpath +=
'com.android.tools.build:gradle:1.0.0-rc1'
}
project.configure(project) {
buildscript.repositories.mavenCentral()
apply plugin: 'android-library'
}
}
}
For some reason a classpath is not being properly loaded, what am I doing wrong?
I guess that at the time You'd like to add the plugin dependencies for the build script have been already resolved, thus it won't work that way. You need to specify the plugin You'd like to apply as a script dependency itself.
It will work that way:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0-rc1'
}
}
apply plugin: 'groovy'
apply plugin: Build
version = '1.0'
group = 'com.mycomp'
dependencies {
compile gradleApi()
compile localGroovy()
}
import org.gradle.api.Plugin
import org.gradle.api.Project
class Build implements Plugin<Project> {
void apply(Project project) {
project.configure(project) {
apply plugin: 'android-library'
}
}
}
Now, android-plugin
is found but it fails because of the fact that groovy
plugin had been applied earlier and there's a conflict.
这篇关于如何从另一个插件应用Gradle插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!