Gradle custom plugin : add dependency from extension object

前端 未结 4 592
走了就别回头了
走了就别回头了 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: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

提交回复
热议问题