Cannot access compiled protobuf java class from one project in src/test of another project

Hi,

I have two gradle subprojects, where:

Subproject A: Compiles a .proto file into a Java class

Subproject B: Is Kotlin code dependent on the output Java class from Subproject A in both src/main and src/test.

After building Subproject A I successfully get the Java class from the .proto. I then added the file path to the main sourceset in Subproject B like below:

sourceSets {
      main {
          java {
              srcDirs(".../proto/main/java")
          }
      }
      test {
          java {
              srcDirs(".../proto/main/java")
          }
      }

main successfully builds, as compileKotlin succeeds, however, the compileTestKotlin step fails with:

Cannot access class ‘…’. Check your module classpath for missing or conflicting dependencies

It is implemented in my dependencies for Subproject B

dependencies {
    implementation(project(":SubProjectA"))

I have also tried to add it to testImplementation and it does not work either.

It seems that the src/test is not able to access the java class output from Subproject A, but src/main is? How do I give the src/test access to it so it will build? Thanks!

Actually, from reading what you wrote this sounds like a strange setup.
Shouldn’t A build source source files into class files, by having something like

sourceSets { 
    main {
        java {
            srcDir(theTaskGeneratingTheJavaSources)
        }
    }
}

and then B just normally depending on A not needing to reach into the other project to take source files from there?

A builds the .proto into java classes using

sourceSets {
    main {
        proto {
            srcDir("src/main/protobuf")
        }
    }
}

The java classes created from the protobuf in A is then in

A/build/generated/source/proto/main/java

B then requires those java classes in both src/main and src/test, so I put this path in the sourceSets of B like below, so it can access the classes

sourceSets {
      main {
          java {
              srcDirs(".../proto/main/java")
          }
      }
      test {
          java {
              srcDirs(".../proto/main/java")
          }
      }

src/main builds fine in B, the issue I’ve got is that src/test does not giving an error that:

Cannot access class ‘…’. Check your module classpath for missing or conflicting dependencies

Even though in the Intellij IDE there is no error shown in the code in src/test in B, only during compilation

Again, you are mixing up things.
A is not generating Java classes, but Java sources.
You should not use those sources in B by hard-coding a path.
If you would really want to use the sources, that A generated within B, you should at least do it properly and safely as documented at Sharing outputs between projects.

But still, I think you should instead configure A properly to also compile those classes and then just depend on A from B. And never hard-code paths like that, especially not paths that are outputs of other tasks or even other projects.