Contents

Limitations

Currently (March 2019), DFMs have the following limitations:

WebView: We don't support DFMs for WebView. If your feature is used by WebView you cannot put it into a DFM. See crbug/949717 for progress.

Android K: DFMs are based on split APKs, a feature introduced in Android L. Therefore, we don't support DFMs on Android K. As a workaround you can add your feature to the Android K APK build. See crbug/881354 for progress.

Native Code: We cannot move native Chrome code into a DFM. See crbug/874564 for progress.

Getting started

This guide walks you through the steps to create a DFM called Foo and add it to the public Monochrome bundle. If you want to ship a DFM, you will also have to add it to the public Chrome Modern and Trichrome Chrome bundle as well as the downstream bundles.

Note: To make your own module you'll essentially have to replace every instance of foo/Foo/FOO with your_feature_name/YourFeatureName/ YOUR_FEATURE_NAME.

Create DFM target

DFMs are APKs. They have a manifest and can contain Java and native code as well as resources. This section walks you through creating the module target in our build system.

<manifestxmlns:android="http://schemas.android.com/apk/res/android"xmlns:dist="http://schemas.android.com/apk/distribution"featureSplit="foo"package="{{manifest_package}}"><!-- For Chrome Modern use android:minSdkVersion="21". --><uses-sdkandroid:minSdkVersion="24"android:targetSdkVersion="{{target_sdk_version}}"/><!-- dist:onDemand="true" makes this a separately installed module.
dist:onDemand="false" would always install the module alongside the
rest of Chrome. --><dist:moduledist:onDemand="true"dist:title="@string/foo_module_title"><!-- This will prevent the module to become part of the Android K
build in case we ever want to use bundles on Android K. --><dist:fusingdist:include="false"/></dist:module><!-- Remove hasCode="false" when adding Java code. --><applicationhasCode="false"/></manifest>

Then, add a package ID for Foo so that Foo's resources have unique identifiers. For this, add a new ID to //chrome/android/features/module_names_to_package_ids.gni:

resource_packages_id_mapping =[...,"foo=0x{XX}",# Set {XX} to next lower hex number.]

Next, create a template that contains the Foo module target.

Note: We put the module target into a template because we have to instantiate it for each Chrome bundle (Chrome Modern, Monochrome and Trichrome for both upstream and downstream) you want to ship your module in.

To do this, create //chrome/android/features/foo/foo_module_tmpl.gni and add the following:

Lastly, give your module a title that Chrome and Play can use for the install UI. To do this, add a string to //chrome/android/java/strings/android_chrome_strings.grd:

...
<messagename="IDS_FOO_MODULE_TITLE"desc="Text shown when the Foo module is referenced in install start, success,
failure UI (e.g. in IDS_MODULE_INSTALL_START_TEXT, which will expand to
'Installing Foo for Chrome…').">
Foo
</message>
...

Congrats! You added the DFM Foo to Monochrome. That is a big step but not very useful so far. In the next sections you'll learn how to add code and resources to it.

Building and installing modules

Before we are going to jump into adding content to Foo, let's take a look on how to build and deploy the Monochrome bundle with the Foo DFM. The remainder of this guide assumes the environment variable OUTDIR is set to a properly configured GN build directory (e.g. out/Debug).

To build and install the Monochrome bundle to your connected device, run:

This will install Foo alongside the rest of Chrome. The rest of Chrome is called base module in the bundle world. The Base module will always be put on the device when initially installing Chrome.

Note: You have to specify -m base here to make it explicit which modules will be installed. If you only specify -m foo the command will fail. It is also possible to specify no modules. In that case, the script will install the set of modules that the Play Store would install when first installing Chrome. That may be different than just specifying -m base if we have non-on-demand modules.

In order to get the Foo implementation depending on whether the Foo DFM is present, we will add a module provider class handling that logic. For this, create the file //chrome/android/features/foo/public/java/src/org/chromium/chrome/features/foo/FooModuleProvider.java and add:

You can then use this provider to access the module if it is installed. To test that, instantiate Foo and call bar() somewhere in Chrome:

if(FooModuleProvider.getFoo()!=null){FooModuleProvider.getFoo().bar();}else{Log.i("FOO","module not installed");}

The interface and module provider have to be available regardless of whether the Foo DFM is present. Therefore, put those classes into the base module. For this create a list of those Java files in //chrome/android/features/foo/public/foo_public_java_sources.gni:

The actual implementation, however, should go into the Foo DFM. For this purpose, create a new file //chrome/android/features/foo/BUILD.gn and make a library with the module Java code in it:

import("//build/config/android/rules.gni")
android_library("java"){# Define like ordinary Java Android library.
java_files =["java/src/org/chromium/chrome/features/foo/FooImpl.java",# Add other Java classes that should go into the Foo DFM here.]# Put other Chrome libs into the classpath so that you can call into the rest# of Chrome from the Foo DFM.
classpath_deps =["//base:base_java","//chrome/android:chrome_java",# etc.# Also, you'll need to depend on any //third_party or //components code you# are using in the module code.]}

Then, add this new library as a dependency of the Foo module target in //chrome/android/features/foo/foo_module_tmpl.gni:

Finally, tell Android that your module is now containing code. Do that by removing the hasCode="false" attribute from the <application> tag in //chrome/android/features/foo/java/AndroidManifest.xml. You should be left with an empty tag like so:

...
<application/>
...

Rebuild and install monochrome_public_bundle. Start Chrome and run through a flow that tries to executes bar(). Depending on whether you installed your module (-m foo) “bar in module” or “module not installed” is printed to logcat. Yay!

Adding native code

You can already add third party native code or native Chrome code that has no dependency on other Chrome code. To add such code add it as a loadable module to the bundle module target in //chrome/android/features/foo/foo_module_tmpl.gni:

Warning: While your module is emulated (see below) your resources are only available through ContextUtils.getApplicationContext(). Not through activities, etc. We therefore recommend that you only access DFM resources this way. See crbug/949729 for progress on making this more robust.

Module install

So far, we have installed the Foo DFM as a true split (-m foo option on the install script). In production, however, we have to explicitly install the Foo DFM for users to get it. There are two install options: on-demand and deferred.

On-demand install

On-demand requesting a module will try to download and install the module as soon as possible regardless of whether the user is on a metered connection or whether they have turned updates off in the Play Store app.

To request a module on-demand we can make use of the ModuleInstaller from //components/module_installer/. For this add, the following function to FooModuleProvider in //chrome/android/features/foo/public/java/src/org/chromium/chrome/foo/FooModuleProvider.java:

Then, use this new function to request the module and call bar() on install completion:

// Need to call init before accessing any modules. Can be called multiple times.ModuleInstaller.init();FooModuleProvider.installModule((success)->{FooModuleProvider.getFoo().bar();});

Optionally, you can show UI telling the user about the install flow. For this, add the function below to FooModuleProvider. Then use installModuleWithUi(...) instead of installModule(...). Note, it is possible to only show either one of the install, failure and success UI or any combination of the three.

To test on-demand install, “fake-install” the DFM. It's fake because the DFM is not installed as a true split. Instead it will be emulated by Chrome. Fake-install and launch Chrome with the following command:

When running the install code, the Foo DFM module will be emulated. This will be the case in production right after installing the module. Emulation will last until Play Store has a chance to install your module as a true split. This usually takes about a day.

Warning: There are subtle differences between emulating a module and installing it as a true split. We therefore recommend that you always test both install methods.

Deferred install

Deferred install means that the DFM is installed in the background when the device is on an unmetered connection and charging. The DFM will only be available after Chrome restarts. When deferred installing a module it will not be faked installed.

To defer install Foo do the following:

ModuleInstaller.installDeferred("foo");

Integration test APK and Android K support

On Android K we still ship an APK. To make the Foo feature available on Android K add its code to the APK build. For this, add the java target to the chrome_public_common_apk_or_module_tmpl in //chrome/android/chrome_public_apk_tmpl.gni like so: