Commit 94222d3c authored by Eric Stevenson's avatar Eric Stevenson Committed by Commit Bot

Android: Support APK Java targets using @NativeMethods.

Currently, trying to use proxy natives in an APK target will result in
multiple copies of GEN_JNI.class being added to the final jar since:
  * The jni_processor creates a GEN_JNI for the library target it
    runs on
  * The java_library_impl inside the APK target also adds a GEN_JNI
    to the output .jar because the final JNI step is a srcjar_dep

APK targets now support the annotation_processor_deps and
processor_args_javac args and must provide
"org.chromium.chrome.skipGenJni" to use proxy natives.

This CL also converts chromium_linker_test_apk__apk to use
annotation_processor_deps directly instead of the workaround added
in http://crrev.com/c/1758655.

Bug: 1001595
Change-Id: Ifb31b7c52327b2793540142eb013d0c39cb6435e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1842297Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Commit-Queue: Eric Stevenson <estevenson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706955}
parent 0a7e8c38
# Copyright 2019 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.
# Including this is an android_apk's processor_args_javac list will result in
# the JNI processor not writing a dummy GEN_JNI class. This is required when
# using proxy natives in android_apk targets.
skip_gen_jni_arg = "org.chromium.chrome.skipGenJni"
...@@ -36,6 +36,7 @@ import javax.annotation.Generated; ...@@ -36,6 +36,7 @@ import javax.annotation.Generated;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor; import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
...@@ -58,8 +59,10 @@ import javax.tools.Diagnostic; ...@@ -58,8 +59,10 @@ import javax.tools.Diagnostic;
* containing an interface annotated with NativeMethods. * containing an interface annotated with NativeMethods.
* *
*/ */
@SupportedOptions({JniProcessor.SKIP_GEN_JNI_ARG})
@AutoService(Processor.class) @AutoService(Processor.class)
public class JniProcessor extends AbstractProcessor { public class JniProcessor extends AbstractProcessor {
static final String SKIP_GEN_JNI_ARG = "org.chromium.chrome.skipGenJni";
private static final Class<NativeMethods> JNI_STATIC_NATIVES_CLASS = NativeMethods.class; private static final Class<NativeMethods> JNI_STATIC_NATIVES_CLASS = NativeMethods.class;
private static final Class<MainDex> MAIN_DEX_CLASS = MainDex.class; private static final Class<MainDex> MAIN_DEX_CLASS = MainDex.class;
...@@ -209,11 +212,14 @@ public class JniProcessor extends AbstractProcessor { ...@@ -209,11 +212,14 @@ public class JniProcessor extends AbstractProcessor {
try { try {
// Need to write NativeClass first because the wrapper classes // Need to write NativeClass first because the wrapper classes
// depend on it. // depend on it. This step is skipped for APK targets since the final GEN_JNI class is
JavaFile nativeClassFile = // provided elsewhere.
JavaFile.builder(mNativeClassPackage, mNativesBuilder.build()).build(); if (!processingEnv.getOptions().containsKey(SKIP_GEN_JNI_ARG)) {
JavaFile nativeClassFile =
JavaFile.builder(mNativeClassPackage, mNativesBuilder.build()).build();
nativeClassFile.writeTo(processingEnv.getFiler()); nativeClassFile.writeTo(processingEnv.getFiler());
}
for (JavaFile f : writeQueue) { for (JavaFile f : writeQueue) {
f.writeTo(processingEnv.getFiler()); f.writeTo(processingEnv.getFiler());
......
...@@ -2140,6 +2140,10 @@ if (enable_java_templates) { ...@@ -2140,6 +2140,10 @@ if (enable_java_templates) {
# LocaleConfig.java file will be generated for each package, and will # LocaleConfig.java file will be generated for each package, and will
# contain the list of compressed and uncompressed locale pak files. # contain the list of compressed and uncompressed locale pak files.
# disable_r8_outlining: Turn off outlining during the proguard step. # disable_r8_outlining: Turn off outlining during the proguard step.
# annotation_processor_deps: List of java_annotation_processor targets to
# use when compiling the java_files given to this target (optional).
# processor_args_javac: List of args to pass to annotation processors when
# compiling java_files given to this target (optional).
template("android_apk_or_module") { template("android_apk_or_module") {
forward_variables_from(invoker, [ "testonly" ]) forward_variables_from(invoker, [ "testonly" ])
assert(defined(invoker.android_manifest)) assert(defined(invoker.android_manifest))
...@@ -2773,10 +2777,10 @@ if (enable_java_templates) { ...@@ -2773,10 +2777,10 @@ if (enable_java_templates) {
"alternative_android_sdk_dep", "alternative_android_sdk_dep",
"android_manifest", "android_manifest",
"android_manifest_dep", "android_manifest_dep",
"annotation_processor_deps",
"apk_under_test", "apk_under_test",
"base_module_target", "base_module_target",
"chromium_code", "chromium_code",
"classpath_deps",
"jacoco_never_instrument", "jacoco_never_instrument",
"java_files", "java_files",
"javac_args", "javac_args",
...@@ -2784,6 +2788,7 @@ if (enable_java_templates) { ...@@ -2784,6 +2788,7 @@ if (enable_java_templates) {
"manifest_package", "manifest_package",
"native_lib_placeholders", "native_lib_placeholders",
"no_build_hooks", "no_build_hooks",
"processor_args_javac",
"secondary_abi_loadable_modules", "secondary_abi_loadable_modules",
"secondary_native_lib_placeholders", "secondary_native_lib_placeholders",
"static_library_dependent_targets", "static_library_dependent_targets",
...@@ -3365,6 +3370,7 @@ if (enable_java_templates) { ...@@ -3365,6 +3370,7 @@ if (enable_java_templates) {
"alternative_android_sdk_dep", "alternative_android_sdk_dep",
"android_manifest", "android_manifest",
"android_manifest_dep", "android_manifest_dep",
"annotation_processor_deps",
"apk_under_test", "apk_under_test",
"app_as_shared_lib", "app_as_shared_lib",
"build_hooks_android_impl_deps", "build_hooks_android_impl_deps",
...@@ -3407,6 +3413,7 @@ if (enable_java_templates) { ...@@ -3407,6 +3413,7 @@ if (enable_java_templates) {
"no_xml_namespaces", "no_xml_namespaces",
"png_to_webp", "png_to_webp",
"post_process_package_resources_script", "post_process_package_resources_script",
"processor_args_javac",
"product_version_resources_dep", "product_version_resources_dep",
"proguard_configs", "proguard_configs",
"proguard_enabled", "proguard_enabled",
...@@ -3497,6 +3504,7 @@ if (enable_java_templates) { ...@@ -3497,6 +3504,7 @@ if (enable_java_templates) {
"alternative_android_sdk_dep", "alternative_android_sdk_dep",
"android_manifest", "android_manifest",
"android_manifest_dep", "android_manifest_dep",
"annotation_processor_deps",
"app_as_shared_lib", "app_as_shared_lib",
"base_module_target", "base_module_target",
"bundle_target", "bundle_target",
...@@ -3529,6 +3537,7 @@ if (enable_java_templates) { ...@@ -3529,6 +3537,7 @@ if (enable_java_templates) {
"package_id", "package_id",
"package_name", "package_name",
"png_to_webp", "png_to_webp",
"processor_args_javac",
"product_version_resources_dep", "product_version_resources_dep",
"proguard_configs", "proguard_configs",
"proguard_enabled", "proguard_enabled",
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import("//base/android/jni_generator/config.gni")
import("//build/config/android/config.gni") import("//build/config/android/config.gni")
import("//build/config/android/rules.gni") import("//build/config/android/rules.gni")
import("//third_party/icu/config.gni") import("//third_party/icu/config.gni")
...@@ -266,8 +267,12 @@ if (current_cpu != "x64") { ...@@ -266,8 +267,12 @@ if (current_cpu != "x64") {
testonly = true testonly = true
deps = [ deps = [
":content_shell_assets", ":content_shell_assets",
":content_shell_java",
":linker_resources", ":linker_resources",
":linker_test_apk_all_java", "//base:base_java",
"//base:jni_java",
"//content/public/android:content_java",
"//ui/android:ui_java",
] ]
android_manifest = chromium_linker_test_manifest android_manifest = chromium_linker_test_manifest
android_manifest_dep = ":chromium_linker_test_manifest" android_manifest_dep = ":chromium_linker_test_manifest"
...@@ -276,24 +281,13 @@ if (current_cpu != "x64") { ...@@ -276,24 +281,13 @@ if (current_cpu != "x64") {
use_chromium_linker = true use_chromium_linker = true
enable_chromium_linker_tests = true enable_chromium_linker_tests = true
jni_registration_header = _linker_test_jni_registration_header jni_registration_header = _linker_test_jni_registration_header
}
android_library("linker_test_apk_all_java") {
testonly = true
deps = [
":content_shell_java",
":linker_resources",
"//base:base_java",
"//base:jni_java",
"//content/public/android:content_java",
"//ui/android:ui_java",
]
java_files = [ java_files = [
"linker_test_apk/src/org/chromium/chromium_linker_test_apk/ChromiumLinkerTestActivity.java", "linker_test_apk/src/org/chromium/chromium_linker_test_apk/ChromiumLinkerTestActivity.java",
"linker_test_apk/src/org/chromium/chromium_linker_test_apk/ChromiumLinkerTestApplication.java", "linker_test_apk/src/org/chromium/chromium_linker_test_apk/ChromiumLinkerTestApplication.java",
"linker_test_apk/src/org/chromium/chromium_linker_test_apk/LinkerTests.java", "linker_test_apk/src/org/chromium/chromium_linker_test_apk/LinkerTests.java",
] ]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ] annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
processor_args_javac = [ skip_gen_jni_arg ]
} }
test_runner_script(_linker_test_apk_test_runner_target_name) { test_runner_script(_linker_test_apk_test_runner_target_name) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment