blob: 6ca74e9c74d0237cff6f3ceeb13f2f7de462c3d1 [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
Scott Haseleyf89462b2022-08-02 23:37:451013. Add a `deps` entry to `"common_java"` in `"//android_webview/BUILD.gn"` if
102 creating a new `android_library` in the previous step:
103
104 ```gn
105 android_library("common_java") {
106 ...
107
108 deps = [
109 ...
110 "//path/to:foo_java",
111 ...
112 ]
113 }
114 ```
115
1164. The generated file `out/Default/gen/.../org/chromium/foo/FooFeatures.java`
Nate Fischerac07b2622020-10-01 20:20:14117 would contain:
118
119 ```java
Avi Drissman7b017a992022-09-07 15:50:38120 // Copyright $YEAR The Chromium Authors
Nate Fischerac07b2622020-10-01 20:20:14121 // Use of this source code is governed by a BSD-style license that can be
122 // found in the LICENSE file.
123
124 package org.chromium.foo;
125
126 // Be sure to escape any curly braces in your template by doubling as
127 // follows.
128 /**
129 * Contains features that are specific to the foo project.
130 */
131 public final class FooFeatures {
132
133 // This following string constants were inserted by
134 // java_cpp_features.py
135 // From
136 // ../../base/android/foo_features.cc
137 // Into
138 // ../../base/android/java_templates/FooFeatures.java.tmpl
139
140 // Documentation for the C++ Feature is copied here.
141 public static final String SOME_FEATURE = "SomeFeature";
142
143 // ...snip...
144
145 // Prevents instantiation.
146 private FooFeatures() {}
147 }
148 ```
149
150### Troubleshooting
151
152The script only supports limited syntaxes for declaring C++ base::Features. You
153may see an error like the following during compilation:
154
155```
156...
157org/chromium/foo/FooFeatures.java:41: error: duplicate declaration of field: MY_FEATURE
158 public static final String MY_FEATURE = "MyFeature";
159```
160
Henrique Nakashima1623df152023-05-31 18:23:35161This can happen if you've re-declared a feature for mutually-exclusive build
Nate Fischerac07b2622020-10-01 20:20:14162configs (ex. the feature is enabled-by-default for one config, but
163disabled-by-default for another). Example:
164
165```c++
166#if defined(...)
Daniel Chengdc644a12022-09-19 23:21:37167BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_ENABLED_BY_DEFAULT);
Nate Fischerac07b2622020-10-01 20:20:14168#else
Daniel Chengdc644a12022-09-19 23:21:37169BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_DISABLED_BY_DEFAULT);
Nate Fischerac07b2622020-10-01 20:20:14170#endif
171```
172
173The `java_cpp_features` rule doesn't know how to evaluate C++ preprocessor
174directives, so it generates two identical Java fields (which is what the
175compilation error is complaining about). Fortunately, the workaround is fairly
176simple. Rewrite the definition to only use directives around the enabled state:
177
178```c++
Daniel Chengdc644a12022-09-19 23:21:37179BASE_FEATURE(kMyFeature,
180 "MyFeature",
Nate Fischerac07b2622020-10-01 20:20:14181#if defined(...)
Daniel Chengdc644a12022-09-19 23:21:37182 base::FEATURE_ENABLED_BY_DEFAULT
Nate Fischerac07b2622020-10-01 20:20:14183#else
Daniel Chengdc644a12022-09-19 23:21:37184 base::FEATURE_DISABLED_BY_DEFAULT
Nate Fischerac07b2622020-10-01 20:20:14185#endif
186};
187
188```
189
Nate Fischerac07b2622020-10-01 20:20:14190
191## See also
192* [Accessing C++ Enums In Java](android_accessing_cpp_enums_in_java.md)
193* [Accessing C++ Switches In Java](android_accessing_cpp_switches_in_java.md)
194
195## Code
196* [Generator code](/build/android/gyp/java_cpp_features.py) and
197 [Tests](/build/android/gyp/java_cpp_features_tests.py)
198* [GN template](/build/config/android/rules.gni)