Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84febeac authored by Izabela Orlowska's avatar Izabela Orlowska
Browse files

AAPT2: Merge-only mode for building static libraries

Add a new option for skipping resource references validation when
building a static library. This pushes resource reference validation
to the final link step (creating an installable APK), but allows a lib
to be linked without having the static libs of its dependencies.

More context: go/autonamespace-transform

Test: MergeOnlyTest + manually verified
Bug: 128824820
Change-Id: I1f3e3b7715a5244b8633c85519d94334c76ff664
parent 438e9051
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -269,6 +269,7 @@ struct ResourceFileFlattenerOptions {
  bool keep_raw_values = false;
  bool do_not_compress_anything = false;
  bool update_proguard_spec = false;
  bool do_not_fail_on_missing_resources = false;
  OutputFormat output_format = OutputFormat::kApk;
  std::unordered_set<std::string> extensions_to_not_compress;
  Maybe<std::regex> regex_to_not_compress;
@@ -435,7 +436,7 @@ std::vector<std::unique_ptr<xml::XmlResource>> ResourceFileFlattener::LinkAndVer
  xml::StripAndroidStudioAttributes(doc->root.get());

  XmlReferenceLinker xml_linker;
  if (!xml_linker.Consume(context_, doc)) {
  if (!options_.do_not_fail_on_missing_resources && !xml_linker.Consume(context_, doc)) {
    return {};
  }

@@ -1579,6 +1580,7 @@ class Linker {
    file_flattener_options.update_proguard_spec =
        static_cast<bool>(options_.generate_proguard_rules_path);
    file_flattener_options.output_format = options_.output_format;
    file_flattener_options.do_not_fail_on_missing_resources = options_.merge_only;

    ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);

@@ -1807,7 +1809,7 @@ class Linker {
    }

    ReferenceLinker linker;
    if (!linker.Consume(context_, &final_table_)) {
    if (!options_.merge_only && !linker.Consume(context_, &final_table_)) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
      return 1;
    }
@@ -1959,7 +1961,7 @@ class Linker {
      manifest_xml->file.name.package = context_->GetCompilationPackage();

      XmlReferenceLinker manifest_linker;
      if (manifest_linker.Consume(context_, manifest_xml.get())) {
      if (options_.merge_only || manifest_linker.Consume(context_, manifest_xml.get())) {
        if (options_.generate_proguard_rules_path &&
            !proguard::CollectProguardRulesForManifest(manifest_xml.get(), &proguard_keep_set)) {
          error = true;
@@ -2093,6 +2095,12 @@ int LinkCommand::Action(const std::vector<std::string>& args) {
    return 1;
  }

  if (options_.merge_only && !static_lib_) {
    context.GetDiagnostics()->Error(
        DiagMessage() << "the --merge-only flag can be only used when building a static library");
    return 1;
  }

  // The default build type.
  context.SetPackageType(PackageType::kApp);
  context.SetPackageId(kAppPackageId);
+5 −0
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ struct LinkOptions {

  // Static lib options.
  bool no_static_lib_packages = false;
  bool merge_only = false;

  // AndroidManifest.xml massaging options.
  ManifestFixerOptions manifest_fixer_options;
@@ -285,6 +286,10 @@ class LinkCommand : public Command {
    AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
    AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
                    &trace_folder_);
    AddOptionalSwitch("--merge-only",
          "Only merge the resources, without verifying resource references. This flag\n"
          "should only be used together with the --static-lib flag.",
          &options_.merge_only);
  }

  int Action(const std::vector<std::string>& args) override;
+2 −0
Original line number Diff line number Diff line
LOCAL_PATH := $(call my-dir)
include $(call all-makefiles-under,$(LOCAL_PATH))
+29 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_USE_AAPT2 := true
LOCAL_AAPT_NAMESPACES := true
LOCAL_PACKAGE_NAME := AaptTestMergeOnly_App
LOCAL_SDK_VERSION := current
LOCAL_EXPORT_PACKAGE_RESOURCES := true
LOCAL_MODULE_TAGS := tests
LOCAL_STATIC_ANDROID_LIBRARIES := \
    AaptTestMergeOnly_LeafLib \
    AaptTestMergeOnly_LocalLib
include $(BUILD_PACKAGE)
 No newline at end of file
+23 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app">

    <application
        android:label="@*com.local.lib:string/lib_string"/>

</manifest>
 No newline at end of file
Loading