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