Why does Gradle module need direct dependency on another module on which it does not depend on directly?

43 Views Asked by At

I have a multimodule project like below

ProjectA
{
    interface IInterfaceA 
    {
        api1();
        api2();
    }
}


ProjectB
{
    class ClassB implements IInterfaceA
    {
        api1();
        api2();
        someStaticFunction();
    }
}
implementation (':ProjectA')


ProjectC
{
    class ClassC
    {
        ClassB.someStaticFunction();   ---> compile error, cannot access IInterfaceA
    }
}
implementation (':ProjectB')
//implementation (':ProjectA') ---> if I add this here, it works

Why though ? since project C is not depending directly on any implemetation of ProjectA, why does it still have to add ":ProjectA" as a direct dependency ?

1

There are 1 best solutions below

4
On

That's because you probably defined ProjectA as a dependency of ProjectB using implementation. This leads to ProjectA being accessible in ProjectB, but not in any modules depending on B.

api – used to make the dependencies explicit and expose them in the classpath. For instance, when implementing a library to be transparent to the library consumers'
implementation – required to compile the production source code and are purely internal. They aren't exposed outside the package

Taken from this baeldung article which outlines the different configuration types of gradle.