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

Commit db19f510 authored by Donald Chai's avatar Donald Chai
Browse files

Generate "keep" rules for actionProviderClass and actionViewClass

Apps commonly use the versions of the attributes from AndroidX, which
will be unqualified.

Bug: 30888677
Change-Id: Id7cb32b28edb1cd1b8113a8c9a7ee29107a1b67e
Tested: aapt2_tests
parent 1cf41847
Loading
Loading
Loading
Loading
+13 −7
Original line number Diff line number Diff line
@@ -160,13 +160,19 @@ class MenuVisitor : public BaseVisitor {
  void Visit(xml::Element* node) override {
    if (node->namespace_uri.empty() && node->name == "item") {
      for (const auto& attr : node->attributes) {
        if (attr.namespace_uri == xml::kSchemaAndroid) {
        // AppCompat-v7 defines its own versions of Android attributes if
        // they're defined after SDK 7 (the below are from 11 and 14,
        // respectively), so don't bother checking the XML namespace.
        //
        // Given the names of the containing XML files and the attribute
        // names, it's unlikely that keeping these classes would be wrong.
        if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
            util::IsJavaClassName(attr.value)) {
          AddClass(node->line_number, attr.value, "android.content.Context");
          } else if (attr.name == "onClick") {
            AddMethod(node->line_number, attr.value, "android.view.MenuItem");
        }

        if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") {
          AddMethod(node->line_number, attr.value, "android.view.MenuItem");
        }
      }
    }
+19 −0
Original line number Diff line number Diff line
@@ -326,6 +326,25 @@ TEST(ProguardRulesTest, MenuRulesAreEmitted) {
  EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
}

TEST(ProguardRulesTest, MenuRulesAreEmittedForActionClasses) {
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"(
      <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/my_item"
            app:actionViewClass="com.foo.Bar"
            app:actionProviderClass="com.foo.Baz" />
      </menu>)");
  menu->file.name = test::ParseNameOrDie("menu/foo");

  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set));

  std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz"));
}

TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) {
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  std::unique_ptr<xml::XmlResource> transition = test::BuildXmlDom(R"(