0
Files
src/docs/android_accessing_cpp_switches_in_java.md
Nate Fischer eb70fb4aca Docs: add best practices for java_cpp_strings
No change to logic, only docs. This updates java_cpp_strings docs:

 * The first paragraph incorrectly stated this uses comments to
   determine the Java class name. This corrects that statement.
 * Merges the two GN steps into one, explains it's a best practice to
   add this to an android_library (or create a new one if there is
   none), and recommends private visibility for :java_switches_srcjar.

Test: Upload to gerrit > open file > click "gitiles"
Change-Id: I4ae45da15f7db02a9d4c101de1a23c40f154b464
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2369652
Commit-Queue: Bo <boliu@chromium.org>
Auto-Submit: Nate Fischer <ntfschr@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800838}
2020-08-22 15:25:55 +00:00

3.0 KiB

Accessing C++ Switches In Java

[TOC]

Introduction

Accessing C++ switches in Java is implemented via a Python script which analyzes the C++ switches file and generates the corresponding Java class, based on a template file. The template file must be specified in the GN target.

Usage

  1. Create a template file (ex. FooSwitches.java.tmpl)

     // 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.
    
     package org.chromium.foo;
    
     // Be sure to escape any curly braces in your template by doubling as
     // follows.
     /**
      * Contains command line switches that are specific to the foo project.
      */
     public final class FooSwitches {{
    
     {NATIVE_STRINGS}
    
         // Prevents instantiation.
         private FooSwitches() {{}}
     }}
    
  2. Add a new build target and add it to the srcjar_deps of an android_library target:

    if (is_android) {
      import("//build/config/android/rules.gni")
    }
    
    if (is_android) {
      java_cpp_strings("java_switches_srcjar") {
        # External code should depend on ":foo_java" instead.
        visibility = [ ":*" ]
        sources = [
          "//base/android/foo_switches.cc",
        ]
        template = "//base/android/java_templates/FooSwitches.java.tmpl"
      }
    
      # If there's already an android_library target, you can add
      # java_switches_srcjar to that target's srcjar_deps. Otherwise, the best
      # practice is to create a new android_library just for this target.
      android_library("foo_java") {
        srcjar_deps = [ ":java_switches_srcjar" ]
      }
    }
    
  3. The generated file out/Default/gen/.../org/chromium/foo/FooSwitches.java would contain:

    // 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.
    
    package org.chromium.foo;
    
    /**
     * Contains command line switches that are specific to the foo project.
     */
    public final class FooSwitches {
    
        // ...snip...
    
        // This following string constants were inserted by
        //     java_cpp_strings.py
        // From
        //     ../../base/android/foo_switches.cc
        // Into
        //     ../../base/android/java_templates/FooSwitches.java.tmpl
    
        // Documentation for the C++ switch is copied here.
        public static final String SOME_SWITCH = "some-switch";
    
        // ...snip...
    
        // Prevents instantiation.
        private FooSwitches() {}
    }
    

Code