Commit 77bc3255 authored by Eric Stevenson's avatar Eric Stevenson Committed by Commit Bot

Android: Make BundleUtils bundle status work with Trichrome.

This is necessary for Trichrome synchronized ProGuard where we need to
support different values for Chrome and WebView (so we can't use the
same class).

Right now, BuildConfig.java isn't generated by TrichromeLibrary and so
the version from the main APK will be used.

Bug: 901465
Change-Id: I40a4d31b545bdb564a90babf4e49f3df0a597090
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1954934
Commit-Queue: Eric Stevenson <estevenson@chromium.org>
Auto-Submit: Eric Stevenson <estevenson@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#724341}
parent c1f71c3e
......@@ -30,6 +30,7 @@ import org.chromium.android_webview.AwProxyController;
import org.chromium.android_webview.AwServiceWorkerController;
import org.chromium.android_webview.AwTracingController;
import org.chromium.android_webview.HttpAuthDatabase;
import org.chromium.android_webview.ProductConfig;
import org.chromium.android_webview.R;
import org.chromium.android_webview.VariationsSeedLoader;
import org.chromium.android_webview.WebViewChromiumRunQueue;
......@@ -37,6 +38,7 @@ import org.chromium.android_webview.common.AwResource;
import org.chromium.android_webview.gfx.AwDrawFnImpl;
import org.chromium.base.BuildConfig;
import org.chromium.base.BuildInfo;
import org.chromium.base.BundleUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.FieldTrialList;
import org.chromium.base.JNIUtils;
......@@ -143,6 +145,8 @@ public class WebViewChromiumAwInit {
ResourceBundle.setAvailablePakLocales(
new String[] {}, AwLocaleConfig.getWebViewSupportedPakLocales());
BundleUtils.setIsBundle(ProductConfig.IS_BUNDLE);
// We are rewriting Java resources in the background.
// NOTE: Any reference to Java resources will cause a crash.
......
......@@ -63,7 +63,8 @@ std::string BundleUtils::ResolveLibraryPath(const std::string& library_name) {
// static
bool BundleUtils::IsBundle() {
return Java_BundleUtils_isBundle(base::android::AttachCurrentThread());
return Java_BundleUtils_isBundleForNative(
base::android::AttachCurrentThread());
}
// static
......
......@@ -8,12 +8,50 @@ import dalvik.system.BaseDexClassLoader;
import org.chromium.base.annotations.CalledByNative;
/** Utils to help working with android app bundles. */
public class BundleUtils {
/* Returns true if the current build is a bundle. */
/**
* Utils for working with android app bundles.
*
* Important notes about bundle status as interpreted by this class:
*
* <ul>
* <li>If {@link BuildConfig#BUNDLES_SUPPORTED} is false, then we are definitely not in a bundle,
* and ProGuard is able to strip out the bundle support library.</li>
* <li>If {@link BuildConfig#BUNDLES_SUPPORTED} is true, then we MIGHT be in a bundle.
* {@link BundleUtils#sIsBundle} is the source of truth.</li>
* </ul>
*
* We need two fields to store one bit of information here to ensure that ProGuard can optimize out
* the bundle support library (since {@link BuildConfig#BUNDLES_SUPPORTED} is final) and so that
* we can dynamically set whether or not we're in a bundle for targets that use static shared
* library APKs.
*/
public final class BundleUtils {
private static boolean sIsBundle;
/**
* {@link BundleUtils#isBundle()} is not called directly by native because
* {@link CalledByNative} prevents inlining, causing the bundle support lib to not be
* removed non-bundle builds.
*
* @return true if the current build is a bundle.
*/
@CalledByNative
public static boolean isBundleForNative() {
return isBundle();
}
/**
* @return true if the current build is a bundle.
*/
public static boolean isBundle() {
return BuildConfig.IS_BUNDLE;
if (!BuildConfig.BUNDLES_SUPPORTED) {
return false;
}
return sIsBundle;
}
public static void setIsBundle(boolean isBundle) {
sIsBundle = isBundle;
}
/* Returns absolute path to a native library in a feature module. */
......
......@@ -69,11 +69,10 @@ public class BuildConfig {
public static MAYBE_FINAL int MIN_SDK_VERSION = 1;
#endif
// Not making this final for BundleTestRule to modify.
#if defined(_IS_BUNDLE)
public static boolean IS_BUNDLE = true;
#if defined(_BUNDLES_SUPPORTED)
public static MAYBE_FINAL boolean BUNDLES_SUPPORTED = true;
#else
public static boolean IS_BUNDLE;
public static MAYBE_FINAL boolean BUNDLES_SUPPORTED MAYBE_FALSE;
#endif
#if defined(_IS_INCREMENTAL_INSTALL)
......
......@@ -8,7 +8,7 @@ import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.chromium.base.BuildConfig;
import org.chromium.base.BundleUtils;
/**
* Ensures that BundleUtils#isBundle returns true for the duration of the test.
......@@ -19,12 +19,12 @@ public class BundleTestRule implements TestRule {
return new Statement() {
@Override
public void evaluate() throws Throwable {
boolean oldValue = BuildConfig.IS_BUNDLE;
boolean oldValue = BundleUtils.isBundle();
try {
BuildConfig.IS_BUNDLE = true;
BundleUtils.setIsBundle(true);
base.evaluate();
} finally {
BuildConfig.IS_BUNDLE = oldValue;
BundleUtils.setIsBundle(oldValue);
}
}
};
......
......@@ -8,10 +8,12 @@ package PACKAGE;
#define MAYBE_FINAL final
#define MAYBE_USE_CHROMIUM_LINKER = USE_CHROMIUM_LINKER_VALUE
#define MAYBE_USE_MODERN_LINKER = USE_MODERN_LINKER_VALUE
#define MAYBE_IS_BUNDLE = IS_BUNDLE_VALUE
#else
#define MAYBE_FINAL
#define MAYBE_USE_CHROMIUM_LINKER
#define MAYBE_USE_MODERN_LINKER
#define MAYBE_IS_BUNDLE
#endif
/**
......@@ -37,4 +39,5 @@ public class ProductConfig {
public static MAYBE_FINAL boolean USE_CHROMIUM_LINKER MAYBE_USE_CHROMIUM_LINKER;
public static MAYBE_FINAL boolean USE_MODERN_LINKER MAYBE_USE_MODERN_LINKER;
public static MAYBE_FINAL boolean IS_BUNDLE MAYBE_IS_BUNDLE;
}
......@@ -1958,7 +1958,9 @@ if (enable_java_templates) {
# variables must not be set.
# enable_multidex: Value for ENABLE_MULTIDEX.
# min_sdk_version: Value for MIN_SDK_VERSION.
#
# bundles_supported: Whether or not this target can be treated as a bundle.
# resources_version_variable:
# is_incremental_install:
template("generate_build_config_srcjar") {
java_cpp_template(target_name) {
package_path = "org/chromium/base"
......@@ -1982,8 +1984,8 @@ if (enable_java_templates) {
defines += [ "_IS_CHROME_BRANDED" ]
}
if (defined(invoker.is_bundle_module) && invoker.is_bundle_module) {
defines += [ "_IS_BUNDLE" ]
if (defined(invoker.bundles_supported) && invoker.bundles_supported) {
defines += [ "_BUNDLES_SUPPORTED" ]
}
if (defined(invoker.is_incremental_install) &&
......@@ -2020,15 +2022,17 @@ if (enable_java_templates) {
#
# Variables:
# build_config: Path to build_config used for locale lists.
# is_bundle_module: Whether or not this target is part of a bundle build.
# java_package: Java package for the generated class.
# use_chromium_linker:
# use_modern_linker:
template("generate_product_config_srcjar") {
java_cpp_template(target_name) {
defines = []
_use_final = defined(invoker.build_config) ||
defined(invoker.use_chromium_linker) ||
defined(invoker.use_modern_linker)
_use_final =
defined(invoker.build_config) ||
defined(invoker.use_chromium_linker) ||
defined(invoker.use_modern_linker) || defined(invoker.is_bundle)
if (_use_final) {
defines += [ "USE_FINAL" ]
}
......@@ -2043,9 +2047,11 @@ if (enable_java_templates) {
defined(invoker.use_chromium_linker) && invoker.use_chromium_linker
_use_modern_linker =
defined(invoker.use_modern_linker) && invoker.use_modern_linker
_is_bundle = defined(invoker.is_bundle_module) && invoker.is_bundle_module
defines += [
"USE_CHROMIUM_LINKER_VALUE=$_use_chromium_linker",
"USE_MODERN_LINKER_VALUE=$_use_modern_linker",
"IS_BUNDLE_VALUE=$_is_bundle",
]
if (defined(invoker.build_config)) {
forward_variables_from(invoker,
......@@ -2743,11 +2749,9 @@ if (enable_java_templates) {
if (_generate_buildconfig_java) {
generate_build_config_srcjar("${_template_name}__build_config_srcjar") {
forward_variables_from(invoker,
[
"is_bundle_module",
"min_sdk_version",
])
forward_variables_from(invoker, [ "min_sdk_version" ])
bundles_supported = _is_bundle_module || _is_static_library_provider ||
(defined(testonly) && testonly)
use_final_fields = true
enable_multidex = _enable_multidex
is_incremental_install = _incremental_apk
......@@ -2767,6 +2771,7 @@ if (enable_java_templates) {
_locale_target_name =
"${_template_name}_${_package}__product_config_srcjar"
generate_product_config_srcjar("$_locale_target_name") {
forward_variables_from(invoker, [ "is_bundle_module" ])
build_config = _build_config
java_package = _package
use_chromium_linker = _use_chromium_linker
......
......@@ -16,6 +16,7 @@ import org.chromium.base.ApplicationState;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.BuildConfig;
import org.chromium.base.BuildInfo;
import org.chromium.base.BundleUtils;
import org.chromium.base.CommandLineInitUtil;
import org.chromium.base.ContextUtils;
import org.chromium.base.JNIUtils;
......@@ -146,6 +147,7 @@ public class ChromeApplication extends Application {
ProductConfig.COMPRESSED_LOCALES, ProductConfig.UNCOMPRESSED_LOCALES);
LibraryLoader.getInstance().setLinkerImplementation(
ProductConfig.USE_CHROMIUM_LINKER, ProductConfig.USE_MODERN_LINKER);
BundleUtils.setIsBundle(ProductConfig.IS_BUNDLE);
if (isBrowserProcess) {
TraceEvent.end("ChromeApplication.attachBaseContext");
......
......@@ -6,7 +6,7 @@ package org.chromium.components.module_installer.builder;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.BuildConfig;
import org.chromium.base.BundleUtils;
import org.chromium.base.StrictModeContext;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
......@@ -153,7 +153,7 @@ public class Module<T> {
* @return The module's {@link ModuleDescriptor}.
*/
private static ModuleDescriptor loadModuleDescriptor(String name) {
if (!BuildConfig.IS_BUNDLE) {
if (!BundleUtils.isBundle()) {
return new ModuleDescriptor() {
@Override
public String[] getLibraries() {
......
......@@ -4,7 +4,7 @@
package org.chromium.components.module_installer.engine;
import org.chromium.base.BuildConfig;
import org.chromium.base.BundleUtils;
import org.chromium.base.CommandLine;
/**
......@@ -12,7 +12,7 @@ import org.chromium.base.CommandLine;
*/
public class EngineFactory {
public InstallEngine getEngine() {
if (!BuildConfig.IS_BUNDLE) {
if (!BundleUtils.isBundle()) {
return new ApkEngine();
}
if (CommandLine.getInstance().hasSwitch("fake-feature-module-install")) {
......
......@@ -4,7 +4,7 @@
package org.chromium.components.module_installer.util;
import org.chromium.base.BuildConfig;
import org.chromium.base.BundleUtils;
import org.chromium.base.annotations.MainDex;
import org.chromium.components.module_installer.logger.SplitAvailabilityLogger;
......@@ -20,7 +20,7 @@ public class ModuleUtil {
* unnecessary code (modules are not supported in APKs).
*/
public static void recordStartupTime() {
if (!BuildConfig.IS_BUNDLE) return;
if (!BundleUtils.isBundle()) return;
Timer.recordStartupTime();
}
......@@ -29,7 +29,7 @@ public class ModuleUtil {
* Records the start time in order to later report the install duration via UMA.
*/
public static void recordModuleAvailability() {
if (!BuildConfig.IS_BUNDLE) return;
if (!BundleUtils.isBundle()) return;
try (Timer timer = new Timer()) {
initApplication();
......@@ -41,7 +41,7 @@ public class ModuleUtil {
* Updates the CrashKey report containing modules currently present.
*/
public static void updateCrashKeys() {
if (!BuildConfig.IS_BUNDLE) return;
if (!BundleUtils.isBundle()) return;
try (Timer timer = new Timer()) {
CrashKeyRecorder.updateCrashKeys();
......@@ -52,7 +52,7 @@ public class ModuleUtil {
* Initializes the PlayCore SplitCompat framework.
*/
public static void initApplication() {
if (!BuildConfig.IS_BUNDLE) return;
if (!BundleUtils.isBundle()) return;
try (Timer timer = new Timer()) {
SplitCompatInitializer.initApplication();
......
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