Skip to content

Resource linking for android_library targets is unnecessary.#11294

Closed
jongerrish wants to merge 1 commit intobazelbuild:masterfrom
jongerrish:no_link_library_resources
Closed

Resource linking for android_library targets is unnecessary.#11294
jongerrish wants to merge 1 commit intobazelbuild:masterfrom
jongerrish:no_link_library_resources

Conversation

@jongerrish
Copy link
Copy Markdown
Contributor

Use R.txt from AndroidCompiledResourceMerger
Don't include R.java in srcjar output
Avoid AndroidResourceLink action
Avoid large library.ap_ + resource.srcjar outputs
Enable with the following flags:-

--nolink_library_resources --experimental_use_rtxt_from_merged_resources

Use R.txt from AndroidCompiledResourceMerger
Don't include R.java in srcjar output
Avoid AndroidResourceLink action
Avoid large library.ap_ + resource.srcjar outputs
Enable with the following flags:-

--nolink_library_resources --experimental_use_rtxt_from_merged_resources
@jongerrish
Copy link
Copy Markdown
Contributor Author

@jin @cgruber Thoughts?

@jongerrish
Copy link
Copy Markdown
Contributor Author

Goes some way to addressing bazelbuild/rules_android#10

@jin
Copy link
Copy Markdown
Member

jin commented May 7, 2020

cc @donaldchai @timpeut @djwhang

@aiuto aiuto added the team-Android Issues for Android team label May 13, 2020
@donaldchai
Copy link
Copy Markdown
Contributor

Thanks for reminding me about --experimental_use_rtxt_from_merged_resources. @jin: Do we need to go through the "incompatible change" process to unconditionally switch to that? (I don't expect any downsides to this.)

Jon: overall I like the idea of pruning things from the build, but I think this disables a useful validation step. In particular the "aapt2 link" verifies that resources referenced from XML are defined. So trying to build java/com/foo:foo should fail:

-- java/com/foo/BUILD --
android_library(
  name = "foo",
  manifest = ...
  resource_files = glob(["res/**"]),
  deps = ["//java/com/bar"],
)

-- java/com/foo/res/values/values.xml --
<resources>
  <string name="asdf">@string/missing</string>
</resources>

-- java/com/bar/BUILD --
android_library(
  name = "bar",
  srcs = "Bar.java",
  # no resources
)

Is that still true after this change? I was planning on writing a new validator (potentially emitting a warning if a referenced resource is not in a direct dep) but haven't gotten to it.

@jin
Copy link
Copy Markdown
Member

jin commented May 14, 2020

Thanks for reminding me about --experimental_use_rtxt_from_merged_resources. @jin: Do we need to go through the "incompatible change" process to unconditionally switch to that? (I don't expect any downsides to this.)

No, I don't think it's required for this.

@ulfjack
Copy link
Copy Markdown
Contributor

ulfjack commented Jun 3, 2020

@donaldchai, since this is behind a flag which defaults to building linking the resources, that seems fine?

@donaldchai
Copy link
Copy Markdown
Contributor

@donaldchai, since this is behind a flag which defaults to building linking the resources, that seems fine?

Sure, but this decision is ultimately about how code is merged and how flags interact. I have no authority here. All of my own changes require approval by someone from the Bazel team, or others CCed to this PR.

@ulfjack
Copy link
Copy Markdown
Contributor

ulfjack commented Jun 19, 2020

@jin can you merge this?

@meisterT
Copy link
Copy Markdown
Member

@jin any objections?

@jongerrish
Copy link
Copy Markdown
Contributor Author

@donaldchai - with this flag enabled you won't get any validation from linking as its disabled.

Probably this validation should happen in AndroidCompiledResourceMergingAction actually, it already supports a flag --throwOnResourceConflict (it looks like this defaults to true unless the rules is whitelisted which I don't think happens in open source Bazel (i.e: resource conflicts are always allowed) but it seems that this is the action that should validate the resources (does a referenced resource exist? is a resource duplicated? etc) as it does the merge (generates merged R class).

@jongerrish
Copy link
Copy Markdown
Contributor Author

Hi @jin - the PR has been outstanding for a while now, is there any concern with it? I'm happy to discuss and address any issue you may have.


if (AndroidResources.definesAndroidResources(attributes)) {
implicitOutputs.add(
AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Removing this will break any current usage of the .srcjar output as a label, whether the flag is on or off, so it looks like we'd need to go through the incompatible change process. Unfortunately there's no way to conditionally register implicit outputs based on configuration. Glancing through the code, it should be technically possible to enable that, but we'd need to think through that separately. The next best thing is to leave this here, and when --link_library_resources is false, register a FailAction for that output. This means that the failure won't be surfaced until execution time, unfortunately.

@donaldchai
Copy link
Copy Markdown
Contributor

Probably this validation should happen in AndroidCompiledResourceMergingAction actually, it already supports a flag --throwOnResourceConflict

I feel the opposite way, i.e. AndroidCompiledResourceMergingAction shouldn't be doing any of these checks since it's on the critical path of the build, and the checks are not needed to generate R classes.

As as a side note, detection and reporting of "resource conflicts" on android_library should be overhauled so that they can be made errors instead of warnings (i.e. build log spam). The current override semantics leave a lot to be desired.

@aiuto
Copy link
Copy Markdown

aiuto commented Jan 6, 2021

Time for a decision. Are we going to merge it or not?

@jin
Copy link
Copy Markdown
Member

jin commented Jan 15, 2021

@ahumesky could you make a call here about merging the PRs modifying Android native rules?

@nkoroste
Copy link
Copy Markdown
Contributor

Any updates on this?

@aiuto
Copy link
Copy Markdown

aiuto commented Oct 7, 2021

Friendly ping to all.

@meisterT
Copy link
Copy Markdown
Member

Given the Starlarkification of the rules, this PR probably should be reopened against https://github.com/bazelbuild/rules_android at one point.

I am going to close the PR, feel free to re-open if you disagree.

@meisterT meisterT closed this Mar 20, 2025
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes team-Android Issues for Android team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants