我正在尝试将 android 插件封装在我自己的插件中,但是当我尝试应用我的插件构建失败并出现异常时:
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.
这是我如何在我自己的插件实现中应用 android 插件:
// 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'
}
}
}
由于某种原因,类路径没有被正确加载,我做错了什么?
最佳答案
我猜当时你想为构建脚本添加插件依赖项已经解决了,因此它不会那样工作。您需要指定您想要应用的插件作为脚本依赖项本身。
它会这样工作:
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'
}
}
}
现在,
android-plugin
已找到,但由于 groovy
而失败之前已经应用了插件并且存在冲突。
关于gradle - 如何从另一个插件应用 Gradle 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27318144/