Tibor Goldschwendt | 19364ba | 2019-04-10 15:59:55 | [diff] [blame^] | 1 | # Dynamic Feature Modules (DFMs) |
| 2 | |
| 3 | [Android App bundles and Dynamic Feature Modules (DFMs)](https://developer.android.com/guide/app-bundle) |
| 4 | is a Play Store feature that allows delivering pieces of an app when they are |
| 5 | needed rather than at install time. We use DFMs to modularize Chrome and make |
| 6 | Chrome's install size smaller. |
| 7 | |
| 8 | [TOC] |
| 9 | |
| 10 | |
| 11 | ## Limitations |
| 12 | |
| 13 | Currently (March 2019), DFMs have the following limitations: |
| 14 | |
| 15 | * **WebView:** We don't support DFMs for WebView. If your feature is used by |
| 16 | WebView you cannot put it into a DFM. See |
| 17 | [crbug/949717](https://bugs.chromium.org/p/chromium/issues/detail?id=949717) |
| 18 | for progress. |
| 19 | * **Android K:** DFMs are based on split APKs, a feature introduced in Android |
| 20 | L. Therefore, we don't support DFMs on Android K. As a workaround |
| 21 | you can add your feature to the Android K APK build. See |
| 22 | [crbug/881354](https://bugs.chromium.org/p/chromium/issues/detail?id=881354) |
| 23 | for progress. |
| 24 | * **Native Code:** We cannot move native Chrome code into a DFM. See |
| 25 | [crbug/874564](https://bugs.chromium.org/p/chromium/issues/detail?id=874564) |
| 26 | for progress. |
| 27 | |
| 28 | ## Getting started |
| 29 | |
| 30 | This guide walks you through the steps to create a DFM called _Foo_ and add it |
| 31 | to the public Monochrome bundle. If you want to ship a DFM, you will also have |
| 32 | to add it to the public Chrome Modern and Trichrome Chrome bundle as well as the |
| 33 | downstream bundles. |
| 34 | |
| 35 | *** note |
| 36 | **Note:** To make your own module you'll essentially have to replace every |
| 37 | instance of `foo`/`Foo`/`FOO` with `your_feature_name`/`YourFeatureName`/ |
| 38 | `YOUR_FEATURE_NAME`. |
| 39 | *** |
| 40 | |
| 41 | |
| 42 | ### Create DFM target |
| 43 | |
| 44 | DFMs are APKs. They have a manifest and can contain Java and native code as well |
| 45 | as resources. This section walks you through creating the module target in our |
| 46 | build system. |
| 47 | |
| 48 | First, create the file `//chrome/android/features/foo/java/AndroidManifest.xml` |
| 49 | and add: |
| 50 | |
| 51 | ```xml |
| 52 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 53 | xmlns:dist="http://schemas.android.com/apk/distribution" |
| 54 | featureSplit="foo" |
| 55 | package="{{manifest_package}}"> |
| 56 | |
| 57 | <!-- For Chrome Modern use android:minSdkVersion="21". --> |
| 58 | <uses-sdk |
| 59 | android:minSdkVersion="24" |
| 60 | android:targetSdkVersion="{{target_sdk_version}}" /> |
| 61 | |
| 62 | <!-- dist:onDemand="true" makes this a separately installed module. |
| 63 | dist:onDemand="false" would always install the module alongside the |
| 64 | rest of Chrome. --> |
| 65 | <dist:module |
| 66 | dist:onDemand="true" |
| 67 | dist:title="@string/foo_module_title"> |
| 68 | <!-- This will prevent the module to become part of the Android K |
| 69 | build in case we ever want to use bundles on Android K. --> |
| 70 | <dist:fusing dist:include="false" /> |
| 71 | </dist:module> |
| 72 | |
| 73 | <!-- Remove hasCode="false" when adding Java code. --> |
| 74 | <application hasCode="false" /> |
| 75 | </manifest> |
| 76 | ``` |
| 77 | |
| 78 | Then, add a package ID for Foo so that Foo's resources have unique identifiers. |
| 79 | For this, add a new ID to |
| 80 | `//chrome/android/features/module_names_to_package_ids.gni`: |
| 81 | |
| 82 | ```gn |
| 83 | resource_packages_id_mapping = [ |
| 84 | ..., |
| 85 | "foo=0x{XX}", # Set {XX} to next lower hex number. |
| 86 | ] |
| 87 | ``` |
| 88 | |
| 89 | Next, create a template that contains the Foo module target. |
| 90 | |
| 91 | *** note |
| 92 | **Note:** We put the module target into a template because we have to |
| 93 | instantiate it for each Chrome bundle (Chrome Modern, Monochrome and Trichrome |
| 94 | for both upstream and downstream) you want to ship your module in. |
| 95 | *** |
| 96 | |
| 97 | To do this, create `//chrome/android/features/foo/foo_module_tmpl.gni` and add |
| 98 | the following: |
| 99 | |
| 100 | ```gn |
| 101 | import("//build/config/android/rules.gni") |
| 102 | import("//build/config/locales.gni") |
| 103 | import("//chrome/android/features/module_names_to_package_ids.gni") |
| 104 | |
| 105 | template("foo_module_tmpl") { |
| 106 | _manifest = "$target_gen_dir/$target_name/AndroidManifest.xml" |
| 107 | _manifest_target = "${target_name}__manifest" |
| 108 | jinja_template(_manifest_target) { |
| 109 | input = "//chrome/android/features/foo/java/AndroidManifest.xml" |
| 110 | output = _manifest |
| 111 | variables = [ |
| 112 | "target_sdk_version=$android_sdk_version", |
| 113 | "manifest_package=${invoker.manifest_package}", |
| 114 | ] |
| 115 | } |
| 116 | |
| 117 | android_app_bundle_module(target_name) { |
| 118 | forward_variables_from(invoker, |
| 119 | [ |
| 120 | "base_module_target", |
| 121 | "module_name", |
| 122 | "uncompress_shared_libraries", |
| 123 | "version_code", |
| 124 | "version_name", |
| 125 | ]) |
| 126 | android_manifest = _manifest |
| 127 | android_manifest_dep = ":${_manifest_target}" |
| 128 | proguard_enabled = !is_java_debug |
| 129 | aapt_locale_whitelist = locales |
| 130 | package_name = "foo" |
| 131 | package_name_to_id_mapping = resource_packages_id_mapping |
| 132 | } |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | Then, instantiate the module template in `//chrome/android/BUILD.gn` inside the |
| 137 | `monochrome_public_bundle_tmpl` template and add it to the bundle target: |
| 138 | |
| 139 | ```gn |
| 140 | ... |
| 141 | import("modules/foo/foo_module_tmpl.gni") |
| 142 | ... |
| 143 | template("monochrome_public_bundle_tmpl") { |
| 144 | ... |
| 145 | foo_module_tmpl("${target_name}__foo_bundle_module") { |
| 146 | manifest_package = manifest_package |
| 147 | module_name = "Foo" + _bundle_name |
| 148 | base_module_target = ":$_base_module_target_name" |
| 149 | version_code = monochrome_version_code |
| 150 | version_name = chrome_version_name |
| 151 | uncompress_shared_libraries = true |
| 152 | } |
| 153 | ... |
| 154 | android_app_bundle(target_name) { |
| 155 | ... |
| 156 | extra_modules += [ |
| 157 | { |
| 158 | name = "foo" |
| 159 | module_target = ":${target_name}__foo_bundle_module" |
| 160 | }, |
| 161 | ] |
| 162 | } |
| 163 | } |
| 164 | ``` |
| 165 | |
| 166 | The next step is to add Foo to the list of feature modules for UMA recording. |
| 167 | For this, add `foo` to the `AndroidFeatureModuleName` in |
| 168 | `//tools/metrics/histograms/histograms.xml`: |
| 169 | |
| 170 | ```xml |
| 171 | <histogram_suffixes name="AndroidFeatureModuleName" ...> |
| 172 | ... |
| 173 | <suffix name="foo" label="Super Duper Foo Module" /> |
| 174 | ... |
| 175 | </histogram_suffixes> |
| 176 | ``` |
| 177 | |
| 178 | <!--- TODO(tiborg): Add info about install UI. --> |
| 179 | Lastly, give your module a title that Chrome and Play can use for the install |
| 180 | UI. To do this, add a string to |
| 181 | `//chrome/android/java/strings/android_chrome_strings.grd`: |
| 182 | |
| 183 | ```xml |
| 184 | ... |
| 185 | <message name="IDS_FOO_MODULE_TITLE" |
| 186 | desc="Text shown when the Foo module is referenced in install start, success, |
| 187 | failure UI (e.g. in IDS_MODULE_INSTALL_START_TEXT, which will expand to |
| 188 | 'Installing Foo for Chrome…')."> |
| 189 | Foo |
| 190 | </message> |
| 191 | ... |
| 192 | ``` |
| 193 | |
| 194 | Congrats! You added the DFM Foo to Monochrome. That is a big step but not very |
| 195 | useful so far. In the next sections you'll learn how to add code and resources |
| 196 | to it. |
| 197 | |
| 198 | |
| 199 | ### Building and installing modules |
| 200 | |
| 201 | Before we are going to jump into adding content to Foo, let's take a look on how |
| 202 | to build and deploy the Monochrome bundle with the Foo DFM. The remainder of |
| 203 | this guide assumes the environment variable `OUTDIR` is set to a properly |
| 204 | configured GN build directory (e.g. `out/Debug`). |
| 205 | |
| 206 | To build and install the Monochrome bundle to your connected device, run: |
| 207 | |
| 208 | ```shell |
| 209 | $ autoninja -C $OUTDIR monochrome_public_bundle |
| 210 | $ $OUTDIR/bin/monochrome_public_bundle install -m base -m foo |
| 211 | ``` |
| 212 | |
| 213 | This will install Foo alongside the rest of Chrome. The rest of Chrome is called |
| 214 | _base_ module in the bundle world. The Base module will always be put on the |
| 215 | device when initially installing Chrome. |
| 216 | |
| 217 | *** note |
| 218 | **Note:** You have to specify `-m base` here to make it explicit which modules |
| 219 | will be installed. If you only specify `-m foo` the command will fail. It is |
| 220 | also possible to specify no modules. In that case, the script will install the |
| 221 | set of modules that the Play Store would install when first installing Chrome. |
| 222 | That may be different than just specifying `-m base` if we have non-on-demand |
| 223 | modules. |
| 224 | *** |
| 225 | |
| 226 | You can then check that the install worked with: |
| 227 | |
| 228 | ```shell |
| 229 | $ adb shell dumpsys package org.chromium.chrome | grep splits |
| 230 | > splits=[base, config.en, foo] |
| 231 | ``` |
| 232 | |
| 233 | Then try installing the Monochrome bundle without your module and print the |
| 234 | installed modules: |
| 235 | |
| 236 | ```shell |
| 237 | $ $OUTDIR/bin/monochrome_public_bundle install -m base |
| 238 | $ adb shell dumpsys package org.chromium.chrome | grep splits |
| 239 | > splits=[base, config.en] |
| 240 | ``` |
| 241 | |
| 242 | |
| 243 | ### Adding java code |
| 244 | |
| 245 | To make Foo useful, let's add some Java code to it. This section will walk you |
| 246 | through the required steps. |
| 247 | |
| 248 | First, define a module interface in the new file |
| 249 | `//chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/Foo.java`: |
| 250 | |
| 251 | ```java |
| 252 | package org.chromium.chrome.features.foo; |
| 253 | |
| 254 | /** Interface to call into Foo feature. */ |
| 255 | public interface Foo { |
| 256 | /** Magical function. */ |
| 257 | void bar(); |
| 258 | } |
| 259 | ``` |
| 260 | |
| 261 | *** note |
| 262 | **Note:** To reflect the separation from "Chrome browser" code, features should |
| 263 | be defined in their own package name, distinct from the chrome package - i.e. |
| 264 | `org.chromium.chrome.features.<feature-name>`. |
| 265 | *** |
| 266 | |
| 267 | Next, define an implementation that goes into the module in the new file |
| 268 | `//chrome/android/features/foo/java/src/org/chromium/chrome/features/foo/FooImpl.java`: |
| 269 | |
| 270 | ```java |
| 271 | package org.chromium.chrome.features.foo; |
| 272 | |
| 273 | import org.chromium.base.Log; |
| 274 | |
| 275 | public class FooImpl implements Foo { |
| 276 | @Override |
| 277 | public void bar() { |
| 278 | Log.i("FOO", "bar in module"); |
| 279 | } |
| 280 | } |
| 281 | ``` |
| 282 | |
| 283 | In order to get the Foo implementation depending on whether the Foo DFM |
| 284 | is present, we will add a module provider class handling that logic. For |
| 285 | this, create the file |
| 286 | `//chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/FooModuleProvider.java` |
| 287 | and add: |
| 288 | |
| 289 | ```java |
| 290 | package org.chromium.chrome.features.foo; |
| 291 | |
| 292 | /** Provides the Foo implementation. */ |
| 293 | public class FooModuleProvider { |
| 294 | private static Foo sFoo; |
| 295 | |
| 296 | /** |
| 297 | * Returns Foo implementation or null if Foo module is not installed. |
| 298 | */ |
| 299 | public static Foo getFoo { |
| 300 | if (sFoo == null) { |
| 301 | try { |
| 302 | sFoo = (Foo) Class |
| 303 | .forName("org.chromium.chrome.features.foo.FooImpl") |
| 304 | .newInstance(); |
| 305 | } catch (ClassNotFoundException | InstantiationException |
| 306 | | IllegalAccessException | IllegalArgumentException e) { |
| 307 | // Foo module is not installed. Leave sFoo as null. |
| 308 | } |
| 309 | } |
| 310 | return sFoo; |
| 311 | } |
| 312 | } |
| 313 | ``` |
| 314 | |
| 315 | You can then use this provider to access the module if it is installed. To test |
| 316 | that, instantiate Foo and call `bar()` somewhere in Chrome: |
| 317 | |
| 318 | ```java |
| 319 | if (FooModuleProvider.getFoo() != null) { |
| 320 | FooModuleProvider.getFoo().bar(); |
| 321 | } else { |
| 322 | Log.i("FOO", "module not installed"); |
| 323 | } |
| 324 | ``` |
| 325 | |
| 326 | The interface and module provider have to be available regardless of whether the |
| 327 | Foo DFM is present. Therefore, put those classes into the base module. For this |
| 328 | create a list of those Java files in |
| 329 | `//chrome/android/features/foo/public/foo_public_java_sources.gni`: |
| 330 | |
| 331 | ```gn |
| 332 | foo_public_java_sources = [ |
| 333 | "//chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/Foo.java", |
| 334 | "//chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/FooModuleProvider.java", |
| 335 | ] |
| 336 | ``` |
| 337 | |
| 338 | Then add this list to `chrome_java in //chrome/android/BUILD.gn`: |
| 339 | |
| 340 | ```gn |
| 341 | ... |
| 342 | import("modules/foo/public/foo_public_java_sources.gni") |
| 343 | ... |
| 344 | android_library("chrome_java") { |
| 345 | ... |
| 346 | java_files += foo_public_java_sources |
| 347 | } |
| 348 | ... |
| 349 | ``` |
| 350 | |
| 351 | The actual implementation, however, should go into the Foo DFM. For this |
| 352 | purpose, create a new file `//chrome/android/features/foo/BUILD.gn` and make a |
| 353 | library with the module Java code in it: |
| 354 | |
| 355 | ```gn |
| 356 | import("//build/config/android/rules.gni") |
| 357 | |
| 358 | android_library("java") { |
| 359 | # Define like ordinary Java Android library. |
| 360 | java_files = [ |
| 361 | "java/src/org/chromium/chrome/features/foo/FooImpl.java", |
| 362 | # Add other Java classes that should go into the Foo DFM here. |
| 363 | ] |
| 364 | # Put other Chrome libs into the classpath so that you can call into the rest |
| 365 | # of Chrome from the Foo DFM. |
| 366 | classpath_deps = [ |
| 367 | "//base:base_java", |
| 368 | "//chrome/android:chrome_java", |
| 369 | # etc. |
| 370 | # Also, you'll need to depend on any //third_party or //components code you |
| 371 | # are using in the module code. |
| 372 | ] |
| 373 | } |
| 374 | ``` |
| 375 | |
| 376 | Then, add this new library as a dependency of the Foo module target in |
| 377 | `//chrome/android/features/foo/foo_module_tmpl.gni`: |
| 378 | |
| 379 | ```gn |
| 380 | android_app_bundle_module(target_name) { |
| 381 | ... |
| 382 | deps = [ |
| 383 | "//chrome/android/module/foo:java", |
| 384 | ] |
| 385 | } |
| 386 | ``` |
| 387 | |
| 388 | Finally, tell Android that your module is now containing code. Do that by |
| 389 | removing the `hasCode="false"` attribute from the `<application>` tag in |
| 390 | `//chrome/android/features/foo/java/AndroidManifest.xml`. You should be left |
| 391 | with an empty tag like so: |
| 392 | |
| 393 | ```xml |
| 394 | ... |
| 395 | <application /> |
| 396 | ... |
| 397 | ``` |
| 398 | |
| 399 | Rebuild and install `monochrome_public_bundle`. Start Chrome and run through a |
| 400 | flow that tries to executes `bar()`. Depending on whether you installed your |
| 401 | module (`-m foo`) "`bar in module`" or "`module not installed`" is printed to |
| 402 | logcat. Yay! |
| 403 | |
| 404 | |
| 405 | ### Adding native code |
| 406 | |
| 407 | Coming soon ( |
| 408 | [crbug/874564](https://bugs.chromium.org/p/chromium/issues/detail?id=874564)). |
| 409 | |
| 410 | You can already add third party native code or native Chrome code that has no |
| 411 | dependency on other Chrome code. To add such code add it as a loadable module to |
| 412 | the bundle module target in `//chrome/android/features/foo/foo_module_tmpl.gni`: |
| 413 | |
| 414 | ```gn |
| 415 | ... |
| 416 | template("foo_module_tmpl") { |
| 417 | ... |
| 418 | android_app_bundle_module(target_name) { |
| 419 | ... |
| 420 | loadable_modules = [ "//path/to/lib.so" ] |
| 421 | } |
| 422 | } |
| 423 | ``` |
| 424 | |
| 425 | |
| 426 | ### Adding android resources |
| 427 | |
| 428 | In this section we will add the required build targets to add Android resources |
| 429 | to the Foo DFM. |
| 430 | |
| 431 | First, add a resources target to `//chrome/android/features/foo/BUILD.gn` and |
| 432 | add it as a dependency on Foo's `java` target in the same file: |
| 433 | |
| 434 | ```gn |
| 435 | ... |
| 436 | android_resources("java_resources") { |
| 437 | # Define like ordinary Android resources target. |
| 438 | ... |
| 439 | custom_package = "org.chromium.chrome.features.foo" |
| 440 | } |
| 441 | ... |
| 442 | android_library("java") { |
| 443 | ... |
| 444 | deps = [ |
| 445 | ":java_resources", |
| 446 | ] |
| 447 | } |
| 448 | ``` |
| 449 | |
| 450 | To add strings follow steps |
| 451 | [here](http://dev.chromium.org/developers/design-documents/ui-localization) to |
| 452 | add new Java GRD file. Then create |
| 453 | `//chrome/android/features/foo/java/strings/android_foo_strings.grd` as follows: |
| 454 | |
| 455 | ```xml |
| 456 | <?xml version="1.0" encoding="UTF-8"?> |
| 457 | <grit |
| 458 | current_release="1" |
| 459 | latest_public_release="0" |
| 460 | output_all_resource_defines="false"> |
| 461 | <outputs> |
| 462 | <output |
| 463 | filename="values-am/android_foo_strings.xml" |
| 464 | lang="am" |
| 465 | type="android" /> |
| 466 | <!-- List output file for all other supported languages. See |
| 467 | //chrome/android/java/strings/android_chrome_strings.grd for the full |
| 468 | list. --> |
| 469 | ... |
| 470 | </outputs> |
| 471 | <translations> |
| 472 | <file lang="am" path="vr_translations/android_foo_strings_am.xtb" /> |
| 473 | <!-- Here, too, list XTB files for all other supported languages. --> |
| 474 | ... |
| 475 | </translations> |
| 476 | <release allow_pseudo="false" seq="1"> |
| 477 | <messages fallback_to_english="true"> |
| 478 | <message name="IDS_BAR_IMPL_TEXT" desc="Magical string."> |
| 479 | impl |
| 480 | </message> |
| 481 | </messages> |
| 482 | </release> |
| 483 | </grit> |
| 484 | ``` |
| 485 | |
| 486 | Then, create a new GRD target and add it as a dependency on `java_resources` in |
| 487 | `//chrome/android/features/foo/BUILD.gn`: |
| 488 | |
| 489 | ```gn |
| 490 | ... |
| 491 | java_strings_grd("java_strings_grd") { |
| 492 | defines = chrome_grit_defines |
| 493 | grd_file = "java/strings/android_foo_strings.grd" |
| 494 | outputs = [ |
| 495 | "values-am/android_foo_strings.xml", |
| 496 | # Here, too, list output files for other supported languages. |
| 497 | ... |
| 498 | ] |
| 499 | } |
| 500 | ... |
| 501 | android_resources("java_resources") { |
| 502 | ... |
| 503 | deps = [":java_strings_grd"] |
| 504 | custom_package = "org.chromium.chrome.features.foo" |
| 505 | } |
| 506 | ... |
| 507 | ``` |
| 508 | |
| 509 | You can then access Foo's resources using the |
| 510 | `org.chromium.chrome.features.foo.R` class. To do this change |
| 511 | `//chrome/android/features/foo/java/src/org/chromium/chrome/features/foo/FooImpl.java` |
| 512 | to: |
| 513 | |
| 514 | ```java |
| 515 | package org.chromium.chrome.features.foo; |
| 516 | |
| 517 | import org.chromium.base.ContextUtils; |
| 518 | import org.chromium.base.Log; |
| 519 | import org.chromium.chrome.features.foo.R; |
| 520 | |
| 521 | public class FooImpl implements Foo { |
| 522 | @Override |
| 523 | public void bar() { |
| 524 | Log.i("FOO", ContextUtils.getApplicationContext().getString( |
| 525 | R.string.bar_impl_text)); |
| 526 | } |
| 527 | } |
| 528 | ``` |
| 529 | |
| 530 | *** note |
| 531 | **Warning:** While your module is emulated (see [below](#on-demand-install)) |
| 532 | your resources are only available through |
| 533 | `ContextUtils.getApplicationContext()`. Not through activities, etc. We |
| 534 | therefore recommend that you only access DFM resources this way. See |
| 535 | [crbug/949729](https://bugs.chromium.org/p/chromium/issues/detail?id=949729) |
| 536 | for progress on making this more robust. |
| 537 | *** |
| 538 | |
| 539 | |
| 540 | ### Module install |
| 541 | |
| 542 | So far, we have installed the Foo DFM as a true split (`-m foo` option on the |
| 543 | install script). In production, however, we have to explicitly install the Foo |
| 544 | DFM for users to get it. There are two install options: _on-demand_ and |
| 545 | _deferred_. |
| 546 | |
| 547 | |
| 548 | #### On-demand install |
| 549 | |
| 550 | On-demand requesting a module will try to download and install the |
| 551 | module as soon as possible regardless of whether the user is on a metered |
| 552 | connection or whether they have turned updates off in the Play Store app. |
| 553 | |
| 554 | To request a module on-demand we can make use of the `ModuleInstaller` from |
| 555 | `//components/module_installer/`. For this add, the following function to |
| 556 | `FooModuleProvider` in |
| 557 | `//chrome/android/features/foo/public/java/src/org/chromium/chrome/foo/FooModuleProvider.java`: |
| 558 | |
| 559 | ```java |
| 560 | /** |
| 561 | * On-demand install Foo module. |
| 562 | * @param onFinishedListener listener to be called when install has finished. |
| 563 | */ |
| 564 | public static installModule( |
| 565 | OnModuleInstallFinishedListener onFinishedListener) { |
| 566 | ModuleInstaller.install("foo", (success) -> { |
| 567 | if (success) { |
| 568 | assert getFoo() != null; |
| 569 | } |
| 570 | onFinishedListener.onFinished(success); |
| 571 | }); |
| 572 | } |
| 573 | ``` |
| 574 | |
| 575 | Then, use this new function to request the module and call `bar()` on install |
| 576 | completion: |
| 577 | |
| 578 | ```java |
| 579 | // Need to call init before accessing any modules. Can be called multiple times. |
| 580 | ModuleInstaller.init(); |
| 581 | FooModuleProvider.installModule((success) -> { |
| 582 | FooModuleProvider.getFoo().bar(); |
| 583 | }); |
| 584 | ``` |
| 585 | |
| 586 | **Optionally**, you can show UI telling the user about the install flow. For |
| 587 | this, add the function below to `FooModuleProvider`. Then use |
| 588 | `installModuleWithUi(...)` instead of `installModule(...)`. Note, it is possible |
| 589 | to only show either one of the install, failure and success UI or any |
| 590 | combination of the three. |
| 591 | |
| 592 | ```java |
| 593 | public static void installModuleWithUi( |
| 594 | Tab tab, OnModuleInstallFinishedListener onFinishedListener) { |
| 595 | ModuleInstallUi ui = |
| 596 | new ModuleInstallUi( |
| 597 | tab, |
| 598 | R.string.foo_module_title, |
| 599 | new ModuleInstallUi.FailureUiListener() { |
| 600 | @Override |
| 601 | public void onRetry() { |
| 602 | installModuleWithUi(tab, onFinishedListener); |
| 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public void onCancel() { |
| 607 | onFinishedListener.onFinished(false); |
| 608 | } |
| 609 | }); |
| 610 | // At the time of writing, shows toast informing user about install start. |
| 611 | ui.showInstallStartUi(); |
| 612 | installModule( |
| 613 | (success) -> { |
| 614 | if (!success) { |
| 615 | // At the time of writing, shows infobar allowing user |
| 616 | // to retry install. |
| 617 | ui.showInstallFailureUi(); |
| 618 | return; |
| 619 | } |
| 620 | // At the time of writing, shows toast informing user about |
| 621 | // install success. |
| 622 | ui.showInstallSuccessUi(); |
| 623 | onFinishedListener.onFinished(true); |
| 624 | }); |
| 625 | } |
| 626 | ``` |
| 627 | |
| 628 | To test on-demand install, "fake-install" the DFM. It's fake because |
| 629 | the DFM is not installed as a true split. Instead it will be emulated by Chrome. |
| 630 | Fake-install and launch Chrome with the following command: |
| 631 | |
| 632 | ```shell |
| 633 | $ $OUTDIR/bin/monochrome_public_bundle install -m base -f foo |
| 634 | $ $OUTDIR/bin/monochrome_public_bundle launch \ |
| 635 | --args="--fake-feature-module-install" |
| 636 | ``` |
| 637 | |
| 638 | When running the install code, the Foo DFM module will be emulated. |
| 639 | This will be the case in production right after installing the module. Emulation |
| 640 | will last until Play Store has a chance to install your module as a true split. |
| 641 | This usually takes about a day. |
| 642 | |
| 643 | *** note |
| 644 | **Warning:** There are subtle differences between emulating a module and |
| 645 | installing it as a true split. We therefore recommend that you always test both |
| 646 | install methods. |
| 647 | *** |
| 648 | |
| 649 | |
| 650 | #### Deferred install |
| 651 | |
| 652 | Deferred install means that the DFM is installed in the background when the |
| 653 | device is on an unmetered connection and charging. The DFM will only be |
| 654 | available after Chrome restarts. When deferred installing a module it will |
| 655 | not be faked installed. |
| 656 | |
| 657 | To defer install Foo do the following: |
| 658 | |
| 659 | ```java |
| 660 | ModuleInstaller.installDeferred("foo"); |
| 661 | ``` |
| 662 | |
| 663 | |
| 664 | ### Integration test APK and Android K support |
| 665 | |
| 666 | On Android K we still ship an APK. To make the Foo feature available on Android |
| 667 | K add its code to the APK build. For this, add the `java` target to |
| 668 | the `chrome_public_common_apk_or_module_tmpl` in |
| 669 | `//chrome/android/chrome_public_apk_tmpl.gni` like so: |
| 670 | |
| 671 | ```gn |
| 672 | template("chrome_public_common_apk_or_module_tmpl") { |
| 673 | ... |
| 674 | target(_target_type, target_name) { |
| 675 | ... |
| 676 | if (_target_type != "android_app_bundle_module") { |
| 677 | deps += [ |
| 678 | "//chrome/android/module/foo:java", |
| 679 | ] |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | ``` |
| 684 | |
| 685 | This will also add Foo's Java to the integration test APK. You may also have to |
| 686 | add `java` as a dependency of `chrome_test_java` if you want to call into Foo |
| 687 | from test code. |