Accessing C++ Switches In Java


Accessing C++ switches in Java is implemented via a Python script which analyzes the C++ switches file and spits out the corresponding Java class. The generated class name will be based upon the switch file name, and the path must be specified in a comment within the switch file itself.


  1. Add directives to your C++ switch file

    // ...snip...
    // Documentation for the following switch.
    const char kSomeSwitch[] = "some-switch";
    // ...snip...
  2. Create a template file

     // Copyright {YEAR} The Chromium Authors. All rights reserved.
     // Use of this source code is governed by a BSD-style license that can be
     // found in the LICENSE file.
     // This file is autogenerated by
     //     {SCRIPT_NAME}
     // From
     //     {SOURCE_PATH}, and
     //     {TEMPLATE_PATH}
     // Be sure to escape any curly braces in your template by doubling as
     // follows.
     public abstract class MySwitches {{
  3. Add a new build target

    java_cpp_strings("foo_generated_switch") {
      sources = [
      template = "//base/android/java_templates/"
  4. Add the new target to the desired android_library targets srcjar_deps:

    android_library("base_java") {
      srcjar_deps = [
  5. The generated file org/chromium/chrome/ would contain:

    public final class NativeFooSwitches {
        // ...snip...
        public static final String SOME_SWITCH = "some-switch";
        // ...snip...