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

Support packaging and extracting protos in Android aar #440

Merged

Conversation

voidzcy
Copy link
Contributor

@voidzcy voidzcy commented Oct 19, 2020

Adds support for packaging proto files into the aar for Android library projects and extracting proto files from aar dependencies.


The CI failure should be fixed by #441.

Fixes #435

@voidzcy
Copy link
Contributor Author

voidzcy commented Oct 19, 2020

I am highly suspecting this chunk of code is redundant and can be deleted:

// TL; DR: Make protos in 'test' sourceSet able to import protos from the 'main'
// sourceSet. Sub-configurations, e.g., 'testCompile' that extends 'compile', don't
// depend on the their super configurations. As a result, 'testCompile' doesn't depend on
// 'compile' and it cannot get the proto files from 'main' sourceSet through the
// configuration. However,
if (Utils.isAndroidProject(project)) {
// TODO(zhangkun83): Android sourceSet doesn't have compileClasspath. If it did, we
// haven't figured out a way to put source protos in 'resources'. For now we use an
// ad-hoc solution that manually includes the source protos of 'main' and its
// dependencies.
if (Utils.isTest(sourceSetOrVariantName)) {
inputFiles.from getSourceSets()['main'].proto
inputFiles.from testedCompileClasspathConfiguration ?: project.configurations['compile']
}
} else {
// In Java projects, the compileClasspath of the 'test' sourceSet includes all the
// 'resources' of the output of 'main', in which the source protos are placed. This is
// nicer than the ad-hoc solution that Android has, because it works for any extended
// configuration, not just 'testCompile'.
inputFiles.from getSourceSets()[sourceSetOrVariantName].compileClasspath
}

Copy link
Collaborator

@ejona86 ejona86 left a comment

Choose a reason for hiding this comment

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

How hard would it be to have a test?

@voidzcy voidzcy closed this Oct 29, 2020
@voidzcy voidzcy reopened this Oct 29, 2020
@voidzcy
Copy link
Contributor Author

voidzcy commented Oct 29, 2020

How hard would it be to have a test?

Yep, that's something still under consideration.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…o bugfix/package_and_extract_proto_in_aar
@voidzcy voidzcy merged commit 5291031 into google:master Oct 30, 2020
@voidzcy
Copy link
Contributor Author

voidzcy commented Nov 2, 2020

I found a solution for tests to cover extracting protos from Jar/Aar built with project dependencies:

We can change

implementation project(':testProjectJavaLibrary')

to

implementation(project(':testProjectJavaLibrary')) {
        attributes {
            attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
                    project.getObjects().named(LibraryElements, LibraryElements.JAR))
        }
 }

which mutates the attributes of the project dependency and changes its libraryelements attribute for variant selection (some docs here and here). Therefore, we can force it to pull in the Jar instead of just classes (java-library's default) and resources (what our internal compileProtoPath enforces).

By turning on the log, you can see the difference for testProjectDependentApp:extractIncludeProto task:

Extracting protos from *********************/protobuf-gradle-plugin/build/tests/testProjectDependentAppMain/testProjectJavaLibrary/build/resources/main to *********************/protobuf-gradle-plugin/build/tests/testProjectDependentAppMain/testProjectDependentApp/build/extracted-include-protos/main
Extracting protos from *********************/protobuf-gradle-plugin/build/tests/testProjectDependentAppMain/testProjectJavaLibrary/build/classes/java/main to *********************/protobuf-gradle-plugin/build/tests/testProjectDependentAppMain/testProjectDependentApp/build/extracted-include-protos/main

vs

Extracting protos from ***************/protobuf-gradle-plugin/build/tests/testProjectDependentAppMain/testProjectJavaLibrary/build/libs/testProjectJavaLibrary.jar to *********************/protobuf-gradle-plugin/build/tests/testProjectDependentAppMain/testProjectDependentApp/build/extracted-include-protos/main

Ideally we can keep both test cases (if not too verbose) as one covers the plugin works for extracting protos from project dependencies built with java-library or android-library and the other covers the case protos coming from actual Jar/Aar.

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.

protobuf packaging doesnt retain directory structure in Android aar libraries
2 participants