Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bubenheimer opened this issue Jan 10, 2023 · 6 comments
Open

Comments

@bubenheimer
Copy link

bubenheimer commented Jan 10, 2023

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
Copy link
Collaborator

ejona86 commented Jan 10, 2023

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
Copy link
Author

bubenheimer commented Jan 10, 2023

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
Copy link
Author

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
Copy link
Collaborator

ejona86 commented Jan 17, 2023

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

@davidliu
Copy link

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
Copy link

khomin commented Mar 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants