Gradle custom plugin : add dependency from extension object

前端 未结 4 583
走了就别回头了
走了就别回头了 2021-02-05 05:07

I\'m trying to write a plugin which adds dependencies to project.dependencies according to informations gathered in the plugin extension object. But it seems to be

相关标签:
4条回答
  • 2021-02-05 05:48

    The easiest way to do this:

    project.dependencies {
      delegate.compile("com.android.support:appcompat-v7:25.0.1")
    }
    
    0 讨论(0)
  • 2021-02-05 05:57

    I originally implemented this solution using the DependencyResolutionListener approach by Saad. However, the listener itself is called only when something iterates over the configuration associated with the dependency. For example, if you want to dynamically add a dependency to compile, you have to make sure that something later on does something like:

    project.configurations.compile.each {
       ...
    }
    

    But this is something that happens as a matter of course, since compile is a known configuration for any project that uses the java plugin. However, if you are using a custom configuration (as I was), then the listener approach won't work unless you explicitly iterate over your custom configuration.

    I was able to find a better way to do this, and within afterEvaluate as the OP originally wanted. I'm using a custom configuration here, but I don't see a reason why it wouldn't work for compile either:

    project.afterEvaluate {
        def version = project.myPlugin.version
        project.configurations.myConfig.dependencies.add(
            project.dependencies.add("myConfig", "org.foo:bar:$version")
        )
    }
    

    Of course, at some point something still has to iterate over the dependencies for them to actually get resolved.

    0 讨论(0)
  • 2021-02-05 05:57

    Don't know if that's still relevant, but you can workaround this by explicitly adding your compile configuration to Java classpath in doFirst:

    variant.javaCompile.doFirst {
        variant.javaCompile.classpath += project.configurations.myconfiguration
    }
    
    0 讨论(0)
  • 2021-02-05 05:58

    Update: I managed to figure this out since my original answer. The way do this is to add a DependencyResolutionListener in which you add the dependencies and then remove the listener so it doesn't try to add them on later resolution steps.

    compileDeps = project.getConfigurations().getByName("compile").getDependencies()
    project.getGradle().addListener(new DependencyResolutionListener() {
        @Override
        void beforeResolve(ResolvableDependencies resolvableDependencies) {
            compileDeps.add(project.getDependencies().create("org.foo:bar:$version"))
            project.getGradle().removeListener(this)
        }
    
        @Override
        void afterResolve(ResolvableDependencies resolvableDependencies) {}
    })
    

    I have a working example of a plugin that uses this here

    Original Answer:

    This is also late but for anyone dropping in. With the latest gradle (2.6 at the time of writing), you can add a DependencyResolutionListener and add any dependencies before dependencies are resolved.

    project.getGradle().addListener(new DependencyResolutionListener() {
        @Override
        void beforeResolve(ResolvableDependencies resolvableDependencies) {
            depsToAdd.each { dep ->
                compileConfig.getDependencies()
                    .add(project.getDependencies().create(dep))
            }
        }
    
        @Override
        void afterResolve(ResolvableDependencies resolvableDependencies) {
    
        }
    })
    

    However, as of this writing I was having some issues getting this to work with Android Studio IDE. The problem is tracked in my question here

    0 讨论(0)
提交回复
热议问题