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
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index 13c1047..20ea3cb6 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -1260,7 +1260,7 @@
return false;
}
- proguard::WriteKeepSet(keep_set, &fout);
+ proguard::WriteKeepSet(keep_set, &fout, options_.generate_minimal_proguard_rules);
fout.Flush();
if (fout.HadError()) {
diff --git a/tools/aapt2/cmd/Link.h b/tools/aapt2/cmd/Link.h
index e58a93e..a42c0f2 100644
--- a/tools/aapt2/cmd/Link.h
+++ b/tools/aapt2/cmd/Link.h
@@ -49,6 +49,7 @@
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 @@
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",
diff --git a/tools/aapt2/java/ProguardRules.cpp b/tools/aapt2/java/ProguardRules.cpp
index d40795a..52e168e 100644
--- a/tools/aapt2/java/ProguardRules.cpp
+++ b/tools/aapt2/java/ProguardRules.cpp
@@ -384,7 +384,7 @@
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 @@
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();
}
diff --git a/tools/aapt2/java/ProguardRules.h b/tools/aapt2/java/ProguardRules.h
index 01dad0b..38b4860 100644
--- a/tools/aapt2/java/ProguardRules.h
+++ b/tools/aapt2/java/ProguardRules.h
@@ -70,7 +70,7 @@
}
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 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);
diff --git a/tools/aapt2/java/ProguardRules_test.cpp b/tools/aapt2/java/ProguardRules_test.cpp
index 83c72d8..3d93cb1 100644
--- a/tools/aapt2/java/ProguardRules_test.cpp
+++ b/tools/aapt2/java/ProguardRules_test.cpp
@@ -26,10 +26,10 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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,11 +211,16 @@
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); }"));
+ "-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,10 +237,16 @@
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>(android.content.Context, android.util.AttributeSet); }"));
+ "-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"));
EXPECT_THAT(actual, HasSubstr("int foo"));
EXPECT_THAT(actual, HasSubstr("int bar"));
@@ -230,11 +264,14 @@
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); }"));
+ "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}
TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
@@ -247,10 +284,13 @@
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); }"));
}
TEST(ProguardRulesTest, MenuRulesAreEmitted) {
@@ -267,10 +307,16 @@
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); }"));
+ "-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); }"));
EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(android.content.Context); }"));
EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
@@ -287,10 +333,12 @@
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); }"));
+ "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}
TEST(ProguardRulesTest, TransitionRulesAreEmitted) {
@@ -304,10 +352,12 @@
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); }"));
+ "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
}
} // namespace aapt