Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a Gradle plugin from another plugin?

I'm trying to encapsulate 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?

like image 398
Pood1331 Avatar asked Dec 05 '14 14:12

Pood1331


People also ask

When Gradle plugins are applied to a project?

Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)

What is apply from in Gradle?

#1 Use 'apply' to structure the script content In Gradle apply command can be used to apply not only plugins, but also script files (*). In this way you can divide your main build. gradle file into smaller parts, and move extra tasks like jacoco report and findbugs to the separate files.

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, script plugins and binary plugins.


Video Answer


1 Answers

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.

like image 119
Opal Avatar answered Nov 18 '22 10:11

Opal