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

Commit cc65b8db authored by Jake Wharton's avatar Jake Wharton
Browse files

Only keep necessary constructor for custom view nodes

This expands the Context+AttributeSet constructor specificity from only work on <view class=> nodes to <my.Type> nodes.

Bug: 37123156
Test: make aapt2_tests
Change-Id: I8fb950731383f86bee225333bda27baf5a7a34c5
parent 98100c38
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -39,7 +39,11 @@ class BaseVisitor : public xml::Visitor {
 public:
  using xml::Visitor::Visit;

  BaseVisitor(const ResourceFile& file, KeepSet* keep_set) : file_(file), keep_set_(keep_set) {
  BaseVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set, "...") {
  }

  BaseVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& ctor_signature)
      : file_(file), keep_set_(keep_set), ctor_signature_(ctor_signature) {
  }

  void Visit(xml::Element* node) override {
@@ -50,11 +54,11 @@ class BaseVisitor : public xml::Visitor {
        // This is a custom view, let's figure out the class name from this.
        std::string package = maybe_package.value().package + "." + node->name;
        if (util::IsJavaClassName(package)) {
          AddClass(node->line_number, package, "...");
          AddClass(node->line_number, package, ctor_signature_);
        }
      }
    } else if (util::IsJavaClassName(node->name)) {
      AddClass(node->line_number, node->name, "...");
      AddClass(node->line_number, node->name, ctor_signature_);
    }

    for (const auto& child : node->children) {
@@ -74,6 +78,7 @@ class BaseVisitor : public xml::Visitor {
 protected:
  ResourceFile file_;
  KeepSet* keep_set_;
  std::string ctor_signature_;

  virtual void AddClass(size_t line_number, const std::string& class_name,
                        const std::string& ctor_signature) {
@@ -104,7 +109,8 @@ class BaseVisitor : public xml::Visitor {

class LayoutVisitor : public BaseVisitor {
 public:
  LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
  LayoutVisitor(const ResourceFile& file, KeepSet* keep_set)
      : BaseVisitor(file, keep_set, "android.content.Context, android.util.AttributeSet") {
  }

  void Visit(xml::Element* node) override {
+8 −4
Original line number Diff line number Diff line
@@ -152,7 +152,8 @@ TEST(ProguardRulesTest, CustomViewRulesAreEmitted) {

  std::string actual = GetKeepSetString(set);

  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr(
      "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}

TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) {
@@ -190,7 +191,8 @@ TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) {
  std::string actual = GetKeepSetString(set);

  EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr(
      "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
  EXPECT_THAT(actual, HasSubstr("int foo"));
  EXPECT_THAT(actual, HasSubstr("int bar"));
}
@@ -209,7 +211,8 @@ TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) {

  std::string actual = GetKeepSetString(set);

  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr(
      "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
  EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
  EXPECT_THAT(actual, HasSubstr("int foo"));
  EXPECT_THAT(actual, HasSubstr("int bar"));
@@ -230,7 +233,8 @@ TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) {
  std::string actual = GetKeepSetString(set);

  EXPECT_THAT(actual, Not(HasSubstr("-if")));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr(
      "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}

TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {