简体   繁体   中英

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).

This is same as using proto generated code in the same module, it needs to be there first before it can be picked up by IDE.

Such dependencies cannot be resolved statically. Proto generated classes are available only after moduleA is built . You would need to build moduleA at least once (and refresh the IDE imports if necessary) to make its generated code reachable to moduleB .

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 .

The location of the classes generated from the .proto files is not known by Intellij out of the box. The 'com.google.protobuf' gradle plugin registers the source directories with the generated code for Intellij to see when the idea ' gradle plugin is applied in the build.gradle file of moduleA.

See IntelliJ IDEA tips for com.google.protobuf gradle plugin for protobuf gradle plugin documentation.

This setup works for me with Intellij IDEA 2021.1.

Short answer: Apply the idea gradle plugin to moduleA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM