google / protobuf-gradle-plugin

Protobuf Plugin for Gradle
Other
1.77k stars 273 forks source link

Generated java sources are not automatically visible to Android Studio #658

Open bubenheimer opened 1 year ago

bubenheimer commented 1 year ago

When generating java sources from protobuf via Gradle, the (re-)generated sources do not automatically become a part of Android Studio's source model. A file system refresh ("Reload from disk"/"Synchronize external changes") or project sync is necessary before references to these generated sources stop showing errors in the IDE editor.

It may seem a simple step to manually refresh in a simple project, but it can quickly turn into a non-obvious issue among many others in a not so simple project. In my experience of many years Android Studio does not tend to behave in a particularly robust manner when it comes to non-Android code, generated in various semi-supported and non-trivial scenarios, e.g. non-Android projects or modules, Kotlin multi-platform projects, etc.

To clarify where I'm coming from, I was essentially unable to use IDE editing support entirely for a long time for some code depending on protobuf in a slightly more complex Android Studio library project. This was just one of various IDE-related issues in the project, some of which were caused by unsupported scenarios. Recently "something" changed to the point where I am able again to leverage IDE benefits for editing the protobuf-related Kotlin sources (fingers crossed). Except I did not notice initially, as I did not manually reload/sync after generating java from protobuf. So while I held some hope that "something" would resolve the editing issues, I initially wrote it off because this secondary (decidedly more trivial) issue of manual syncing obscured my vision.

TL;DR addressing this issue in some way would be helpful for eliminating a headache and see other problems more clearly. Maybe something else can be done via the idea plugin that I could add myself? The README implies that the protobuf plugin already applies proper configuration for idea; I did not have any better luck with additional idea configuration.

I tested this on the helloworld grpc-java Android example project with various recent versions Android Studio, Gradle, and protobuf plugin. On Intel Mac. The problem can be reproduced out of the box with the most recent helloworld sources from grpc-java 1.51.1.

ejona86 commented 1 year ago

There's been recent developments along these lines, so it would be helpful to know which precise version of the plugin you were using. Things got much better with 0.9.0, but you still needed to have the eclipse or idea plugins present. (It sounds like you were on 0.9.x, but let's confirm.)

After a lot of fighting, we have determined that simply creating the output directories early is all the IDEs need. So that may lead to something closer to #619 which could unconditionally create the directories on first configuration. But if you are already having trouble with the IDE plugin applied, then that likely wouldn't change the behavior you see.

bubenheimer commented 1 year ago

Thanks for the info, I tested this with the source's original 0.8.18, 0.9.1, and 0.9.2, but I did not see a difference in terms of this specific issue. Some 0.9.x may have fixed the more serious other problem I had, though.

I am not positive that this issue is specific to protobuf code generation as opposed to other code generator plugins. It is a bigger problem for me in this case because I tend to run into more problems in the protobuf case, so my general vision is more clouded and I can't readily see what's going on.

bubenheimer commented 1 year ago

It's a problem with Android Studio, I see the same behavior with Dagger. No problem in IntelliJ (at least in the case of Dagger): https://issuetracker.google.com/issues/265726264

ejona86 commented 1 year ago

Oh, nice find. I wondered if Android Studio was just on an older version of IntelliJ; seems not.

davidliu commented 1 year ago

For what it's worth, manually adding the generated files directory to the source sets seems to resolve this for me:


    sourceSets {
        main {
            proto {
                /*...*/
            }
            java {
                srcDir "${protobuf.generatedFilesBaseDir}/main/javalite"
            }
        }
    }
khomin commented 8 months ago

Be aware about the maximum file size limit in android studio By default the value is

idea.max.intellisense.filesize=2500

Syntax analyzer showed Unresolved reference because of that in my case

Would be nice if there was such note in the doc Preferably in large font

mvdiener commented 2 months ago

Be aware about the maximum file size limit in android studio By default the value is

idea.max.intellisense.filesize=2500

Syntax analyzer showed Unresolved reference because of that in my case

Would be nice if there was such note in the doc Preferably in large font

@khomin I just want to thank you from the bottom of my heart for this answer. I have scoured the internet for days trying to find out why my generated Kotlin proto files all have unresolved references. My java source file is ~7mb which is beyond the filesize limit, and as soon as I upped the limit everything immediately worked. Thank you for saving my sanity.