0

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 ?

Rahul Katte
  • 103
  • 2
  • 10

1 Answers1

0

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.

SIMULATAN
  • 833
  • 2
  • 17
  • I get the difference between api and implementation. In my case, ProjectC does not depend "directly" on ProjectA. It only depends on projectB, so it should be enough for projectA to be a transitive dependency to it through ProjectB. Atleast that's what I think. – Rahul Katte Aug 01 '23 at 16:54
  • Yes, BUT it needs to be declared as an `api`. With `implementation`, ProjectC doesn't have access to `ProjectA`. – SIMULATAN Aug 02 '23 at 16:54
  • I understand that it will work if you either make the 'projectA' as api dependecy to projectB or just add projectA also as a impl dependency to projectC – Rahul Katte Aug 03 '23 at 06:36
  • My question was, why ? projectC does not directly depend on project A. It only depends on a static function of projectB. Another weird behavior I noticed is, if projectC uses a variable (static or instance) of the same class instead of the static function, it works fine without needing projectA as impl dependency to projectC or as api dependency to projectB. how do you explain that ? It looks like variable access is fine, method access is not – Rahul Katte Aug 03 '23 at 06:42