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

Register unitTest generated sources with android studio #234

Merged
merged 5 commits into from
Jun 25, 2018

Conversation

zpencer
Copy link
Contributor

@zpencer zpencer commented Jun 3, 2018

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

fixes #229

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
@zpencer
Copy link
Contributor Author

zpencer commented Jun 3, 2018

Before:
before

After:
after

The unfortunate warnings:
warnings

@zpencer
Copy link
Contributor Author

zpencer commented Jun 4, 2018

Just realized that variantData is returning an internal class. Let's see if there's an alternative way to achieve this.

@zhangkun83
Copy link
Collaborator

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

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.

Copy link
Contributor Author

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

@zpencer zpencer force-pushed the register-generated-unit-test-code branch from 9d4c8b5 to 31ab6fe Compare June 4, 2018 19:32
@zpencer
Copy link
Contributor Author

zpencer commented Jun 4, 2018

@rschiu I wanted to do something like this, but variantData is internal API:

// 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)
          }
      }
  }
}

@rschiu
Copy link
Contributor

rschiu commented Jun 4, 2018

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.

@zpencer zpencer changed the title Register generated sources with android studio Register unitTest generated sources with android studio Jun 8, 2018
@zpencer
Copy link
Contributor Author

zpencer commented Jun 8, 2018

@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 @Incubating.

Copy link
Collaborator

@zhangkun83 zhangkun83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zpencer zpencer merged commit f3ee6da into google:master Jun 25, 2018
@zpencer zpencer deleted the register-generated-unit-test-code branch June 25, 2018 16:46
zhangkun83 pushed a commit to zhangkun83/protobuf-gradle-plugin-1 that referenced this pull request Nov 7, 2018
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.
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

Successfully merging this pull request may close these issues.

[Android] Generated unit test source files not being picked up by Android Studio
3 participants