-
Notifications
You must be signed in to change notification settings - Fork 274
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
Register unitTest generated sources with android studio #234
Register unitTest generated sources with android studio #234
Conversation
It turns out, android studio does not use the `idea` plugin and instead does its own thing. The android project's non local unit test protos are automatically registered when we call `registerJavaGeneratingTask`. But local unit tests run without the android SDK, and `registerJavaGeneratingTask` is not implemented. They must be registered explicitly. Also, an Android sub project can depend on another subproject that has GenerateProtoTasks. We must scan through each variant and find such subproject dependencies, and register their gen outputs. Android Studio automatically ignores linter warnings for `registerJavaGeneratingTask` sources, but for these sources it will warn. Unfortunately the current version of proto lite generates code that fails lint, and this causes warnings. But I think the typical user would rather see warnings than have sources not be registered at all, so turning this on by default makes sense. To disable this behavior, set the project property: protobuf.androidstudio.extrasrcs.experimental=false
Just realized that variantData is returning an internal class. Let's see if there's an alternative way to achieve this. |
/cc @rschiu from Android SDK team for any comments / suggestions. |
protoSrcDirSet.srcDirs.each { File protoDir -> | ||
Utils.addToIdeSources(project, Utils.isTest(sourceSet.name), protoDir) | ||
// The generated javalite sources have lint issues: | ||
// https://github.com/google/protobuf/pull/2823 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't these issues already fixed?
Besides, lint warnings don't seem to be a strong enough case to justify a new flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest javalite artifact is still from 2016, so there's no release with the fix yet:
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22protoc-gen-javalite%22
9d4c8b5
to
31ab6fe
Compare
@rschiu I wanted to do something like this, but // An android project can depend on a different subproject and use its protos.
// This is not a very common project structure but it is possible.
(getNonTestVariants() + project.android.testVariants
+ project.android.unitTestVariants).each { variant ->
variant.variantData.variantDependency.compileConfiguration.allDependencies.findAll {
it instanceof ProjectDependency
} each {
ProjectDependency it ->
it.dependencyProject.tasks.withType(GenerateProtoTask).each {
GenerateProtoTask generateProtoTask ->
generateProtoTask.getOutputSourceDirectorySet().srcDirs.each { File outputDir ->
variant.addJavaSourceFoldersToModel(outputDir)
}
}
}
} |
AFAIK, there is no external API to do that. There is a project to create better API for plugin writers, but I am not on the team anymore and I do not know the current status of that project. I have forwarded this thread to the team internally for their comment. |
@zhangkun83 let's just shoot for this partial fix for now. I think I have found an API to figure this out across sub projects but that API is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR makes non emulator unit test proto outputs be added to the IDE. Still absent are the outputs from cross sub-project protos, but we do not have a stable API to traverse the task graph and solve this problem.
It turns out, android studio does not use the
idea
plugin andinstead does its own thing.
The android project's non local unit test protos are automatically
registered when we call
registerJavaGeneratingTask
. But local unittests run without the android SDK, and
registerJavaGeneratingTask
isnot implemented. They must be registered explicitly.
Also, an Android sub project can depend on another subproject that has
GenerateProtoTasks. We must scan through each variant and find such
subproject dependencies, and register their gen outputs.
Android Studio automatically ignores linter warnings for
registerJavaGeneratingTask
sources, but for these sources it willwarn. Unfortunately the current version of proto lite generates code
that fails lint, and this causes warnings. But I think the typical
user would rather see warnings than have sources not be registered at
all, so turning this on by default makes sense.
To disable this behavior, set the project property:
protobuf.androidstudio.extrasrcs.experimental=false
fixes #229