blob: 2d46d6a7b0d1068de54673ac375debea94c91ba7 [file] [log] [blame] [view]
Nate Fischerac07b2622020-10-01 20:20:141# Accessing C++ Features In Java
2
3[TOC]
4
Henrique Nakashima1623df152023-05-31 18:23:355# Checking if a Feature is enabled
6
7In C++, add your `base::Feature` to an existing `base::android::FeatureMap` in the appropriate layer/component. Then, you can check the
8enabled 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`.
13if (FooFeatureMap.getInstance().isEnabled(FooFeatures.MY_FEATURE)) {
14 // ...
15}
16```
17
18If the components or layer does not have a FeatureMap, create a new one:
19
201. 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`.
262. 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()`.
323. 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 Fischerac07b2622020-10-01 20:20:1441
42Accessing C++ `base::Features` in Java is implemented via a Python script which
43analyzes the `*_features.cc` file and generates the corresponding Java class,
44based on a template file. The template file must be specified in the GN target.
45This outputs Java String constants which represent the name of the
46`base::Feature`.
47
48## Usage
49
501. 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 Drissman7b017a992022-09-07 15:50:3854 // Copyright 2020 The Chromium Authors
Nate Fischerac07b2622020-10-01 20:20:1455 // 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
742. 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 Wiszkowski2df94972024-04-29 23:09:051013. 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 Haseleyf89462b2022-08-02 23:37:45107
108 ```gn
109 android_library("common_java") {
110 ...
111
112 deps = [
113 ...
114 "//path/to:foo_java",
115 ...
116 ]
117 }
118 ```
119
1204. The generated file `out/Default/gen/.../org/chromium/foo/FooFeatures.java`
Nate Fischerac07b2622020-10-01 20:20:14121 would contain:
122
123 ```java
Avi Drissman7b017a992022-09-07 15:50:38124 // Copyright $YEAR The Chromium Authors
Nate Fischerac07b2622020-10-01 20:20:14125 // 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
156The script only supports limited syntaxes for declaring C++ base::Features. You
157may see an error like the following during compilation:
158
159```
160...
161org/chromium/foo/FooFeatures.java:41: error: duplicate declaration of field: MY_FEATURE
162 public static final String MY_FEATURE = "MyFeature";
163```
164
Henrique Nakashima1623df152023-05-31 18:23:35165This can happen if you've re-declared a feature for mutually-exclusive build
Nate Fischerac07b2622020-10-01 20:20:14166configs (ex. the feature is enabled-by-default for one config, but
167disabled-by-default for another). Example:
168
169```c++
170#if defined(...)
Daniel Chengdc644a12022-09-19 23:21:37171BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_ENABLED_BY_DEFAULT);
Nate Fischerac07b2622020-10-01 20:20:14172#else
Daniel Chengdc644a12022-09-19 23:21:37173BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_DISABLED_BY_DEFAULT);
Nate Fischerac07b2622020-10-01 20:20:14174#endif
175```
176
177The `java_cpp_features` rule doesn't know how to evaluate C++ preprocessor
178directives, so it generates two identical Java fields (which is what the
179compilation error is complaining about). Fortunately, the workaround is fairly
180simple. Rewrite the definition to only use directives around the enabled state:
181
182```c++
Daniel Chengdc644a12022-09-19 23:21:37183BASE_FEATURE(kMyFeature,
184 "MyFeature",
Nate Fischerac07b2622020-10-01 20:20:14185#if defined(...)
Daniel Chengdc644a12022-09-19 23:21:37186 base::FEATURE_ENABLED_BY_DEFAULT
Nate Fischerac07b2622020-10-01 20:20:14187#else
Daniel Chengdc644a12022-09-19 23:21:37188 base::FEATURE_DISABLED_BY_DEFAULT
Nate Fischerac07b2622020-10-01 20:20:14189#endif
190};
191
192```
193
Nate Fischerac07b2622020-10-01 20:20:14194
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)