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

Commit 481f027d authored by Chris Warrington's avatar Chris Warrington
Browse files

AAPT2: Automatic Static Library Namespacing.

Introduces a link flag --auto-namespace-static-lib for use when linking
static libraries.

When linking a static library with compiled sources that have references
to resources in provided libraries without an explicit package name,
the flag enables automatic inference of the package.

If a resource is present in the package that is being compiled, that is
used, otherwise the reference is rewritten to the highest precedence
resource with matching name and type.

Test: m out/host/linux-x86/nativetest64/aapt2_tests/aapt2_tests && \
      $ANDROID_HOST_OUT/nativetest64/aapt2_tests/aapt2_tests
Test: m frameworks/base/tools/aapt2/integration-tests
Change-Id: I6c6017e054654d1f60782d0a428a7a2a47f8952b
parent f1a183b0
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -677,6 +677,10 @@ class CompileContext : public IAaptContext {
    return 0;
    return 0;
  }
  }


  bool IsAutoNamespace() override {
    return false;
  }

 private:
 private:
  DISALLOW_COPY_AND_ASSIGN(CompileContext);
  DISALLOW_COPY_AND_ASSIGN(CompileContext);


+4 −0
Original line number Original line Diff line number Diff line
@@ -310,6 +310,10 @@ class Context : public IAaptContext {
    return 0u;
    return 0u;
  }
  }


  bool IsAutoNamespace() override {
    return false;
  }

  bool verbose_ = false;
  bool verbose_ = false;
  std::string package_;
  std::string package_;


+4 −0
Original line number Original line Diff line number Diff line
@@ -64,6 +64,10 @@ class DiffContext : public IAaptContext {
    return 0;
    return 0;
  }
  }


  bool IsAutoNamespace() override {
    return false;
  }

 private:
 private:
  std::string empty_;
  std::string empty_;
  StdErrDiagnostics diagnostics_;
  StdErrDiagnostics diagnostics_;
+4 −0
Original line number Original line Diff line number Diff line
@@ -298,6 +298,10 @@ class DumpContext : public IAaptContext {
    return 0;
    return 0;
  }
  }


  bool IsAutoNamespace() override {
    return false;
  }

 private:
 private:
  StdErrDiagnostics diagnostics_;
  StdErrDiagnostics diagnostics_;
  bool verbose_ = false;
  bool verbose_ = false;
+24 −1
Original line number Original line Diff line number Diff line
@@ -111,6 +111,7 @@ struct LinkOptions {


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


  // AndroidManifest.xml massaging options.
  // AndroidManifest.xml massaging options.
  ManifestFixerOptions manifest_fixer_options;
  ManifestFixerOptions manifest_fixer_options;
@@ -193,6 +194,14 @@ class LinkContext : public IAaptContext {
    min_sdk_version_ = minSdk;
    min_sdk_version_ = minSdk;
  }
  }


  bool IsAutoNamespace() override {
    return auto_namespace_;
  }

  void SetAutoNamespace(bool val) {
    auto_namespace_ = val;
  }

 private:
 private:
  DISALLOW_COPY_AND_ASSIGN(LinkContext);
  DISALLOW_COPY_AND_ASSIGN(LinkContext);


@@ -204,6 +213,7 @@ class LinkContext : public IAaptContext {
  SymbolTable symbols_;
  SymbolTable symbols_;
  bool verbose_ = false;
  bool verbose_ = false;
  int min_sdk_version_ = 0;
  int min_sdk_version_ = 0;
  bool auto_namespace_ = false;
};
};


// A custom delegate that generates compatible pre-O IDs for use with feature splits.
// A custom delegate that generates compatible pre-O IDs for use with feature splits.
@@ -2112,6 +2122,10 @@ int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
          .OptionalSwitch("--no-static-lib-packages",
          .OptionalSwitch("--no-static-lib-packages",
                          "Merge all library resources under the app's package.",
                          "Merge all library resources under the app's package.",
                          &options.no_static_lib_packages)
                          &options.no_static_lib_packages)
          .OptionalSwitch("--auto-namespace-static-lib",
                          "Automatically namespace resource references when building a static\n"
                          "library.",
                          &options.auto_namespace_static_lib)
          .OptionalSwitch("--non-final-ids",
          .OptionalSwitch("--non-final-ids",
                          "Generates R.java without the final modifier. This is implied when\n"
                          "Generates R.java without the final modifier. This is implied when\n"
                          "--static-lib is specified.",
                          "--static-lib is specified.",
@@ -2221,6 +2235,15 @@ int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
    options.output_format = OutputFormat::kProto;
    options.output_format = OutputFormat::kProto;
  }
  }


  if (options.auto_namespace_static_lib) {
    if (!static_lib) {
      context.GetDiagnostics()->Error(
          DiagMessage() << "--auto-namespace-static-lib can only be used with --static-lib");
      return 1;
    }
    context.SetAutoNamespace(true);
  }

  if (package_id) {
  if (package_id) {
    if (context.GetPackageType() != PackageType::kApp) {
    if (context.GetPackageType() != PackageType::kApp) {
      context.GetDiagnostics()->Error(
      context.GetDiagnostics()->Error(
Loading