android_library packs all resources instead of specific resource_files

Open
#345 0 comments 2 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
android

Research direction

Start with _make_resources_flag in rules/busybox.bzl lines 110-125, then inspect the resource handling around lines 681-689 and how android_library passes resource_files to the aapt build action. Verify the behavior using the feature-specific targets described in the issue; done means each target packages only its listed resource files rather than every resource in the shared directories.

Written by the indexing model from the issue text.

Description

Observed behavior

We have one Bazel module for Icons used in different apps and we declare feature specific targets like so:

modules/iconography/BUILD:

android_library(
  name = "feature_x_icons",
  resource_files = [ "src/main/res/drawable/x_icon_1.xml", "src/main/res/drawable/x_icon_2.xml"]
)

android_library(
  name = "feature_y_icons",
  resource_files = [ "src/main/res/drawable/y_icon_a.xml", "src/main/res/drawable/y_icon_b.xml"]
)

What we've found is that different apps ended up packing all resources from modules/iconography/src/main/res/drawable despite only depending on very specific targets with specific resource_files referenced in them.

Environment

  • Bazel 9.0.0-pre.20250115.2
  • rules_android 0.6.2

Findings after Debugging

It turns out rules_android computes a set of common unique directories among all the resource_files and then passes those unique directories to aapt build action effectively packaging all resources from those dirs rather than specific resource_files user passed in the BUILD file.

_make_resources_flag:

https://github.com/bazelbuild/rules_android/blob/aed4306a9117bd0812ec80d4bc2cedadb1db6f94/rules/busybox.bzl#L110-L125

And

https://github.com/bazelbuild/rules_android/blob/aed4306a9117bd0812ec80d4bc2cedadb1db6f94/rules/busybox.bzl#L681-L689

In our case this resulted in 6MB-8MB excessive resources packaged into different apps that never needed nor referenced those resources.


Expected behavior

android_library() target should only package specific resource_files passed by user.

Dominant language
Java
Stars
205
Forks
113
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_android

All issues in bazelbuild/rules_android

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.