Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 1 | # Accessing C++ Features In Java |
| 2 | |
| 3 | [TOC] |
| 4 | |
Henrique Nakashima | 1623df15 | 2023-05-31 18:23:35 | [diff] [blame] | 5 | # Checking if a Feature is enabled |
| 6 | |
| 7 | In C++, add your `base::Feature` to an existing `base::android::FeatureMap` in the appropriate layer/component. Then, you can check the |
| 8 | enabled state like so: |
| 9 | |
| 10 | ```java |
| 11 | // FooFeatureMap can check FooFeatures.MY_FEATURE as long as foo_feature_map.cc |
| 12 | // adds `kMyFeature` to its `base::android::FeatureMap`. |
| 13 | if (FooFeatureMap.getInstance().isEnabled(FooFeatures.MY_FEATURE)) { |
| 14 | // ... |
| 15 | } |
| 16 | ``` |
| 17 | |
| 18 | If the components or layer does not have a FeatureMap, create a new one: |
| 19 | |
| 20 | 1. In C++, create a new `foo_feature_map.cc` (ex. |
| 21 | [`content_feature_map`](/content/browser/android/content_feature_map.cc)) with: |
| 22 | * `kFeaturesExposedToJava` array with a pointer to your `base::Feature`. |
| 23 | * `GetFeatureMap` with a static `base::android::FeatureMap` initialized |
| 24 | with `kFeaturesExposedToJava`. |
| 25 | * `JNI_FooFeatureList_GetNativeMap` simply calling `GetFeatureMap`. |
| 26 | 2. In Java, create a `FooFeatureMap.java` class extending `FeatureMap.java` |
| 27 | (ex. [`ContentFeatureMap`](/content/public/android/java/src/org/chromium/content/browser/ContentFeatureMap.java)) with: |
| 28 | * A `getInstance()` that returns the singleton instance. |
| 29 | * A single `long getNativeMap()` as @NativeMethods. |
| 30 | * An `@Override` for the abstract `getNativeMap()` simply calling |
| 31 | `FooFeatureMapJni.get().getNativeMap()`. |
| 32 | 3. Still in Java, `FooFeatures.java` with the String constants with the feature |
| 33 | names needs to be generated or created. |
| 34 | * Auto-generate it by writing a `FooFeatures.java.tmpl`. [See instructions |
| 35 | below]((#generating-foo-feature-list-java)). |
| 36 | * If `FooFeatures` cannot be auto-generated, keep the list of String |
| 37 | constants with the feature names in a `FooFeatures` or `FooFeatureList` |
| 38 | separate from the pure boilerplate `FooFeatureMap`. |
| 39 | |
| 40 | # Auto-generating FooFeatureList.java {#generating-foo-feature-list-java} |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 41 | |
| 42 | Accessing C++ `base::Features` in Java is implemented via a Python script which |
| 43 | analyzes the `*_features.cc` file and generates the corresponding Java class, |
| 44 | based on a template file. The template file must be specified in the GN target. |
| 45 | This outputs Java String constants which represent the name of the |
| 46 | `base::Feature`. |
| 47 | |
| 48 | ## Usage |
| 49 | |
| 50 | 1. Create a template file (ex. `FooFeatures.java.tmpl`). Change "Copyright |
| 51 | 2020" to be whatever the year is at the time of writing (as you would for any |
| 52 | other file). |
| 53 | ```java |
Avi Drissman | 7b017a99 | 2022-09-07 15:50:38 | [diff] [blame] | 54 | // Copyright 2020 The Chromium Authors |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 55 | // Use of this source code is governed by a BSD-style license that can be |
| 56 | // found in the LICENSE file. |
| 57 | |
| 58 | package org.chromium.foo; |
| 59 | |
| 60 | // Be sure to escape any curly braces in your template by doubling as |
| 61 | // follows. |
| 62 | /** |
| 63 | * Contains features that are specific to the foo project. |
| 64 | */ |
| 65 | public final class FooFeatures {{ |
| 66 | |
| 67 | {NATIVE_FEATURES} |
| 68 | |
| 69 | // Prevents instantiation. |
| 70 | private FooFeatures() {{}} |
| 71 | }} |
| 72 | ``` |
| 73 | |
| 74 | 2. Add a new build target and add it to the `srcjar_deps` of an |
| 75 | `android_library` target: |
| 76 | |
| 77 | ```gn |
| 78 | if (is_android) { |
| 79 | import("//build/config/android/rules.gni") |
| 80 | } |
| 81 | |
| 82 | if (is_android) { |
| 83 | java_cpp_features("java_features_srcjar") { |
| 84 | # External code should depend on ":foo_java" instead. |
| 85 | visibility = [ ":*" ] |
| 86 | sources = [ |
| 87 | "//base/android/foo_features.cc", |
| 88 | ] |
| 89 | template = "//base/android/java_templates/FooFeatures.java.tmpl" |
| 90 | } |
| 91 | |
| 92 | # If there's already an android_library target, you can add |
| 93 | # java_features_srcjar to that target's srcjar_deps. Otherwise, the best |
| 94 | # practice is to create a new android_library just for this target. |
| 95 | android_library("foo_java") { |
| 96 | srcjar_deps = [ ":java_features_srcjar" ] |
| 97 | } |
| 98 | } |
| 99 | ``` |
| 100 | |
Tomasz Wiszkowski | 2df9497 | 2024-04-29 23:09:05 | [diff] [blame] | 101 | 3. If you need to expose your flag in WebView, and you created a new |
| 102 | `android_library` in the previous step, then add a `deps` entry to |
| 103 | `common_java` in `//android_webview/BUILD.gn`. |
| 104 | |
| 105 | If you don't need to expose a flag in WebView, then skip this and go to the |
| 106 | next step. |
Scott Haseley | f89462b | 2022-08-02 23:37:45 | [diff] [blame] | 107 | |
| 108 | ```gn |
| 109 | android_library("common_java") { |
| 110 | ... |
| 111 | |
| 112 | deps = [ |
| 113 | ... |
| 114 | "//path/to:foo_java", |
| 115 | ... |
| 116 | ] |
| 117 | } |
| 118 | ``` |
| 119 | |
| 120 | 4. The generated file `out/Default/gen/.../org/chromium/foo/FooFeatures.java` |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 121 | would contain: |
| 122 | |
| 123 | ```java |
Avi Drissman | 7b017a99 | 2022-09-07 15:50:38 | [diff] [blame] | 124 | // Copyright $YEAR The Chromium Authors |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 125 | // Use of this source code is governed by a BSD-style license that can be |
| 126 | // found in the LICENSE file. |
| 127 | |
| 128 | package org.chromium.foo; |
| 129 | |
| 130 | // Be sure to escape any curly braces in your template by doubling as |
| 131 | // follows. |
| 132 | /** |
| 133 | * Contains features that are specific to the foo project. |
| 134 | */ |
| 135 | public final class FooFeatures { |
| 136 | |
| 137 | // This following string constants were inserted by |
| 138 | // java_cpp_features.py |
| 139 | // From |
| 140 | // ../../base/android/foo_features.cc |
| 141 | // Into |
| 142 | // ../../base/android/java_templates/FooFeatures.java.tmpl |
| 143 | |
| 144 | // Documentation for the C++ Feature is copied here. |
| 145 | public static final String SOME_FEATURE = "SomeFeature"; |
| 146 | |
| 147 | // ...snip... |
| 148 | |
| 149 | // Prevents instantiation. |
| 150 | private FooFeatures() {} |
| 151 | } |
| 152 | ``` |
| 153 | |
| 154 | ### Troubleshooting |
| 155 | |
| 156 | The script only supports limited syntaxes for declaring C++ base::Features. You |
| 157 | may see an error like the following during compilation: |
| 158 | |
| 159 | ``` |
| 160 | ... |
| 161 | org/chromium/foo/FooFeatures.java:41: error: duplicate declaration of field: MY_FEATURE |
| 162 | public static final String MY_FEATURE = "MyFeature"; |
| 163 | ``` |
| 164 | |
Henrique Nakashima | 1623df15 | 2023-05-31 18:23:35 | [diff] [blame] | 165 | This can happen if you've re-declared a feature for mutually-exclusive build |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 166 | configs (ex. the feature is enabled-by-default for one config, but |
| 167 | disabled-by-default for another). Example: |
| 168 | |
| 169 | ```c++ |
| 170 | #if defined(...) |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 171 | BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_ENABLED_BY_DEFAULT); |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 172 | #else |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 173 | BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_DISABLED_BY_DEFAULT); |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 174 | #endif |
| 175 | ``` |
| 176 | |
| 177 | The `java_cpp_features` rule doesn't know how to evaluate C++ preprocessor |
| 178 | directives, so it generates two identical Java fields (which is what the |
| 179 | compilation error is complaining about). Fortunately, the workaround is fairly |
| 180 | simple. Rewrite the definition to only use directives around the enabled state: |
| 181 | |
| 182 | ```c++ |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 183 | BASE_FEATURE(kMyFeature, |
| 184 | "MyFeature", |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 185 | #if defined(...) |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 186 | base::FEATURE_ENABLED_BY_DEFAULT |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 187 | #else |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 188 | base::FEATURE_DISABLED_BY_DEFAULT |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 189 | #endif |
| 190 | }; |
| 191 | |
| 192 | ``` |
| 193 | |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 194 | |
| 195 | ## See also |
| 196 | * [Accessing C++ Enums In Java](android_accessing_cpp_enums_in_java.md) |
| 197 | * [Accessing C++ Switches In Java](android_accessing_cpp_switches_in_java.md) |
| 198 | |
| 199 | ## Code |
| 200 | * [Generator code](/build/android/gyp/java_cpp_features.py) and |
| 201 | [Tests](/build/android/gyp/java_cpp_features_tests.py) |
| 202 | * [GN template](/build/config/android/rules.gni) |