How to apply a Gradle plugin from another plugin?

后端 未结 2 1093
被撕碎了的回忆
被撕碎了的回忆 2021-02-08 08:42

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          


        
相关标签:
2条回答
  • 2021-02-08 08:45

    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.

    0 讨论(0)
  • 2021-02-08 08:46

    Use the project's PluginManager. For example, the war plugin pulls in the java plugin like this:

    public class WarPlugin implements Plugin<Project> {
        // ...
        public void apply(final Project project) {
            project.getPluginManager().apply(org.gradle.api.plugins.JavaPlugin.class);
            // ...
        }
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题