xref: /aosp_15_r20/external/angle/build/android/docs/resources_in_java.md (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# How to Reference android_resources in Java (R.java)
2
3## Overview
4
5This document has basic instructions on how to reference an android resource in
6java. It also attempts to explain what R.java is.
7
8## How to reference Android Resources in java
9
101. Find the `resources_package` in your `android_library` target in BUILD.gn
11
12```gn
13android_library("java") {
14  sources = [ "java/org/chromium/browser_ui/Widget.java" ]
15  resources_package = "org.chromium.browser_ui"
16  deps = [
17    "//base:java",
18    "//base:resources",
19    "//third_party/android_deps:com_google_material_material_java",
20    "//third_party/androidx:androidx_core_core_java",
21  ]
22}
23```
24
252. Add a dep on the `android_resources` target with your resource.
26    - or `android_aar_prebuilt` or `android_java_prebuilt` for prebuilt targets
27    such as those under `//third_party/android_deps`
28
293. In your java file, `import <resources_package>.R;`
30
314. You can now reference `R.<type>.<name>` in your java.
32
33### What not to do?
34
35 - **You should not** reference multiple `R` classes.
36    - All R classes will be the same in the final APK, you should never need to
37    import multiple different R classes.
38 - **You should not** import an `R` class from a different package than the one
39 listed as the `resources_package` in you `android_library` target in `BUILD.gn`
40 - **You should not** rely on transitive deps for resources you use directly.
41
42## Android Resources and R.java
43
44Android resources are global for the whole app. While you import the R class
45from a package (eg: `org.chromium.base.R` or `org.chromium.browser_ui.R`) so you
46might assume that there are resources under the `org.chromium.base` package that
47are different from `org.chromium.browser_ui`, the actual resources from
48android's point of view are not divided into packages but form a global pool
49with unique consecutive ids.
50
51The R class is a generated class that contains a mapping from the name of the
52resources `R.string.hello` to its numeric id `0x7f015d14`. The build system
53generates this R class for you based on the resources in your dependencies.
54
55In the final apk, all the `R` classes from all the packages will all inherit
56from one global R class that has the list of all resources with the final ids as
57generated by `aapt2` (See [Life of a Resource](life_of_a_resources.md) for more
58details on how resources are processed by our build system).
59
60## Resources and per library java compilation
61
62Chrome does not compile all the java in an apk at once. But instead it builds
63each `android_library` separately and then brings them all together in the end.
64This means that when compiling a single `android_library`, the build system does
65not know the full list of resources that will end up in the apk. Thus, it can't
66actually generate the final `R.class` with real resource ids but instead
67creates a temporary one that only exists when compiling these library
68subtargets.
69
70`resources_package` in your android_library target tells the build system which
71package to generate the R class in. During compile, this temporary `R.class`
72will contain only the resources you depend on in your build target.
73