Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 1 | # App Bundles and Dynamic Feature Modules (DFMs) |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 2 | |
| 3 | [TOC] |
| 4 | |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 5 | ## About Bundles |
| 6 | [Android App bundles] is a Play Store feature that allows packaging an app as |
| 7 | multiple `.apk` files, known as "splits". Bundles are zip files with an `.aab` |
| 8 | extension. See [android_build_instructions.md#multiple-chrome-targets] for a |
| 9 | list of buildable bundle targets. |
| 10 | |
| 11 | Bundles provide three main advantages over monolithic `.apk` files: |
| 12 | 1. Language resources are split into language-specific `.apk` files, known as |
| 13 | "resource splits". Delivering only the active languages reduces the overhead |
| 14 | of UI strings. |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 15 | * Resource splits can also be made on a per-screen-density basis (for drawables), |
| 16 | but Chrome has not taken advantage of this (yet). |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 17 | 2. Features can be packaged into lazily loaded `.apk` files, known as |
Andrew Grieve | 520e090 | 2022-04-27 13:19:25 | [diff] [blame] | 18 | "feature splits". Chrome enables [isolated splits], which means feature |
| 19 | splits have no performance overhead until used (on Android O+ at least). |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 20 | 3. Feature splits can be downloaded on-demand, saving disk space for users that |
| 21 | do not need the functionality they provide. These are known as |
| 22 | "Dynamic feature modules", or "DFMs". |
Andrew Grieve | bf63c45 | 2024-09-04 16:47:49 | [diff] [blame] | 23 | * **The install experience for DFMs is quite poor (5-30 seconds install times, |
| 24 | sometimes fails, sometimes [triggers a crash]).** |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 25 | |
| 26 | You can inspect which `.apk` files are produced by a bundle target via: |
| 27 | ``` |
| 28 | out/Default/bin/${target_name} build-bundle-apks --output-apks foo.apks |
| 29 | unzip -l foo.apks |
| 30 | ``` |
| 31 | |
Andrew Grieve | 23c828d | 2021-05-13 19:37:29 | [diff] [blame] | 32 | *** note |
Andrew Grieve | 5d8fc66 | 2024-08-16 18:10:40 | [diff] [blame] | 33 | Adding new features via feature splits is highly encouraged when it makes sense |
Andrew Grieve | 23c828d | 2021-05-13 19:37:29 | [diff] [blame] | 34 | to do so: |
Andrew Grieve | 5d8fc66 | 2024-08-16 18:10:40 | [diff] [blame] | 35 | * Has a non-trivial amount of Java code (after optimization). E.g. >150kb |
Andrew Grieve | 23c828d | 2021-05-13 19:37:29 | [diff] [blame] | 36 | * Not needed on startup |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 37 | * Has a small integration surface (calls into it must be done with reflection) |
Andrew Grieve | 5d8fc66 | 2024-08-16 18:10:40 | [diff] [blame] | 38 | * Not used by WebView |
Andrew Grieve | 23c828d | 2021-05-13 19:37:29 | [diff] [blame] | 39 | *** |
| 40 | |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 41 | [android_build_instructions.md#multiple-chrome-targets]: android_build_instructions.md#multiple-chrome-targets |
| 42 | [Android App Bundles]: https://developer.android.com/guide/app-bundle |
Andrew Grieve | 520e090 | 2022-04-27 13:19:25 | [diff] [blame] | 43 | [isolated splits]: android_isolated_splits.md |
Andrew Grieve | bf63c45 | 2024-09-04 16:47:49 | [diff] [blame] | 44 | [triggers a crash]: https://chromium.googlesource.com/chromium/src/+/main/docs/android_isolated_splits.md#Conflicting-ClassLoaders-2 |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 45 | |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 46 | ### Declaring App Bundles with GN Templates |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 47 | |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 48 | Here's an example that shows how to declare a simple bundle that contains a |
| 49 | single base module, which enables language-based splits: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 50 | |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 51 | ```gn |
| 52 | android_app_bundle_module("foo_base_module") { |
| 53 | # Declaration are similar to android_apk here. |
| 54 | ... |
| 55 | } |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 56 | |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 57 | android_app_bundle("foo_bundle") { |
| 58 | base_module_target = ":foo_base_module" |
| 59 | |
| 60 | # The name of our bundle file (without any suffix). |
| 61 | bundle_name = "FooBundle" |
| 62 | |
| 63 | # Enable language-based splits for this bundle. Which means that |
| 64 | # resources and assets specific to a given language will be placed |
| 65 | # into their own split APK in the final .apks archive. |
| 66 | enable_language_splits = true |
| 67 | |
| 68 | # Proguard settings must be passed at the bundle, not module, target. |
| 69 | proguard_enabled = !is_java_debug |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | When generating the `foo_bundle` target with Ninja, you will end up with |
| 74 | the following: |
| 75 | |
| 76 | * The bundle file under `out/Release/apks/FooBundle.aab` |
| 77 | |
| 78 | * A helper script called `out/Release/bin/foo_bundle`, which can be used |
| 79 | to install / launch / uninstall the bundle on local devices. |
| 80 | |
| 81 | This works like an APK wrapper script (e.g. `foo_apk`). Use `--help` |
| 82 | to see all possible commands supported by the script. |
| 83 | |
| 84 | |
| 85 | The remainder of this doc focuses on DFMs. |
| 86 | |
| 87 | ## Declaring Dynamic Feature Modules (DFMs) |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 88 | |
| 89 | This guide walks you through the steps to create a DFM called _Foo_ and add it |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 90 | to the Chrome bundles. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 91 | |
| 92 | *** note |
| 93 | **Note:** To make your own module you'll essentially have to replace every |
| 94 | instance of `foo`/`Foo`/`FOO` with `your_feature_name`/`YourFeatureName`/ |
| 95 | `YOUR_FEATURE_NAME`. |
| 96 | *** |
| 97 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 98 | ### Reference DFM |
| 99 | |
| 100 | In addition to this guide, the |
| 101 | [Test Dummy](https://cs.chromium.org/chromium/src/chrome/android/modules/test_dummy/test_dummy_module.gni) |
| 102 | module serves as an actively-maintained reference DFM. Test Dummy is used in |
| 103 | automated bundle testing, and covers both Java and native code and resource |
| 104 | usage. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 105 | |
| 106 | ### Create DFM target |
| 107 | |
| 108 | DFMs are APKs. They have a manifest and can contain Java and native code as well |
| 109 | as resources. This section walks you through creating the module target in our |
| 110 | build system. |
| 111 | |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 112 | First, create the file |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 113 | `//chrome/android/modules/foo/internal/java/AndroidManifest.xml` and add: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 114 | |
| 115 | ```xml |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 116 | <?xml version="1.0" encoding="utf-8"?> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 117 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 118 | xmlns:dist="http://schemas.android.com/apk/distribution" |
Tibor Goldschwendt | 5172118f | 2019-06-24 21:57:47 | [diff] [blame] | 119 | featureSplit="foo"> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 120 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 121 | <!-- dist:onDemand="true" makes this a separately installed module. |
| 122 | dist:onDemand="false" would always install the module alongside the |
| 123 | rest of Chrome. --> |
| 124 | <dist:module |
| 125 | dist:onDemand="true" |
| 126 | dist:title="@string/foo_module_title"> |
Ben Mason | e571ea5a | 2019-09-06 18:29:37 | [diff] [blame] | 127 | <!-- This will fuse the module into the base APK if a system image |
| 128 | APK is built from this bundle. --> |
| 129 | <dist:fusing dist:include="true" /> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 130 | </dist:module> |
| 131 | |
Samuel Huang | 39c7db63 | 2019-05-15 14:57:18 | [diff] [blame] | 132 | <!-- Remove android:hasCode="false" when adding Java code. --> |
| 133 | <application android:hasCode="false" /> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 134 | </manifest> |
| 135 | ``` |
| 136 | |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 137 | Next, create a descriptor configuring the Foo module. To do this, create |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 138 | `//chrome/android/modules/foo/foo_module.gni` and add the following: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 139 | |
| 140 | ```gn |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 141 | foo_module_desc = { |
| 142 | name = "foo" |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 143 | android_manifest = |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 144 | "//chrome/android/modules/foo/internal/java/AndroidManifest.xml" |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 145 | } |
| 146 | ``` |
| 147 | |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 148 | Then, add the module descriptor to the appropriate descriptor list in |
Andrew Grieve | 912f6633 | 2023-04-11 16:11:56 | [diff] [blame] | 149 | //chrome/android/modules/chrome_feature_modules.gni, e.g. the Chrome list: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 150 | |
| 151 | ```gn |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 152 | import("//chrome/android/modules/foo/foo_module.gni") |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 153 | ... |
Andrew Grieve | 912f6633 | 2023-04-11 16:11:56 | [diff] [blame] | 154 | chrome_module_descs += [ foo_module_desc ] |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 155 | ``` |
| 156 | |
| 157 | The next step is to add Foo to the list of feature modules for UMA recording. |
| 158 | For this, add `foo` to the `AndroidFeatureModuleName` in |
James Lee | ec45f5d | 2024-06-21 07:13:38 | [diff] [blame] | 159 | `//tools/metrics/histograms/metadata/histogram_suffixes_list.xml`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 160 | |
| 161 | ```xml |
| 162 | <histogram_suffixes name="AndroidFeatureModuleName" ...> |
| 163 | ... |
| 164 | <suffix name="foo" label="Super Duper Foo Module" /> |
| 165 | ... |
| 166 | </histogram_suffixes> |
| 167 | ``` |
| 168 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 169 | Lastly, give your module a title that Chrome and Play can use for the install |
| 170 | UI. To do this, add a string to |
Adam Langley | 891ea2b | 2020-04-10 17:11:18 | [diff] [blame] | 171 | `//chrome/browser/ui/android/strings/android_chrome_strings.grd`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 172 | |
| 173 | ```xml |
| 174 | ... |
| 175 | <message name="IDS_FOO_MODULE_TITLE" |
| 176 | desc="Text shown when the Foo module is referenced in install start, success, |
| 177 | failure UI (e.g. in IDS_MODULE_INSTALL_START_TEXT, which will expand to |
| 178 | 'Installing Foo for Chrome…')."> |
| 179 | Foo |
| 180 | </message> |
| 181 | ... |
| 182 | ``` |
| 183 | |
Samuel Huang | 7f2b5375 | 2019-05-23 15:10:05 | [diff] [blame] | 184 | *** note |
| 185 | **Note:** This is for module title only. Other strings specific to the module |
| 186 | should go in the module, not here (in the base module). |
| 187 | *** |
| 188 | |
Andrew Grieve | 912f6633 | 2023-04-11 16:11:56 | [diff] [blame] | 189 | Congrats! You added the DFM Foo to Chrome. That is a big step but not very |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 190 | useful so far. In the next sections you'll learn how to add code and resources |
| 191 | to it. |
| 192 | |
| 193 | |
| 194 | ### Building and installing modules |
| 195 | |
| 196 | Before we are going to jump into adding content to Foo, let's take a look on how |
| 197 | to build and deploy the Monochrome bundle with the Foo DFM. The remainder of |
| 198 | this guide assumes the environment variable `OUTDIR` is set to a properly |
| 199 | configured GN build directory (e.g. `out/Debug`). |
| 200 | |
| 201 | To build and install the Monochrome bundle to your connected device, run: |
| 202 | |
| 203 | ```shell |
| 204 | $ autoninja -C $OUTDIR monochrome_public_bundle |
Andrew Grieve | f0d97776 | 2021-08-18 20:20:43 | [diff] [blame] | 205 | $ $OUTDIR/bin/monochrome_public_bundle install -m foo |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 206 | ``` |
| 207 | |
Andrew Grieve | f0d97776 | 2021-08-18 20:20:43 | [diff] [blame] | 208 | This will install the `Foo` module, the `base` module, and all modules with an |
| 209 | `AndroidManifest.xml` that: |
| 210 | * Sets `<module dist:onDemand="false">`, or |
| 211 | * Has `<dist:delivery>` conditions that are satisfied by the device being |
| 212 | installed to. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 213 | |
| 214 | *** note |
Tibor Goldschwendt | f430b27 | 2019-11-25 19:19:41 | [diff] [blame] | 215 | **Note:** The install script may install more modules than you specify, e.g. |
| 216 | when there are default or conditionally installed modules (see |
| 217 | [below](#conditional-install) for details). |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 218 | *** |
| 219 | |
| 220 | You can then check that the install worked with: |
| 221 | |
| 222 | ```shell |
| 223 | $ adb shell dumpsys package org.chromium.chrome | grep splits |
| 224 | > splits=[base, config.en, foo] |
| 225 | ``` |
| 226 | |
| 227 | Then try installing the Monochrome bundle without your module and print the |
| 228 | installed modules: |
| 229 | |
| 230 | ```shell |
Andrew Grieve | f0d97776 | 2021-08-18 20:20:43 | [diff] [blame] | 231 | $ $OUTDIR/bin/monochrome_public_bundle install |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 232 | $ adb shell dumpsys package org.chromium.chrome | grep splits |
| 233 | > splits=[base, config.en] |
| 234 | ``` |
| 235 | |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 236 | *** note |
| 237 | The wrapper script's `install` command does approximately: |
| 238 | ```sh |
Joanna Wang | 81bea75 | 2024-08-15 16:26:56 | [diff] [blame] | 239 | java -jar third_party/android_build_tools/bundletool/cipd/bundletool.jar build-apks --output tmp.apks ... |
| 240 | java -jar third_party/android_build_tools/bundletool/cipd/bundletool.jar install-apks --apks tmp.apks |
Andrew Grieve | 7e777abf | 2021-08-17 19:43:59 | [diff] [blame] | 241 | ``` |
| 242 | |
| 243 | The `install-apks` command uses `adb install-multiple` under-the-hood. |
| 244 | *** |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 245 | |
Samuel Huang | 3dc9fce8 | 2020-02-26 18:09:57 | [diff] [blame] | 246 | ### Adding Java code |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 247 | |
| 248 | To make Foo useful, let's add some Java code to it. This section will walk you |
| 249 | through the required steps. |
| 250 | |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 251 | First, define a module interface for Foo. This is accomplished by adding the |
| 252 | `@ModuleInterface` annotation to the Foo interface. This annotation |
| 253 | automatically creates a `FooModule` class that can be used later to install and |
| 254 | access the module. To do this, add the following in the new file |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 255 | `//chrome/browser/foo/android/java/src/org/chromium/chrome/browser/foo/Foo.java`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 256 | |
| 257 | ```java |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 258 | package org.chromium.chrome.browser.foo; |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 259 | |
Fred Mello | 2623e05 | 2019-10-02 20:18:04 | [diff] [blame] | 260 | import org.chromium.components.module_installer.builder.ModuleInterface; |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 261 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 262 | /** Interface to call into Foo feature. */ |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 263 | @ModuleInterface(module = "foo", impl = "org.chromium.chrome.browser.FooImpl") |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 264 | public interface Foo { |
| 265 | /** Magical function. */ |
| 266 | void bar(); |
| 267 | } |
| 268 | ``` |
| 269 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 270 | Next, define an implementation that goes into the module in the new file |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 271 | `//chrome/browser/foo/internal/android/java/src/org/chromium/chrome/browser/foo/FooImpl.java`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 272 | |
| 273 | ```java |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 274 | package org.chromium.chrome.browser.foo; |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 275 | |
| 276 | import org.chromium.base.Log; |
| 277 | |
| 278 | public class FooImpl implements Foo { |
| 279 | @Override |
| 280 | public void bar() { |
| 281 | Log.i("FOO", "bar in module"); |
| 282 | } |
| 283 | } |
| 284 | ``` |
| 285 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 286 | You can then use this provider to access the module if it is installed. To test |
| 287 | that, instantiate Foo and call `bar()` somewhere in Chrome: |
| 288 | |
| 289 | ```java |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 290 | if (FooModule.isInstalled()) { |
| 291 | FooModule.getImpl().bar(); |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 292 | } else { |
| 293 | Log.i("FOO", "module not installed"); |
| 294 | } |
| 295 | ``` |
| 296 | |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 297 | The interface has to be available regardless of whether the Foo DFM is present. |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 298 | Therefore, put those classes into the base module, creating a new public |
| 299 | build target in: `//chrome/browser/foo/BUILD.gn`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 300 | |
| 301 | ```gn |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 302 | import("//build/config/android/rules.gni") |
| 303 | |
| 304 | android_library("java") { |
| 305 | sources = [ |
| 306 | "android/java/src/org/chromium/chrome/browser/foo/Foo.java", |
| 307 | ] |
Mohamed Heikal | 826b4d5 | 2021-06-25 18:13:57 | [diff] [blame] | 308 | deps = [ |
| 309 | "//components/module_installer/android:module_installer_java", |
| 310 | "//components/module_installer/android:module_interface_java", |
| 311 | ] |
| 312 | annotation_processor_deps = |
| 313 | [ "//components/module_installer/android:module_interface_processor" ] |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 314 | } |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 315 | ``` |
| 316 | |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 317 | Then, depend on this target from where it is used as usual. For example, if the |
| 318 | caller is in `chrome_java in //chrome/android/BUILD.gn`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 319 | |
| 320 | ```gn |
| 321 | ... |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 322 | android_library("chrome_java") { |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 323 | deps =[ |
| 324 | ... |
| 325 | "//chrome/browser/foo:java", |
| 326 | ... |
| 327 | ] |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 328 | } |
| 329 | ... |
| 330 | ``` |
| 331 | |
| 332 | The actual implementation, however, should go into the Foo DFM. For this |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 333 | purpose, create a new file `//chrome/browser/foo/internal/BUILD.gn` and |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 334 | make a library with the module Java code in it: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 335 | |
| 336 | ```gn |
| 337 | import("//build/config/android/rules.gni") |
| 338 | |
| 339 | android_library("java") { |
| 340 | # Define like ordinary Java Android library. |
Natalie Chouinard | cbdc6dc | 2019-12-24 00:02:35 | [diff] [blame] | 341 | sources = [ |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 342 | "android/java/src/org/chromium/chrome/browser/foo/FooImpl.java", |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 343 | # Add other Java classes that should go into the Foo DFM here. |
| 344 | ] |
Fred Mello | b32b302 | 2019-06-21 18:10:11 | [diff] [blame] | 345 | deps = [ |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 346 | "//base:base_java", |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 347 | # Put other Chrome libs into the classpath so that you can call into them |
| 348 | # from the Foo DFM. |
| 349 | "//chrome/browser/bar:java", |
| 350 | # The module can depend even on `chrome_java` due to factory magic, but this |
| 351 | # is discouraged. Consider passing a delegate interface in instead. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 352 | "//chrome/android:chrome_java", |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 353 | # Also, you'll need to depend on any //third_party or //components code you |
| 354 | # are using in the module code. |
| 355 | ] |
| 356 | } |
| 357 | ``` |
| 358 | |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 359 | Then, add this new library as a dependency of the Foo module descriptor in |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 360 | `//chrome/android/modules/foo/foo_module.gni`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 361 | |
| 362 | ```gn |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 363 | foo_module_desc = { |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 364 | ... |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 365 | java_deps = [ |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 366 | "//chrome/browser/foo/internal:java", |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 367 | ] |
| 368 | } |
| 369 | ``` |
| 370 | |
| 371 | Finally, tell Android that your module is now containing code. Do that by |
Samuel Huang | 39c7db63 | 2019-05-15 14:57:18 | [diff] [blame] | 372 | removing the `android:hasCode="false"` attribute from the `<application>` tag in |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 373 | `//chrome/android/modules/foo/internal/java/AndroidManifest.xml`. You should be |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 374 | left with an empty tag like so: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 375 | |
| 376 | ```xml |
| 377 | ... |
| 378 | <application /> |
| 379 | ... |
| 380 | ``` |
| 381 | |
| 382 | Rebuild and install `monochrome_public_bundle`. Start Chrome and run through a |
| 383 | flow that tries to executes `bar()`. Depending on whether you installed your |
| 384 | module (`-m foo`) "`bar in module`" or "`module not installed`" is printed to |
| 385 | logcat. Yay! |
| 386 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 387 | ### Adding pre-built native libraries |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 388 | |
Christopher Grant | 8fea5a1 | 2019-07-31 19:12:31 | [diff] [blame] | 389 | You can add a third-party native library (or any standalone library that doesn't |
| 390 | depend on Chrome code) by adding it as a loadable module to the module descriptor in |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 391 | `//chrome/android/moduiles/foo/foo_module.gni`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 392 | |
| 393 | ```gn |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 394 | foo_module_desc = { |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 395 | ... |
Tibor Goldschwendt | aef8e39 | 2019-07-19 16:39:10 | [diff] [blame] | 396 | loadable_modules_32_bit = [ "//path/to/32/bit/lib.so" ] |
| 397 | loadable_modules_64_bit = [ "//path/to/64/bit/lib.so" ] |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 398 | } |
| 399 | ``` |
| 400 | |
Christopher Grant | 8fea5a1 | 2019-07-31 19:12:31 | [diff] [blame] | 401 | ### Adding Chrome native code |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 402 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 403 | Chrome native code may be placed in a DFM. The easiest way to access native |
| 404 | feature code is by calling it from Java via JNI. When a module is first |
| 405 | accessed, its native library (or potentially libraries, if using a component |
| 406 | build), are automatically opened by the DFM framework, and a feature-specific |
| 407 | JNI method (supplied by the feature's implementation) is invoked. Hence, a |
| 408 | module's Java code may freely use JNI to call module native code. |
Christopher Grant | 8fea5a1 | 2019-07-31 19:12:31 | [diff] [blame] | 409 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 410 | Using the module framework and JNI to access the native code eliminates concerns |
| 411 | with DFM library file names (which vary across build variants), |
| 412 | `android_dlopen_ext()` (needed to open feature libraries), and use of dlsym(). |
Christopher Grant | 8fea5a1 | 2019-07-31 19:12:31 | [diff] [blame] | 413 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 414 | This mechanism can be extended if necessary by DFM implementers to facilitate |
| 415 | subsequent native-native calls, by having a JNI-called initialization method |
| 416 | create instance of a object or factory, and register it through a call to the |
| 417 | base module's native code (DFM native code can call base module code directly). |
Christopher Grant | 8fea5a1 | 2019-07-31 19:12:31 | [diff] [blame] | 418 | |
Eric Stevenson | 8c9ab26b | 2019-08-30 15:44:40 | [diff] [blame] | 419 | #### JNI |
| 420 | |
Sam Maier | e1df6f2 | 2023-08-11 14:20:40 | [diff] [blame] | 421 | Read the `jni_generator` [docs](../third_party/jni_zero/README.md) before |
Eric Stevenson | 8c9ab26b | 2019-08-30 15:44:40 | [diff] [blame] | 422 | reading this section. |
| 423 | |
| 424 | There are some subtleties to how JNI registration works with DFMs: |
| 425 | |
| 426 | * Generated wrapper `ClassNameJni` classes are packaged into the DFM's dex file |
Sam Maier | 79e40da | 2023-01-27 17:26:11 | [diff] [blame] | 427 | * The class containing the actual native definitions, |
| 428 | `<module_name>_GEN_JNI.java`, is currently stored in the base module, but |
| 429 | could be moved out |
| 430 | * The `Natives` interface you provide will need to be annotated with your module |
| 431 | name as an argument to `NativeMethods`, eg. `@NativeMethods("foo")`, resulting |
| 432 | in a uniquely named `foo_GEN_JNI.java` |
| 433 | * The DFM will need to provide a `generate_jni_registration` target |
Eric Stevenson | 8c9ab26b | 2019-08-30 15:44:40 | [diff] [blame] | 434 | that will generate all of the native registration functions |
| 435 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 436 | #### Calling DFM native code via JNI |
| 437 | |
| 438 | A linker-assisted partitioning system automates the placement of code into |
| 439 | either the main Chrome library or feature-specific .so libraries. Feature code |
| 440 | may continue to make use of core Chrome code (eg. base::) without modification, |
| 441 | but Chrome must call feature code through a virtual interface (any "direct" |
| 442 | calls to the feature code from the main library will cause the feature code to |
| 443 | be pulled back into the main library). |
| 444 | |
| 445 | Partitioning is explained in [Android Native |
| 446 | Libraries](android_native_libraries.md#partitioned-libraries). |
| 447 | |
| 448 | First, build a module native interface. Supply a JNI method named |
| 449 | `JNI_OnLoad_foo` for the module framework to call, in |
| 450 | `//chrome/android/modules/foo/internal/entrypoints.cc`. This method is invoked |
| 451 | on all Chrome build variants, including Monochrome (unlike base module JNI). |
| 452 | |
| 453 | ```c++ |
Sam Maier | aa61518 | 2024-02-13 19:56:17 | [diff] [blame] | 454 | #include "third_party/jni_zero/jni_zero_helper.h" |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 455 | #include "base/android/jni_utils.h" |
| 456 | #include "chrome/android/modules/foo/internal/jni_registration.h" |
| 457 | |
| 458 | extern "C" { |
| 459 | // This JNI registration method is found and called by module framework code. |
Andrew Grieve | ff18140b | 2024-11-25 17:29:19 | [diff] [blame] | 460 | JNI_ZERO_BOUNDARY_EXPORT bool JNI_OnLoad_foo(JNIEnv* env) { |
Sam Maier | b0bd6d9 | 2023-04-05 13:22:23 | [diff] [blame] | 461 | if (!foo::RegisterNatives(env)) { |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | return true; |
| 465 | } |
| 466 | } // extern "C" |
| 467 | ``` |
| 468 | |
| 469 | Next, include the module entrypoint and related pieces in the build config at |
| 470 | `//chrome/android/modules/foo/internal/BUILD.gn`: |
| 471 | |
| 472 | ```gn |
| 473 | import("//build/config/android/rules.gni") |
| 474 | import("//chrome/android/modules/buildflags.gni") |
| 475 | ... |
| 476 | |
| 477 | # Put the JNI entrypoint in a component, so that the component build has a |
| 478 | # library to include in the foo module. This makes things feel consistent with |
| 479 | # a release build. |
| 480 | component("foo") { |
| 481 | sources = [ |
| 482 | "entrypoints.cc", |
| 483 | ] |
| 484 | deps = [ |
| 485 | ":jni_registration", |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 486 | "//base", |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 487 | "//chrome/browser/foo/internal:native", |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 488 | ] |
| 489 | |
| 490 | # Instruct the compiler to flag exported entrypoint function as belonging in |
| 491 | # foo's library. The linker will use this information when creating the |
| 492 | # native libraries. The partition name must be <feature>_partition. |
| 493 | if (use_native_partitions) { |
| 494 | cflags = [ "-fsymbol-partition=foo_partition" ] |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | # Generate JNI registration for the methods called by the Java side. Note the |
| 499 | # no_transitive_deps argument, which ensures that JNI is generated for only the |
| 500 | # specified Java target, and not all its transitive deps (which could include |
| 501 | # the base module). |
| 502 | generate_jni_registration("jni_registration") { |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 503 | targets = [ "//chrome/browser/foo/internal:java" ] |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 504 | namespace = "foo" |
| 505 | no_transitive_deps = true |
Sam Maier | 79e40da | 2023-01-27 17:26:11 | [diff] [blame] | 506 | manual_jni_registration = true |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | # This group is a convenience alias representing the module's native code, |
| 510 | # allowing it to be named "native" for clarity in module descriptors. |
| 511 | group("native") { |
| 512 | deps = [ |
| 513 | ":foo", |
| 514 | ] |
| 515 | } |
| 516 | ``` |
| 517 | |
| 518 | Now, over to the implementation of the module. These are the parts that |
| 519 | shouldn't know or care whether they're living in a module or not. |
| 520 | |
| 521 | Add a stub implementation in |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 522 | `//chrome/browser/foo/internal/android/foo_impl.cc`: |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 523 | |
| 524 | ```c++ |
| 525 | #include "base/logging.h" |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 526 | #include "chrome/browser/foo/internal/jni_headers/FooImpl_jni.h" |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 527 | |
| 528 | static int JNI_FooImpl_Execute(JNIEnv* env) { |
| 529 | LOG(INFO) << "Running foo feature code!"; |
| 530 | return 123; |
| 531 | } |
| 532 | ``` |
| 533 | |
| 534 | And, the associated build config in |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 535 | `//chrome/browser/foo/internal/BUILD.gn`: |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 536 | |
| 537 | ```gn |
| 538 | import("//build/config/android/rules.gni") |
| 539 | |
| 540 | ... |
| 541 | |
| 542 | source_set("native") { |
| 543 | sources = [ |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 544 | "android/foo_impl.cc", |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 545 | ] |
| 546 | |
| 547 | deps = [ |
| 548 | ":jni_headers", |
| 549 | "//base", |
| 550 | ] |
| 551 | } |
| 552 | |
| 553 | generate_jni("jni_headers") { |
| 554 | sources = [ |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 555 | "android/java/src/org/chromium/chrome/browser/foo/FooImpl.java", |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 556 | ] |
| 557 | } |
| 558 | ``` |
| 559 | |
| 560 | With a declaration of the native method on the Java side: |
| 561 | |
| 562 | ```java |
| 563 | public class FooImpl implements Foo { |
| 564 | ... |
| 565 | |
Sam Maier | 79e40da | 2023-01-27 17:26:11 | [diff] [blame] | 566 | @NativeMethods("foo") |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 567 | interface Natives { |
| 568 | int execute(); |
| 569 | } |
| 570 | } |
| 571 | ``` |
| 572 | |
| 573 | Finally, augment the module descriptor in |
| 574 | `//chrome/android/modules/foo/foo_module.gni` with the native dependencies: |
| 575 | |
| 576 | ```gn |
| 577 | foo_module_desc = { |
| 578 | ... |
| 579 | native_deps = [ |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 580 | "//chrome/android/modules/foo/internal:native", |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 581 | "//chrome/browser/foo/internal:native", |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 582 | ] |
Samuel Huang | 3dc9fce8 | 2020-02-26 18:09:57 | [diff] [blame] | 583 | load_native_on_get_impl = true |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 584 | } |
| 585 | ``` |
| 586 | |
Samuel Huang | 3dc9fce8 | 2020-02-26 18:09:57 | [diff] [blame] | 587 | If `load_native_on_get_impl` is set to `true` then Chrome automatically loads |
| 588 | Foo DFM's native libraries and PAK file resources when `FooModule.getImpl()` is |
| 589 | called for the first time. The loading requires Chrome's main native libraries |
| 590 | to be loaded. If you wish to call `FooModule.getImpl()` earlier than that, then |
| 591 | you'd need to set `load_native_on_get_impl` to `false`, and manage native |
| 592 | libraries / resources loading yourself (potentially, on start-up and on install, |
| 593 | or on use). |
| 594 | |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 595 | #### Calling feature module native code from base the module |
| 596 | |
| 597 | If planning to use direct native-native calls into DFM code, then the module |
| 598 | should have a purely virtual interface available. The main module can obtain a |
| 599 | pointer to a DFM-created object or factory (implemented by the feature), and |
| 600 | call its virtual methods. |
| 601 | |
| 602 | Ideally, the interface to the feature will avoid feature-specific types. If a |
Jennifer Nejad | 1a8e208 | 2024-10-15 23:30:32 | [diff] [blame] | 603 | feature defines complex data types, and uses them in its own interface, then it's |
Christopher Grant | f649d28 | 2020-01-09 22:56:08 | [diff] [blame] | 604 | likely the main library will utilize the code backing these types. That code, |
| 605 | and anything it references, will in turn be pulled back into the main library, |
| 606 | negating the intent to house code in the DFM. |
| 607 | |
| 608 | Therefore, designing the feature interface to use C types, C++ standard types, |
| 609 | or classes that aren't expected to move out of Chrome's main library is ideal. |
| 610 | If feature-specific classes are needed, they simply need to avoid referencing |
| 611 | feature library internals. |
Eric Stevenson | 8c9ab26b | 2019-08-30 15:44:40 | [diff] [blame] | 612 | |
Christopher Grant | 8fea5a1 | 2019-07-31 19:12:31 | [diff] [blame] | 613 | ### Adding Android resources |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 614 | |
| 615 | In this section we will add the required build targets to add Android resources |
| 616 | to the Foo DFM. |
| 617 | |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 618 | First, add a resources target to |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 619 | `//chrome/browser/foo/internal/BUILD.gn` and add it as a dependency on |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 620 | Foo's `java` target in the same file: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 621 | |
| 622 | ```gn |
| 623 | ... |
| 624 | android_resources("java_resources") { |
| 625 | # Define like ordinary Android resources target. |
| 626 | ... |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 627 | custom_package = "org.chromium.chrome.browser.foo" |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 628 | } |
| 629 | ... |
| 630 | android_library("java") { |
| 631 | ... |
| 632 | deps = [ |
| 633 | ":java_resources", |
| 634 | ] |
| 635 | } |
| 636 | ``` |
| 637 | |
| 638 | To add strings follow steps |
| 639 | [here](http://dev.chromium.org/developers/design-documents/ui-localization) to |
| 640 | add new Java GRD file. Then create |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 641 | `//chrome/browser/foo/internal/android/resources/strings/android_foo_strings.grd` as |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 642 | follows: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 643 | |
| 644 | ```xml |
| 645 | <?xml version="1.0" encoding="UTF-8"?> |
dpapad | bfad4d4 | 2025-01-14 09:29:31 | [diff] [blame] | 646 | <grit current_release="1" latest_public_release="0"> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 647 | <outputs> |
| 648 | <output |
| 649 | filename="values-am/android_foo_strings.xml" |
| 650 | lang="am" |
| 651 | type="android" /> |
| 652 | <!-- List output file for all other supported languages. See |
Henrique Nakashima | f5439f8 | 2021-01-29 23:52:24 | [diff] [blame] | 653 | //chrome/browser/ui/android/strings/android_chrome_strings.grd for the |
| 654 | full list. --> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 655 | ... |
| 656 | </outputs> |
| 657 | <translations> |
| 658 | <file lang="am" path="vr_translations/android_foo_strings_am.xtb" /> |
| 659 | <!-- Here, too, list XTB files for all other supported languages. --> |
| 660 | ... |
| 661 | </translations> |
Matt Stark | 1debb5de | 2021-02-15 16:08:24 | [diff] [blame] | 662 | <release seq="1"> |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 663 | <messages fallback_to_english="true"> |
| 664 | <message name="IDS_BAR_IMPL_TEXT" desc="Magical string."> |
| 665 | impl |
| 666 | </message> |
| 667 | </messages> |
| 668 | </release> |
| 669 | </grit> |
| 670 | ``` |
| 671 | |
| 672 | Then, create a new GRD target and add it as a dependency on `java_resources` in |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 673 | `//chrome/browser/foo/internal/BUILD.gn`: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 674 | |
| 675 | ```gn |
| 676 | ... |
| 677 | java_strings_grd("java_strings_grd") { |
| 678 | defines = chrome_grit_defines |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 679 | grd_file = "android/resources/strings/android_foo_strings.grd" |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 680 | outputs = [ |
| 681 | "values-am/android_foo_strings.xml", |
| 682 | # Here, too, list output files for other supported languages. |
| 683 | ... |
| 684 | ] |
| 685 | } |
| 686 | ... |
| 687 | android_resources("java_resources") { |
| 688 | ... |
| 689 | deps = [":java_strings_grd"] |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 690 | custom_package = "org.chromium.chrome.browser.foo" |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 691 | } |
| 692 | ... |
| 693 | ``` |
| 694 | |
| 695 | You can then access Foo's resources using the |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 696 | `org.chromium.chrome.browser.foo.R` class. To do this change |
| 697 | `//chrome/browser/foo/internal/android/java/src/org/chromium/chrome/browser/foo/FooImpl.java` |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 698 | to: |
| 699 | |
| 700 | ```java |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 701 | package org.chromium.chrome.browser.foo; |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 702 | |
| 703 | import org.chromium.base.ContextUtils; |
| 704 | import org.chromium.base.Log; |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 705 | import org.chromium.chrome.browser.foo.R; |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 706 | |
| 707 | public class FooImpl implements Foo { |
| 708 | @Override |
| 709 | public void bar() { |
| 710 | Log.i("FOO", ContextUtils.getApplicationContext().getString( |
| 711 | R.string.bar_impl_text)); |
| 712 | } |
| 713 | } |
| 714 | ``` |
| 715 | |
Samuel Huang | 3dc9fce8 | 2020-02-26 18:09:57 | [diff] [blame] | 716 | ### Adding non-string native resources |
| 717 | |
| 718 | This section describes how to add non-string native resources to Foo DFM. |
| 719 | Key ideas: |
| 720 | |
| 721 | * The compiled resource file shipped with the DFM is `foo_resourcess.pak`. |
| 722 | * At run time, native resources need to be loaded before use. Also, DFM native |
| 723 | resources can only be used from the Browser process. |
| 724 | |
| 725 | #### Creating PAK file |
| 726 | |
| 727 | Two ways to create `foo_resourcess.pak` (using GRIT) are: |
| 728 | |
| 729 | 1. (Preferred) Use `foo_resourcess.grd` to refer to individual files (e.g., |
| 730 | images, HTML, JS, or CSS) and assigns resource text IDs. `foo_resourcess.pak` |
| 731 | must have an entry in `/tools/gritsettings/resource_ids.spec`. |
| 732 | 1. Combine existing .pak files via `repack` rules in GN build files. This is |
| 733 | done by the DevUI DFM, which aggregates resources from many DevUI pages. |
| 734 | |
| 735 | #### Loading PAK file |
| 736 | |
| 737 | At runtime, `foo_resources.pak` needs to be loaded (memory-mapped) before any of |
| 738 | its resource gets used. Alternatives to do this are: |
| 739 | |
| 740 | 1. (Simplest) Specify native resources (with native libraries if any exist) to |
| 741 | be automatically loaded on first call to `FooModule.getImpl()`. This behavior |
| 742 | is specified via `load_native_on_get_impl = true` in `foo_module_desc`. |
| 743 | 1. In Java code, call `FooModule.ensureNativeLoaded()`. |
| 744 | 1. In C++ code, use JNI to call `FooModule.ensureNativeLoaded()`. The code to do |
| 745 | this can be placed in a helper class, which can also have JNI calls to |
| 746 | `FooModule.isInstalled()` and `FooModule.installModule()`. |
| 747 | |
| 748 | #### Cautionary notes |
| 749 | |
| 750 | Compiling `foo_resources.pak` auto-generates `foo_resources.h`, which defines |
| 751 | textual resource IDs, e.g., `IDR_FOO_HTML`. C++ code then uses these IDs to get |
| 752 | resource bytes. Unfortunately, this behavior is fragile: If `IDR_FOO_HTML` is |
| 753 | accessed before the Foo DFM is (a) installed, or (b) loaded, then runtime error |
| 754 | ensues! Some mitigation strategies are as follows: |
| 755 | |
| 756 | * (Ideal) Access Foo DFM's native resources only from code in Foo DFM's native |
| 757 | libraries. So by the time that `IDR_FOO_HTML` is accessed, everything is |
| 758 | already in place! This isn't always possible; henceforth we assume that |
| 759 | `IDR_FOO_HTML` is accessed by code in the base DFM. |
| 760 | * Before accessing IDR_FOO_HTML, ensure Foo DFM is installed and loaded. The |
| 761 | latter can use `FooModule.ensureNativeLoaded()` (needs to be called from |
| 762 | Browser thread). |
| 763 | * Use inclusion of `foo_resources.h` to restrict availability of `IDR_FOO_HTML`. |
| 764 | Only C++ files dedicated to "DFM-gated code" (code that runs only when its DFM |
| 765 | is installed and loaded) should include `foo_resources.h`. |
| 766 | |
| 767 | #### Associating native resources with DFM |
| 768 | |
| 769 | Here are the main GN changes to specify PAK files and default loading behavior |
| 770 | for a DFM's native resources: |
| 771 | |
| 772 | ```gn |
| 773 | foo_module_desc = { |
| 774 | ... |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 775 | paks = [ "$root_gen_dir/chrome/browser/foo/internal/foo_resourcess.pak" ] |
| 776 | pak_deps = [ "//chrome/browser/foo/internal:foo_paks" ] |
Samuel Huang | 3dc9fce8 | 2020-02-26 18:09:57 | [diff] [blame] | 777 | load_native_on_get_impl = true |
| 778 | } |
| 779 | ``` |
| 780 | |
| 781 | Note that `load_native_on_get_impl` specifies both native libraries and native |
| 782 | resources. |
| 783 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 784 | |
| 785 | ### Module install |
| 786 | |
| 787 | So far, we have installed the Foo DFM as a true split (`-m foo` option on the |
| 788 | install script). In production, however, we have to explicitly install the Foo |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 789 | DFM for users to get it. There are three install options: _on-demand_, |
| 790 | _deferred_ and _conditional_. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 791 | |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 792 | #### On-demand install |
| 793 | |
| 794 | On-demand requesting a module will try to download and install the |
| 795 | module as soon as possible regardless of whether the user is on a metered |
| 796 | connection or whether they have turned updates off in the Play Store app. |
| 797 | |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 798 | You can use the autogenerated module class to on-demand install the module like |
| 799 | so: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 800 | |
| 801 | ```java |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 802 | FooModule.install((success) -> { |
| 803 | if (success) { |
| 804 | FooModule.getImpl().bar(); |
| 805 | } |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 806 | }); |
| 807 | ``` |
| 808 | |
| 809 | **Optionally**, you can show UI telling the user about the install flow. For |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 810 | this, add a function like the one below. Note, it is possible |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 811 | to only show either one of the install, failure and success UI or any |
| 812 | combination of the three. |
| 813 | |
| 814 | ```java |
| 815 | public static void installModuleWithUi( |
| 816 | Tab tab, OnModuleInstallFinishedListener onFinishedListener) { |
| 817 | ModuleInstallUi ui = |
| 818 | new ModuleInstallUi( |
| 819 | tab, |
| 820 | R.string.foo_module_title, |
| 821 | new ModuleInstallUi.FailureUiListener() { |
| 822 | @Override |
Samuel Huang | febcccd | 2019-08-21 20:48:47 | [diff] [blame] | 823 | public void onFailureUiResponse(retry) { |
| 824 | if (retry) { |
| 825 | installModuleWithUi(tab, onFinishedListener); |
| 826 | } else { |
| 827 | onFinishedListener.onFinished(false); |
| 828 | } |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 829 | } |
| 830 | }); |
| 831 | // At the time of writing, shows toast informing user about install start. |
| 832 | ui.showInstallStartUi(); |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 833 | FooModule.install( |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 834 | (success) -> { |
| 835 | if (!success) { |
| 836 | // At the time of writing, shows infobar allowing user |
| 837 | // to retry install. |
| 838 | ui.showInstallFailureUi(); |
| 839 | return; |
| 840 | } |
| 841 | // At the time of writing, shows toast informing user about |
| 842 | // install success. |
| 843 | ui.showInstallSuccessUi(); |
| 844 | onFinishedListener.onFinished(true); |
| 845 | }); |
| 846 | } |
| 847 | ``` |
| 848 | |
| 849 | To test on-demand install, "fake-install" the DFM. It's fake because |
Peter Wen | 8bf82d4 | 2021-08-13 22:03:54 | [diff] [blame] | 850 | the DFM is not installed as a true split. Instead it will be emulated by play |
| 851 | core's `--local-testing` [mode][play-core-local-testing]. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 852 | Fake-install and launch Chrome with the following command: |
| 853 | |
| 854 | ```shell |
Andrew Grieve | f0d97776 | 2021-08-18 20:20:43 | [diff] [blame] | 855 | $ $OUTDIR/bin/monochrome_public_bundle install -f foo |
Peter Wen | 8bf82d4 | 2021-08-13 22:03:54 | [diff] [blame] | 856 | $ $OUTDIR/bin/monochrome_public_bundle launch |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 857 | ``` |
| 858 | |
| 859 | When running the install code, the Foo DFM module will be emulated. |
| 860 | This will be the case in production right after installing the module. Emulation |
| 861 | will last until Play Store has a chance to install your module as a true split. |
Peter Wen | 577a6fe5 | 2019-12-11 22:02:05 | [diff] [blame] | 862 | This usually takes about a day. After it has been installed, it will be updated |
| 863 | atomically alongside Chrome. Always check that it is installed and available |
| 864 | before invoking code within the DFM. |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 865 | |
| 866 | *** note |
| 867 | **Warning:** There are subtle differences between emulating a module and |
| 868 | installing it as a true split. We therefore recommend that you always test both |
| 869 | install methods. |
| 870 | *** |
| 871 | |
Samuel Huang | 6f5c7ddb8 | 2020-05-14 17:10:52 | [diff] [blame] | 872 | *** note |
| 873 | To simplify development, the DevUI DFM (dev_ui) is installed by default, i.e., |
| 874 | `-m dev_ui` is implied by default. This is overridden by: |
| 875 | * `--no-module dev_ui`, to test error from missing DevUI, |
| 876 | * `-f dev_ui`, for fake module install. |
| 877 | *** |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 878 | |
| 879 | #### Deferred install |
| 880 | |
| 881 | Deferred install means that the DFM is installed in the background when the |
| 882 | device is on an unmetered connection and charging. The DFM will only be |
| 883 | available after Chrome restarts. When deferred installing a module it will |
| 884 | not be faked installed. |
| 885 | |
| 886 | To defer install Foo do the following: |
| 887 | |
| 888 | ```java |
Tibor Goldschwendt | 573cf302 | 2019-05-10 17:23:30 | [diff] [blame] | 889 | FooModule.installDeferred(); |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 890 | ``` |
| 891 | |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 892 | #### Conditional install |
| 893 | |
| 894 | Conditional install means the DFM will be installed automatically upon first |
| 895 | installing or updating Chrome if the device supports a particular feature. |
| 896 | Conditional install is configured in the module's manifest. To install your |
| 897 | module on all Daydream-ready devices for instance, your |
Henrique Nakashima | cfdcce3 | 2020-04-24 22:19:36 | [diff] [blame] | 898 | `//chrome/android/modules/foo/internal/java/AndroidManifest.xml` should look |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 899 | like this: |
| 900 | |
| 901 | ```xml |
| 902 | <?xml version="1.0" encoding="utf-8"?> |
| 903 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 904 | xmlns:dist="http://schemas.android.com/apk/distribution" |
| 905 | featureSplit="foo"> |
| 906 | |
| 907 | <dist:module |
| 908 | dist:instant="false" |
| 909 | dist:title="@string/foo_module_title"> |
Ben Mason | e571ea5a | 2019-09-06 18:29:37 | [diff] [blame] | 910 | <dist:fusing dist:include="true" /> |
Tibor Goldschwendt | 68c5f72 | 2019-08-01 15:10:15 | [diff] [blame] | 911 | <dist:delivery> |
| 912 | <dist:install-time> |
| 913 | <dist:conditions> |
| 914 | <dist:device-feature |
| 915 | dist:name="android.hardware.vr.high_performance" /> |
| 916 | </dist:conditions> |
| 917 | </dist:install-time> |
| 918 | <!-- Allows on-demand or deferred install on non-Daydream-ready |
| 919 | devices. --> |
| 920 | <dist:on-demand /> |
| 921 | </dist:delivery> |
| 922 | </dist:module> |
| 923 | |
| 924 | <application /> |
| 925 | </manifest> |
| 926 | ``` |
| 927 | |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 928 | You can also specify no conditions to have your module always installed. |
| 929 | You might want to do this in order to delay the performance implications |
| 930 | of loading your module until its first use (true only on Android O+ where |
| 931 | [android:isolatedSplits](https://developer.android.com/reference/android/R.attr#isolatedSplits) |
| 932 | is supported. See [go/isolated-splits-dev-guide](http://go/isolated-splits-dev-guide) |
| 933 | (googlers only). |
| 934 | |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 935 | ### chrome_public_apk and Integration Tests |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 936 | |
Andrew Grieve | f6069feb | 2021-04-29 18:29:17 | [diff] [blame] | 937 | To make the Foo feature available in the non-bundle `chrome_public_apk` |
Andrew Grieve | 982fe8a | 2024-01-02 16:41:51 | [diff] [blame] | 938 | target, add the `java` target to the template in |
| 939 | `//chrome/android/chrome_public_apk_tmpl.gni` like so: |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 940 | |
| 941 | ```gn |
Andrew Grieve | 982fe8a | 2024-01-02 16:41:51 | [diff] [blame] | 942 | # Add to where "chrome_all_java" is added: |
| 943 | if (!_is_bundle) { |
| 944 | deps += [ "//chrome/browser/foo/internal:java" ] |
Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | ``` |
| 948 | |
Martin Kong | dfea3ec | 2025-01-16 21:40:43 | [diff] [blame] | 949 | You may also have to add `java` as a dependency of |
| 950 | `//chrome/android/javatests/chrome_test_java_org.chromium.chrome.browser.foo` |
| 951 | if you want to call into Foo from test code. |
Peter Wen | 8bf82d4 | 2021-08-13 22:03:54 | [diff] [blame] | 952 | |
Mohamed Heikal | f9d9edb | 2021-10-04 20:05:10 | [diff] [blame] | 953 | [play-core-local-testing]: https://developer.android.com/guide/playcore/feature-delivery/on-demand#local-testing |