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

Commit 7e5236dc authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Restore Proguard behavior and add minimal flag

We previously changed AAPT2 to correctly only generate keep rules for
the constructors required to inflate the different views. This cause
projects that did not have keep rules for the other constructors that
were accessed through reflection to have runtime crashes. This change
adds a flag to the link stage (--proguard-minimal-keep-rules) that
allows AAPT2 to only keep the constructors required for layout
inflation. If the flag is not present, then AAPT2 will generate less
specific keep rules than keep all constructors.

Bug: 116201243
Test: aapt2_tests
Change-Id: I8bb5cdf8446518ab153ea988e1243ca9494258c7
parent d5180a5e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1260,7 +1260,7 @@ class Linker {
      return false;
    }

    proguard::WriteKeepSet(keep_set, &fout);
    proguard::WriteKeepSet(keep_set, &fout, options_.generate_minimal_proguard_rules);
    fout.Flush();

    if (fout.HadError()) {
+4 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ struct LinkOptions {
  Maybe<std::string> generate_proguard_rules_path;
  Maybe<std::string> generate_main_dex_proguard_rules_path;
  bool generate_conditional_proguard_rules = false;
  bool generate_minimal_proguard_rules = false;
  bool generate_non_final_ids = false;
  std::vector<std::string> javadoc_annotations;
  Maybe<std::string> private_symbols;
@@ -119,6 +120,9 @@ class LinkCommand : public Command {
    AddOptionalSwitch("--proguard-conditional-keep-rules",
        "Generate conditional Proguard keep rules.",
        &options_.generate_conditional_proguard_rules);
    AddOptionalSwitch("--proguard-minimal-keep-rules",
        "Generate a minimal set of Proguard keep rules.",
        &options_.generate_minimal_proguard_rules);
    AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
        &options_.no_auto_version);
    AddOptionalSwitch("--no-version-vectors",
+9 −5
Original line number Diff line number Diff line
@@ -384,7 +384,7 @@ bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet
  return true;
}

void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) {
void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) {
  Printer printer(out);
  for (const auto& entry : keep_set.manifest_class_set_) {
    for (const UsageLocation& location : entry.second) {
@@ -406,15 +406,19 @@ void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) {
        printer.Print("-if class **.R$layout { int ")
            .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
            .Println("; }");
        printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(")
            .Print(entry.first.signature).Println("); }");

        printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
        printer.Print((minimal_keep) ? entry.first.signature : "...");
        printer.Println("); }");
      }
    } else {
      for (const UsageLocation& location : entry.second) {
        printer.Print("# Referenced at ").Println(location.source.to_string());
      }
      printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(")
          .Print(entry.first.signature).Println("); }");

      printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
      printer.Print((minimal_keep) ? entry.first.signature : "...");
      printer.Println("); }");
    }
    printer.Println();
  }
+2 −2
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ class KeepSet {
  }

 private:
  friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out);
  friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep);

  friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
                               std::set<UsageLocation>* locations);
@@ -89,7 +89,7 @@ bool CollectProguardRules(IAaptContext* context, xml::XmlResource* res, KeepSet*

bool CollectResourceReferences(IAaptContext* context, ResourceTable* table, KeepSet* keep_set);

void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out);
void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep);

bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
                      std::set<UsageLocation>* locations);
+72 −22
Original line number Diff line number Diff line
@@ -26,10 +26,10 @@ using ::testing::Not;

namespace aapt {

std::string GetKeepSetString(const proguard::KeepSet& set) {
std::string GetKeepSetString(const proguard::KeepSet& set, bool minimal_rules) {
  std::string out;
  StringOutputStream sout(&out);
  proguard::WriteKeepSet(set, &sout);
  proguard::WriteKeepSet(set, &sout, minimal_rules);
  sout.Flush();
  return out;
}
@@ -53,8 +53,17 @@ TEST(ProguardRulesTest, ManifestRuleDefaultConstructorOnly) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRulesForManifest(manifest.get(), &set, false));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { <init>(); }"));

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }"));
@@ -75,8 +84,10 @@ TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

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

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
}

@@ -89,8 +100,10 @@ TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

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

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
}

@@ -105,8 +118,11 @@ TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

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

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(); }"));
}
@@ -133,7 +149,12 @@ TEST(ProguardRulesTest, NavigationFragmentNameAndClassRulesAreEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), navigation.get(), &set));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));
@@ -150,8 +171,10 @@ TEST(ProguardRulesTest, CustomViewRulesAreEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

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

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr(
      "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}
@@ -188,8 +211,13 @@ TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) {
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), bar_layout.get(), &set));
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), foo_layout.get(), &set));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("int foo"));
  EXPECT_THAT(actual, HasSubstr("int bar"));

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
  EXPECT_THAT(actual, HasSubstr(
    "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
@@ -209,8 +237,14 @@ TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) {
  set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name);
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr(
      "-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
  EXPECT_THAT(actual, HasSubstr("int foo"));
  EXPECT_THAT(actual, HasSubstr("int bar"));

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr(
    "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
  EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
@@ -230,8 +264,11 @@ TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) {
  set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name);
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
  EXPECT_THAT(actual, Not(HasSubstr("-if")));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, Not(HasSubstr("-if")));
  EXPECT_THAT(actual, HasSubstr(
    "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
@@ -247,8 +284,11 @@ TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set,  /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr(
      "-keepclassmembers class * { *** bar_method(android.view.View); }"));

  actual = GetKeepSetString(set,  /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr(
    "-keepclassmembers class * { *** bar_method(android.view.View); }"));
}
@@ -267,8 +307,14 @@ TEST(ProguardRulesTest, MenuRulesAreEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set));

  std::string actual = GetKeepSetString(set);
  std::string actual = GetKeepSetString(set,  /** minimal_rules */ false);
  EXPECT_THAT(actual, HasSubstr(
    "-keepclassmembers class * { *** on_click(android.view.MenuItem); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
  EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));

  actual = GetKeepSetString(set,  /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr(
    "-keepclassmembers class * { *** on_click(android.view.MenuItem); }"));
  EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(android.content.Context); }"));
@@ -287,8 +333,10 @@ TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transition.get(), &set));

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

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr(
    "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}
@@ -304,8 +352,10 @@ TEST(ProguardRulesTest, TransitionRulesAreEmitted) {
  proguard::KeepSet set;
  ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transitionSet.get(), &set));

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

  actual = GetKeepSetString(set, /** minimal_rules */ true);
  EXPECT_THAT(actual, HasSubstr(
    "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}