Search code examples
javagradleintellij-ideagrpcproto

IntelliJ IDEA doesn't see classes generated from protobuf files in a subproject


I have a Gradle project with modules. moduleA contains only protobuf files and produces a jar file with classes generated from the .proto files. moduleB depends on the moduleA (implementation project(':moduleA')).

moduleA
│   build.gradle
│   src
│   └───main
│       └───proto  <-- proto file defining gRPC services
moduleB
│   build.gradle
│   src            <-- code dependent on classes generated from moduleA
build.gradle

The project works well if I build/run it from Gradle.

Problem: IntelliJ IDEA doesn't see the classes generated from moduleA in sources of moduleB (imports are red).

Question: How to make IntelliJ IDEA correctly recognize classes built from .proto files?

I am using IntelliJ IDEA 2020.2.4 (Ultimate Edition).


Solution

  • For the IDE to resolve classes and imports from dependant module these classes should exist and they must be located in dependant module's source directory. Looks like the classes are generated into a directory which is not recognized by IDE as a source directory. Try adding this generated directory as a Gradle source set. In moduleA's Gradel build file add:

    sourceSets {
        main {
            java {
                srcDirs = ['build/generated/source/proto/main/java']
            }
        }
    }
    

    where 'build/generated/source/proto/main/java' - the directory where sources are generated.

    There is a related issue for IntelliJ IDEA: IDEA-209418.