How to apply Gradle plugin from another plugin?

I am trying to encapsulate an Android plugin in my own plugin, but when I try to apply the plugin assembly, it fails except:

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's how I use the Android plugin in my own plugin 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, the classpath does not load correctly, what am I doing wrong?

+10
source share
2 answers

I assume that at the time you would like to add the dependencies of the plugin to build the script, have already been resolved, so this will not work. You need to specify the plugin that you would like to apply as the script dependency itself.

It will work as follows:

 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, the android-plugin found, but it fails due to the fact that the groovy plugin was applied earlier and there was a conflict.

+7
source

Use the PluginManager project. For example, the war plugin retrieves the java plugin as follows:

 public class WarPlugin implements Plugin<Project> { // ... public void apply(final Project project) { project.getPluginManager().apply(org.gradle.api.plugins.JavaPlugin.class); // ... } // ... } 
+6
source

Source: https://habr.com/ru/post/979176/


All Articles