blob: 6e00ac1f756b6d6fee81206f3fd448fbd707bc5b [file] [log] [blame] [view]
Tibor Goldschwendt19364ba2019-04-10 15:59:551# Dynamic Feature Modules (DFMs)
2
3[Android App bundles and Dynamic Feature Modules (DFMs)](https://developer.android.com/guide/app-bundle)
4is a Play Store feature that allows delivering pieces of an app when they are
5needed rather than at install time. We use DFMs to modularize Chrome and make
6Chrome's install size smaller.
7
8[TOC]
9
10
11## Limitations
12
Tibor Goldschwendt68c5f722019-08-01 15:10:1513DFMs have the following limitations:
Tibor Goldschwendt19364ba2019-04-10 15:59:5514
15* **WebView:** We don't support DFMs for WebView. If your feature is used by
Tibor Goldschwendt68c5f722019-08-01 15:10:1516 WebView you cannot put it into a DFM.
Tibor Goldschwendt19364ba2019-04-10 15:59:5517* **Android K:** DFMs are based on split APKs, a feature introduced in Android
18 L. Therefore, we don't support DFMs on Android K. As a workaround
Tibor Goldschwendt68c5f722019-08-01 15:10:1519 you can add your feature to the Android K APK build. See below for details.
Tibor Goldschwendt19364ba2019-04-10 15:59:5520
21## Getting started
22
23This guide walks you through the steps to create a DFM called _Foo_ and add it
Tibor Goldschwendtaef8e392019-07-19 16:39:1024to the Chrome bundles.
Tibor Goldschwendt19364ba2019-04-10 15:59:5525
26*** note
27**Note:** To make your own module you'll essentially have to replace every
28instance of `foo`/`Foo`/`FOO` with `your_feature_name`/`YourFeatureName`/
29`YOUR_FEATURE_NAME`.
30***
31
32
33### Create DFM target
34
35DFMs are APKs. They have a manifest and can contain Java and native code as well
36as resources. This section walks you through creating the module target in our
37build system.
38
Tibor Goldschwendt68c5f722019-08-01 15:10:1539First, create the file
40`//chrome/android/features/foo/internal/java/AndroidManifest.xml` and add:
Tibor Goldschwendt19364ba2019-04-10 15:59:5541
42```xml
Tibor Goldschwendt68c5f722019-08-01 15:10:1543<?xml version="1.0" encoding="utf-8"?>
Tibor Goldschwendt19364ba2019-04-10 15:59:5544<manifest xmlns:android="http://schemas.android.com/apk/res/android"
45 xmlns:dist="http://schemas.android.com/apk/distribution"
Tibor Goldschwendt5172118f2019-06-24 21:57:4746 featureSplit="foo">
Tibor Goldschwendt19364ba2019-04-10 15:59:5547
Tibor Goldschwendt19364ba2019-04-10 15:59:5548 <!-- dist:onDemand="true" makes this a separately installed module.
49 dist:onDemand="false" would always install the module alongside the
50 rest of Chrome. -->
51 <dist:module
52 dist:onDemand="true"
53 dist:title="@string/foo_module_title">
Ben Masone571ea5a2019-09-06 18:29:3754 <!-- This will fuse the module into the base APK if a system image
55 APK is built from this bundle. -->
56 <dist:fusing dist:include="true" />
Tibor Goldschwendt19364ba2019-04-10 15:59:5557 </dist:module>
58
Samuel Huang39c7db632019-05-15 14:57:1859 <!-- Remove android:hasCode="false" when adding Java code. -->
60 <application android:hasCode="false" />
Tibor Goldschwendt19364ba2019-04-10 15:59:5561</manifest>
62```
63
Tibor Goldschwendtaef8e392019-07-19 16:39:1064Next, create a descriptor configuring the Foo module. To do this, create
65`//chrome/android/features/foo/foo_module.gni` and add the following:
Tibor Goldschwendt19364ba2019-04-10 15:59:5566
67```gn
Tibor Goldschwendtaef8e392019-07-19 16:39:1068foo_module_desc = {
69 name = "foo"
Tibor Goldschwendt68c5f722019-08-01 15:10:1570 android_manifest =
71 "//chrome/android/features/foo/internal/java/AndroidManifest.xml"
Tibor Goldschwendt19364ba2019-04-10 15:59:5572}
73```
74
Tibor Goldschwendtaef8e392019-07-19 16:39:1075Then, add the module descriptor to the appropriate descriptor list in
76//chrome/android/modules/chrome_feature_modules.gni, e.g. the Chrome Modern
77list:
Tibor Goldschwendt19364ba2019-04-10 15:59:5578
79```gn
Tibor Goldschwendtaef8e392019-07-19 16:39:1080import("//chrome/android/features/foo/foo_module.gni")
Tibor Goldschwendt19364ba2019-04-10 15:59:5581...
Tibor Goldschwendtaef8e392019-07-19 16:39:1082chrome_modern_module_descs += [ foo_module_desc ]
Tibor Goldschwendt19364ba2019-04-10 15:59:5583```
84
85The next step is to add Foo to the list of feature modules for UMA recording.
86For this, add `foo` to the `AndroidFeatureModuleName` in
87`//tools/metrics/histograms/histograms.xml`:
88
89```xml
90<histogram_suffixes name="AndroidFeatureModuleName" ...>
91 ...
92 <suffix name="foo" label="Super Duper Foo Module" />
93 ...
94</histogram_suffixes>
95```
96
Tibor Goldschwendtf430b272019-11-25 19:19:4197See [below](#metrics) for what metrics will be automatically collected after
98this step.
99
Tibor Goldschwendt19364ba2019-04-10 15:59:55100<!--- TODO(tiborg): Add info about install UI. -->
101Lastly, give your module a title that Chrome and Play can use for the install
102UI. To do this, add a string to
103`//chrome/android/java/strings/android_chrome_strings.grd`:
104
105```xml
106...
107<message name="IDS_FOO_MODULE_TITLE"
108 desc="Text shown when the Foo module is referenced in install start, success,
109 failure UI (e.g. in IDS_MODULE_INSTALL_START_TEXT, which will expand to
110 'Installing Foo for Chrome…').">
111 Foo
112</message>
113...
114```
115
Samuel Huang7f2b53752019-05-23 15:10:05116*** note
117**Note:** This is for module title only. Other strings specific to the module
118should go in the module, not here (in the base module).
119***
120
Tibor Goldschwendt19364ba2019-04-10 15:59:55121Congrats! You added the DFM Foo to Monochrome. That is a big step but not very
122useful so far. In the next sections you'll learn how to add code and resources
123to it.
124
125
126### Building and installing modules
127
128Before we are going to jump into adding content to Foo, let's take a look on how
129to build and deploy the Monochrome bundle with the Foo DFM. The remainder of
130this guide assumes the environment variable `OUTDIR` is set to a properly
131configured GN build directory (e.g. `out/Debug`).
132
133To build and install the Monochrome bundle to your connected device, run:
134
135```shell
136$ autoninja -C $OUTDIR monochrome_public_bundle
137$ $OUTDIR/bin/monochrome_public_bundle install -m base -m foo
138```
139
140This will install Foo alongside the rest of Chrome. The rest of Chrome is called
Tibor Goldschwendtf430b272019-11-25 19:19:41141_base_ module in the bundle world. The base module will always be put on the
Tibor Goldschwendt19364ba2019-04-10 15:59:55142device when initially installing Chrome.
143
144*** note
Tibor Goldschwendtf430b272019-11-25 19:19:41145**Note:** The install script may install more modules than you specify, e.g.
146when there are default or conditionally installed modules (see
147[below](#conditional-install) for details).
Tibor Goldschwendt19364ba2019-04-10 15:59:55148***
149
150You can then check that the install worked with:
151
152```shell
153$ adb shell dumpsys package org.chromium.chrome | grep splits
154> splits=[base, config.en, foo]
155```
156
157Then try installing the Monochrome bundle without your module and print the
158installed modules:
159
160```shell
161$ $OUTDIR/bin/monochrome_public_bundle install -m base
162$ adb shell dumpsys package org.chromium.chrome | grep splits
163> splits=[base, config.en]
164```
165
166
167### Adding java code
168
169To make Foo useful, let's add some Java code to it. This section will walk you
170through the required steps.
171
Tibor Goldschwendt573cf3022019-05-10 17:23:30172First, define a module interface for Foo. This is accomplished by adding the
173`@ModuleInterface` annotation to the Foo interface. This annotation
174automatically creates a `FooModule` class that can be used later to install and
175access the module. To do this, add the following in the new file
Tibor Goldschwendt19364ba2019-04-10 15:59:55176`//chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/Foo.java`:
177
178```java
179package org.chromium.chrome.features.foo;
180
Fred Mello2623e052019-10-02 20:18:04181import org.chromium.components.module_installer.builder.ModuleInterface;
Tibor Goldschwendt573cf3022019-05-10 17:23:30182
Tibor Goldschwendt19364ba2019-04-10 15:59:55183/** Interface to call into Foo feature. */
Tibor Goldschwendt573cf3022019-05-10 17:23:30184@ModuleInterface(module = "foo", impl = "org.chromium.chrome.features.FooImpl")
Tibor Goldschwendt19364ba2019-04-10 15:59:55185public interface Foo {
186 /** Magical function. */
187 void bar();
188}
189```
190
191*** note
192**Note:** To reflect the separation from "Chrome browser" code, features should
193be defined in their own package name, distinct from the chrome package - i.e.
194`org.chromium.chrome.features.<feature-name>`.
195***
196
197Next, define an implementation that goes into the module in the new file
Tibor Goldschwendt68c5f722019-08-01 15:10:15198`//chrome/android/features/foo/internal/java/src/org/chromium/chrome/features/foo/FooImpl.java`:
Tibor Goldschwendt19364ba2019-04-10 15:59:55199
200```java
201package org.chromium.chrome.features.foo;
202
203import org.chromium.base.Log;
Tibor Goldschwendt573cf3022019-05-10 17:23:30204import org.chromium.base.annotations.UsedByReflection;
Tibor Goldschwendt19364ba2019-04-10 15:59:55205
Tibor Goldschwendt573cf3022019-05-10 17:23:30206@UsedByReflection("FooModule")
Tibor Goldschwendt19364ba2019-04-10 15:59:55207public class FooImpl implements Foo {
208 @Override
209 public void bar() {
210 Log.i("FOO", "bar in module");
211 }
212}
213```
214
Tibor Goldschwendt19364ba2019-04-10 15:59:55215You can then use this provider to access the module if it is installed. To test
216that, instantiate Foo and call `bar()` somewhere in Chrome:
217
218```java
Tibor Goldschwendt573cf3022019-05-10 17:23:30219if (FooModule.isInstalled()) {
220 FooModule.getImpl().bar();
Tibor Goldschwendt19364ba2019-04-10 15:59:55221} else {
222 Log.i("FOO", "module not installed");
223}
224```
225
Tibor Goldschwendt573cf3022019-05-10 17:23:30226The interface has to be available regardless of whether the Foo DFM is present.
227Therefore, put those classes into the base module. For this create a list of
228those Java files in
Tibor Goldschwendt19364ba2019-04-10 15:59:55229`//chrome/android/features/foo/public/foo_public_java_sources.gni`:
230
231```gn
232foo_public_java_sources = [
233 "//chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/Foo.java",
Tibor Goldschwendt19364ba2019-04-10 15:59:55234]
235```
236
237Then add this list to `chrome_java in //chrome/android/BUILD.gn`:
238
239```gn
240...
Tibor Goldschwendt68c5f722019-08-01 15:10:15241import("//chrome/android/features/foo/public/foo_public_java_sources.gni")
Tibor Goldschwendt19364ba2019-04-10 15:59:55242...
243android_library("chrome_java") {
244 ...
245 java_files += foo_public_java_sources
246}
247...
248```
249
250The actual implementation, however, should go into the Foo DFM. For this
Tibor Goldschwendt68c5f722019-08-01 15:10:15251purpose, create a new file `//chrome/android/features/foo/internal/BUILD.gn` and
252make a library with the module Java code in it:
Tibor Goldschwendt19364ba2019-04-10 15:59:55253
254```gn
255import("//build/config/android/rules.gni")
256
257android_library("java") {
258 # Define like ordinary Java Android library.
259 java_files = [
260 "java/src/org/chromium/chrome/features/foo/FooImpl.java",
261 # Add other Java classes that should go into the Foo DFM here.
262 ]
263 # Put other Chrome libs into the classpath so that you can call into the rest
264 # of Chrome from the Foo DFM.
Fred Mellob32b3022019-06-21 18:10:11265 deps = [
Tibor Goldschwendt19364ba2019-04-10 15:59:55266 "//base:base_java",
267 "//chrome/android:chrome_java",
268 # etc.
269 # Also, you'll need to depend on any //third_party or //components code you
270 # are using in the module code.
271 ]
272}
273```
274
Tibor Goldschwendtaef8e392019-07-19 16:39:10275Then, add this new library as a dependency of the Foo module descriptor in
276`//chrome/android/features/foo/foo_module.gni`:
Tibor Goldschwendt19364ba2019-04-10 15:59:55277
278```gn
Tibor Goldschwendtaef8e392019-07-19 16:39:10279foo_module_desc = {
Tibor Goldschwendt19364ba2019-04-10 15:59:55280 ...
Tibor Goldschwendtaef8e392019-07-19 16:39:10281 java_deps = [
Tibor Goldschwendt68c5f722019-08-01 15:10:15282 "//chrome/android/features/foo/internal:java",
Tibor Goldschwendt19364ba2019-04-10 15:59:55283 ]
284}
285```
286
287Finally, tell Android that your module is now containing code. Do that by
Samuel Huang39c7db632019-05-15 14:57:18288removing the `android:hasCode="false"` attribute from the `<application>` tag in
Tibor Goldschwendt68c5f722019-08-01 15:10:15289`//chrome/android/features/foo/internal/java/AndroidManifest.xml`. You should be
290left with an empty tag like so:
Tibor Goldschwendt19364ba2019-04-10 15:59:55291
292```xml
293...
294 <application />
295...
296```
297
298Rebuild and install `monochrome_public_bundle`. Start Chrome and run through a
299flow that tries to executes `bar()`. Depending on whether you installed your
300module (`-m foo`) "`bar in module`" or "`module not installed`" is printed to
301logcat. Yay!
302
Christopher Grant8fea5a12019-07-31 19:12:31303### Adding third-party native code
Tibor Goldschwendt19364ba2019-04-10 15:59:55304
Christopher Grant8fea5a12019-07-31 19:12:31305You can add a third-party native library (or any standalone library that doesn't
306depend on Chrome code) by adding it as a loadable module to the module descriptor in
307`//chrome/android/features/foo/foo_module.gni`:
Tibor Goldschwendt19364ba2019-04-10 15:59:55308
309```gn
Tibor Goldschwendtaef8e392019-07-19 16:39:10310foo_module_desc = {
Tibor Goldschwendt19364ba2019-04-10 15:59:55311 ...
Tibor Goldschwendtaef8e392019-07-19 16:39:10312 loadable_modules_32_bit = [ "//path/to/32/bit/lib.so" ]
313 loadable_modules_64_bit = [ "//path/to/64/bit/lib.so" ]
Tibor Goldschwendt19364ba2019-04-10 15:59:55314}
315```
316
Christopher Grant8fea5a12019-07-31 19:12:31317### Adding Chrome native code
Tibor Goldschwendt19364ba2019-04-10 15:59:55318
Christopher Grant8fea5a12019-07-31 19:12:31319Chrome native code may be placed in a DFM.
320
321A linker-assisted partitioning system automates the placement of code into
322either the main Chrome library or feature-specific .so libraries. Feature code
323may continue to make use of core Chrome code (eg. base::) without modification,
324but Chrome must call feature code through a virtual interface.
325
326Partitioning is explained in [Android Native
327Libraries](android_native_libraries.md#partitioned-libraries).
328
329#### Creating an interface to feature code
330
331One way of creating an interface to a feature library is through an interface
332definition. Feature Foo could define the following in
333`//chrome/android/features/foo/public/foo_interface.h`:
334
335```c++
336class FooInterface {
337 public:
338 virtual ~FooInterface() = default;
339
340 virtual void ProcessInput(const std::string& input) = 0;
341}
342```
343
344Alongside the interface definition, also in
345`//chrome/android/features/foo/public/foo_interface.h`, it's helpful to define a
346factory function type that can be used to create a Foo instance:
347
348```c++
349typedef FooInterface* CreateFooFunction(bool arg1, bool arg2);
350```
351
352<!--- TODO(cjgrant): Add a full, pastable Foo implementation. -->
353The feature library implements class Foo, hiding its implementation within the
354library. The library may then expose a single entrypoint, a Foo factory
355function. Here, C naming is (optionally) used so that the entrypoint symbol
356isn't mangled. In `//chrome/android/features/foo/internal/foo.cc`:
357
358```c++
359extern "C" {
360// This symbol is retrieved from the Foo feature module library via dlsym(),
361// where it's bare address is type-cast to its actual type and executed.
362// The forward declaration here ensures that CreateFoo()'s signature is correct.
363CreateFooFunction CreateFoo;
364
365__attribute__((visibility("default"))) FooInterface* CreateFoo(
366 bool arg1, bool arg2) {
367 return new Foo(arg1, arg2);
368}
369} // extern "C"
370```
371
372Ideally, the interface to the feature will avoid feature-specific types. If a
373feature defines complex data types, and uses them in its own interface, then its
374likely the main library will utilize the code backing these types. That code,
375and anything it references, will in turn be pulled back into the main library.
376
377Therefore, designing the feature inferface to use C types, C++ standard types,
378or classes that aren't expected to move out of Chrome's main library is ideal.
379If feature-specific classes are needed, they simply need to avoid referencing
380feature library internals.
381
382*** note
383**Note:** To help enforce separation between the feature interface and
384implementation, the interface class is best placed in its own GN target, on
385which the feature and main library code both depend.
386***
387
388#### Marking feature entrypoints
389
390Foo's feature module descriptor needs to pull in the appropriate native GN code
391dependencies, and also indicate the name of the file that lists the entrypoint
392symbols. In `//chrome/android/features/foo/foo_module.gni`:
393
394```gn
395foo_module_desc = {
396 ...
397 native_deps = [ "//chrome/android/features/foo/internal:foo" ]
398 native_entrypoints = "//chrome/android/features/foo/internal/module_entrypoints.lst"
399}
400```
401
402The module entrypoint file is a text file listing symbols. In this example,
403`//chrome/android/features/foo/internal/module_entrypoints.lst` has only a
404single factory function exposed:
405
406```shell
407# This file lists entrypoints exported from the Foo native feature library.
408
409CreateFoo
410```
411
412These symbols will be pulled into a version script for the linker, indicating
413that they should be exported in the dynamic symbol table of the feature library.
414
415*** note
416**Note:** If C++ symbol names are chosen as entrypoints, the full mangled names
417must be listed.
418***
419
420Additionally, it's necessary to map entrypoints to a particular partition. To
421follow compiler/linker convention, this is done at the compiler stage. A cflag
422is applied to source file(s) that may supply entrypoints (it's okay to apply the
423flag to all feature source - the attribute is utilized only on modules that
424export symbols). In `//chrome/android/features/foo/internal/BUILD.gn`:
425
426```gn
427static_library("foo") {
428 sources = [
429 ...
430 ]
431
432 # Mark symbols in this target as belonging to the Foo library partition. Only
433 # exported symbols (entrypoints) are affected, and only if this build supports
434 # native modules.
435 if (use_native_modules) {
436 cflags = [ "-fsymbol-partition=libfoo.so" ]
437 }
438}
439```
440
441Feature code is free to use any existing Chrome code (eg. logging, base::,
442skia::, cc::, etc), as well as other feature targets. From a GN build config
443perspective, the dependencies are defined as they normally would. The
444partitioning operation works independently of GN's dependency tree.
445
446```gn
447static_library("foo") {
448 ...
449
450 # It's fine to depend on base:: and other Chrome code.
451 deps = [
452 "//base",
453 "//cc/animation",
454 ...
455 ]
456
457 # Also fine to depend on other feature sub-targets.
458 deps += [
459 ":some_other_foo_target"
460 ]
461
462 # And fine to depend on the interface.
463 deps += [
464 ":foo_interface"
465 ]
466}
467```
468
469#### Opening the feature library
470
471Now, code in the main library can open the feature library and create an
472instance of feature Foo. Note that in this example, no care is taken to scope
473the lifetime of the opened library. Depending on the feature, it may be
474preferable to open and close the library as a feature is used.
475`//chrome/android/features/foo/factory/foo_factory.cc` may contain this:
476
477```c++
478std::unique_ptr<FooInterface> FooFactory(bool arg1, bool arg2) {
479 // Open the feature library, using the partition library helper to map it into
480 // the correct memory location. Specifying partition name *foo* will open
481 // libfoo.so.
482 void* foo_library_handle =
483 base::android::BundleUtils::DlOpenModuleLibraryPartition("foo");
484 }
485 DCHECK(foo_library_handle != nullptr) << "Could not open foo library:"
486 << dlerror();
487
488 // Pull the Foo factory function out of the library. The function name isn't
489 // mangled because it was extern "C".
490 CreateFooFunction* create_foo = reinterpret_cast<CreateFooFunction*>(
491 dlsym(foo_library_handle, "CreateFoo"));
492 DCHECK(create_foo != nullptr);
493
494 // Make and return a Foo!
495 return base::WrapUnique(create_foo(arg1, arg2));
496}
497
498```
499
500*** note
501**Note:** Component builds do not support partitioned libraries (code splitting
502happens across component boundaries instead). As such, an alternate, simplified
503feature factory implementation must be supplied (either by linking in a
504different factory source file, or using #defines in the factory) that simply
505instantiates a Foo object directly.
506***
507
508Finally, the main library is free to utilize Foo:
509
510```c++
511 auto foo = FooFactory::Create(arg1, arg2);
512 foo->ProcessInput(const std::string& input);
513```
514
Eric Stevenson8c9ab26b2019-08-30 15:44:40515#### JNI
516
517Read the `jni_generator` [docs](../base/android/jni_generator/README.md) before
518reading this section.
519
520There are some subtleties to how JNI registration works with DFMs:
521
522* Generated wrapper `ClassNameJni` classes are packaged into the DFM's dex file
523* The class containing the actual native definitions, `GEN_JNI.java`, is always
524 stored in the base module
525* If the DFM is only included in bundles that use
526 [implicit JNI registration](android_native_libraries.md#JNI-Native-Methods-Resolution)
527 (i.e. Monochrome and newer), then no extra consideration is necessary
528* Otherwise, the DFM will need to provide a `generate_jni_registration` target
529 that will generate all of the native registration functions
530
531
Christopher Grant8fea5a12019-07-31 19:12:31532### Adding Android resources
Tibor Goldschwendt19364ba2019-04-10 15:59:55533
534In this section we will add the required build targets to add Android resources
535to the Foo DFM.
536
Tibor Goldschwendt68c5f722019-08-01 15:10:15537First, add a resources target to
538`//chrome/android/features/foo/internal/BUILD.gn` and add it as a dependency on
539Foo's `java` target in the same file:
Tibor Goldschwendt19364ba2019-04-10 15:59:55540
541```gn
542...
543android_resources("java_resources") {
544 # Define like ordinary Android resources target.
545 ...
546 custom_package = "org.chromium.chrome.features.foo"
547}
548...
549android_library("java") {
550 ...
551 deps = [
552 ":java_resources",
553 ]
554}
555```
556
557To add strings follow steps
558[here](http://dev.chromium.org/developers/design-documents/ui-localization) to
559add new Java GRD file. Then create
Tibor Goldschwendt68c5f722019-08-01 15:10:15560`//chrome/android/features/foo/internal/java/strings/android_foo_strings.grd` as
561follows:
Tibor Goldschwendt19364ba2019-04-10 15:59:55562
563```xml
564<?xml version="1.0" encoding="UTF-8"?>
565<grit
566 current_release="1"
567 latest_public_release="0"
568 output_all_resource_defines="false">
569 <outputs>
570 <output
571 filename="values-am/android_foo_strings.xml"
572 lang="am"
573 type="android" />
574 <!-- List output file for all other supported languages. See
575 //chrome/android/java/strings/android_chrome_strings.grd for the full
576 list. -->
577 ...
578 </outputs>
579 <translations>
580 <file lang="am" path="vr_translations/android_foo_strings_am.xtb" />
581 <!-- Here, too, list XTB files for all other supported languages. -->
582 ...
583 </translations>
584 <release allow_pseudo="false" seq="1">
585 <messages fallback_to_english="true">
586 <message name="IDS_BAR_IMPL_TEXT" desc="Magical string.">
587 impl
588 </message>
589 </messages>
590 </release>
591</grit>
592```
593
594Then, create a new GRD target and add it as a dependency on `java_resources` in
Tibor Goldschwendt68c5f722019-08-01 15:10:15595`//chrome/android/features/foo/internal/BUILD.gn`:
Tibor Goldschwendt19364ba2019-04-10 15:59:55596
597```gn
598...
599java_strings_grd("java_strings_grd") {
600 defines = chrome_grit_defines
601 grd_file = "java/strings/android_foo_strings.grd"
602 outputs = [
603 "values-am/android_foo_strings.xml",
604 # Here, too, list output files for other supported languages.
605 ...
606 ]
607}
608...
609android_resources("java_resources") {
610 ...
611 deps = [":java_strings_grd"]
612 custom_package = "org.chromium.chrome.features.foo"
613}
614...
615```
616
617You can then access Foo's resources using the
618`org.chromium.chrome.features.foo.R` class. To do this change
Tibor Goldschwendt68c5f722019-08-01 15:10:15619`//chrome/android/features/foo/internal/java/src/org/chromium/chrome/features/foo/FooImpl.java`
Tibor Goldschwendt19364ba2019-04-10 15:59:55620to:
621
622```java
623package org.chromium.chrome.features.foo;
624
625import org.chromium.base.ContextUtils;
626import org.chromium.base.Log;
Tibor Goldschwendt573cf3022019-05-10 17:23:30627import org.chromium.base.annotations.UsedByReflection;
Tibor Goldschwendt19364ba2019-04-10 15:59:55628import org.chromium.chrome.features.foo.R;
629
Tibor Goldschwendt573cf3022019-05-10 17:23:30630@UsedByReflection("FooModule")
Tibor Goldschwendt19364ba2019-04-10 15:59:55631public class FooImpl implements Foo {
632 @Override
633 public void bar() {
634 Log.i("FOO", ContextUtils.getApplicationContext().getString(
635 R.string.bar_impl_text));
636 }
637}
638```
639
Tibor Goldschwendt19364ba2019-04-10 15:59:55640
641### Module install
642
643So far, we have installed the Foo DFM as a true split (`-m foo` option on the
644install script). In production, however, we have to explicitly install the Foo
Tibor Goldschwendt68c5f722019-08-01 15:10:15645DFM for users to get it. There are three install options: _on-demand_,
646_deferred_ and _conditional_.
Tibor Goldschwendt19364ba2019-04-10 15:59:55647
648
649#### On-demand install
650
651On-demand requesting a module will try to download and install the
652module as soon as possible regardless of whether the user is on a metered
653connection or whether they have turned updates off in the Play Store app.
654
Tibor Goldschwendt573cf3022019-05-10 17:23:30655You can use the autogenerated module class to on-demand install the module like
656so:
Tibor Goldschwendt19364ba2019-04-10 15:59:55657
658```java
Tibor Goldschwendt573cf3022019-05-10 17:23:30659FooModule.install((success) -> {
660 if (success) {
661 FooModule.getImpl().bar();
662 }
Tibor Goldschwendt19364ba2019-04-10 15:59:55663});
664```
665
666**Optionally**, you can show UI telling the user about the install flow. For
Tibor Goldschwendt573cf3022019-05-10 17:23:30667this, add a function like the one below. Note, it is possible
Tibor Goldschwendt19364ba2019-04-10 15:59:55668to only show either one of the install, failure and success UI or any
669combination of the three.
670
671```java
672public static void installModuleWithUi(
673 Tab tab, OnModuleInstallFinishedListener onFinishedListener) {
674 ModuleInstallUi ui =
675 new ModuleInstallUi(
676 tab,
677 R.string.foo_module_title,
678 new ModuleInstallUi.FailureUiListener() {
679 @Override
Samuel Huangfebcccd2019-08-21 20:48:47680 public void onFailureUiResponse(retry) {
681 if (retry) {
682 installModuleWithUi(tab, onFinishedListener);
683 } else {
684 onFinishedListener.onFinished(false);
685 }
Tibor Goldschwendt19364ba2019-04-10 15:59:55686 }
687 });
688 // At the time of writing, shows toast informing user about install start.
689 ui.showInstallStartUi();
Tibor Goldschwendt573cf3022019-05-10 17:23:30690 FooModule.install(
Tibor Goldschwendt19364ba2019-04-10 15:59:55691 (success) -> {
692 if (!success) {
693 // At the time of writing, shows infobar allowing user
694 // to retry install.
695 ui.showInstallFailureUi();
696 return;
697 }
698 // At the time of writing, shows toast informing user about
699 // install success.
700 ui.showInstallSuccessUi();
701 onFinishedListener.onFinished(true);
702 });
703}
704```
705
706To test on-demand install, "fake-install" the DFM. It's fake because
707the DFM is not installed as a true split. Instead it will be emulated by Chrome.
708Fake-install and launch Chrome with the following command:
709
710```shell
711$ $OUTDIR/bin/monochrome_public_bundle install -m base -f foo
Samuel Huang39c7db632019-05-15 14:57:18712$ $OUTDIR/bin/monochrome_public_bundle launch --args="--fake-feature-module-install"
Tibor Goldschwendt19364ba2019-04-10 15:59:55713```
714
715When running the install code, the Foo DFM module will be emulated.
716This will be the case in production right after installing the module. Emulation
717will last until Play Store has a chance to install your module as a true split.
Peter Wen577a6fe52019-12-11 22:02:05718This usually takes about a day. After it has been installed, it will be updated
719atomically alongside Chrome. Always check that it is installed and available
720before invoking code within the DFM.
Tibor Goldschwendt19364ba2019-04-10 15:59:55721
722*** note
723**Warning:** There are subtle differences between emulating a module and
724installing it as a true split. We therefore recommend that you always test both
725install methods.
726***
727
728
729#### Deferred install
730
731Deferred install means that the DFM is installed in the background when the
732device is on an unmetered connection and charging. The DFM will only be
733available after Chrome restarts. When deferred installing a module it will
734not be faked installed.
735
736To defer install Foo do the following:
737
738```java
Tibor Goldschwendt573cf3022019-05-10 17:23:30739FooModule.installDeferred();
Tibor Goldschwendt19364ba2019-04-10 15:59:55740```
741
Tibor Goldschwendt68c5f722019-08-01 15:10:15742#### Conditional install
743
744Conditional install means the DFM will be installed automatically upon first
745installing or updating Chrome if the device supports a particular feature.
746Conditional install is configured in the module's manifest. To install your
747module on all Daydream-ready devices for instance, your
748`//chrome/android/features/foo/internal/java/AndroidManifest.xml` should look
749like this:
750
751```xml
752<?xml version="1.0" encoding="utf-8"?>
753<manifest xmlns:android="http://schemas.android.com/apk/res/android"
754 xmlns:dist="http://schemas.android.com/apk/distribution"
755 featureSplit="foo">
756
757 <dist:module
758 dist:instant="false"
759 dist:title="@string/foo_module_title">
Ben Masone571ea5a2019-09-06 18:29:37760 <dist:fusing dist:include="true" />
Tibor Goldschwendt68c5f722019-08-01 15:10:15761 <dist:delivery>
762 <dist:install-time>
763 <dist:conditions>
764 <dist:device-feature
765 dist:name="android.hardware.vr.high_performance" />
766 </dist:conditions>
767 </dist:install-time>
768 <!-- Allows on-demand or deferred install on non-Daydream-ready
769 devices. -->
770 <dist:on-demand />
771 </dist:delivery>
772 </dist:module>
773
774 <application />
775</manifest>
776```
777
Tibor Goldschwendtf430b272019-11-25 19:19:41778### Metrics
779
780After adding your module to `AndroidFeatureModuleName` (see
781[above](#create-dfm-target)) we will collect, among others, the following
782metrics:
783
784* `Android.FeatureModules.AvailabilityStatus.Foo`: Measures your module's
785 install penetration. That is, the share of users who eventually installed
786 the module after requesting it (once or multiple times).
787
788* `Android.FeatureModules.InstallStatus.Foo`: The result of an on-demand
789 install request. Can be success or one of several error conditions.
790
791* `Android.FeatureModules.UncachedAwakeInstallDuration.Foo`: The duration to
792 install your module successfully after on-demand requesting it.
793
Tibor Goldschwendt19364ba2019-04-10 15:59:55794
795### Integration test APK and Android K support
796
797On Android K we still ship an APK. To make the Foo feature available on Android
798K add its code to the APK build. For this, add the `java` target to
799the `chrome_public_common_apk_or_module_tmpl` in
800`//chrome/android/chrome_public_apk_tmpl.gni` like so:
801
802```gn
803template("chrome_public_common_apk_or_module_tmpl") {
804 ...
805 target(_target_type, target_name) {
806 ...
807 if (_target_type != "android_app_bundle_module") {
808 deps += [
Tibor Goldschwendt68c5f722019-08-01 15:10:15809 "//chrome/android/features/foo/internal:java",
Tibor Goldschwendt19364ba2019-04-10 15:59:55810 ]
811 }
812 }
813}
814```
815
816This will also add Foo's Java to the integration test APK. You may also have to
817add `java` as a dependency of `chrome_test_java` if you want to call into Foo
818from test code.