From 181cbfde801b55adfc1b20f510dff02e5a9dcef1 Mon Sep 17 00:00:00 2001 From: Jean-Luc Coelho Date: Wed, 27 Nov 2019 12:37:48 -0800 Subject: [PATCH] Add "no-proguard-location-reference" flag to aapt2 This fixes relocatability issues with the code shrinkers. Bug: 144529940 Test: None Change-Id: I65e08e850193e67dcdf4c533d9e66332714f0c8c --- tools/aapt2/cmd/Link.cpp | 3 ++- tools/aapt2/cmd/Link.h | 4 ++++ tools/aapt2/java/ProguardRules.cpp | 26 +++++++++++++++++-------- tools/aapt2/java/ProguardRules.h | 6 ++++-- tools/aapt2/java/ProguardRules_test.cpp | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp index 99a686be1390a..4555caafb4788 100644 --- a/tools/aapt2/cmd/Link.cpp +++ b/tools/aapt2/cmd/Link.cpp @@ -1297,7 +1297,8 @@ class Linker { return false; } - proguard::WriteKeepSet(keep_set, &fout, options_.generate_minimal_proguard_rules); + proguard::WriteKeepSet(keep_set, &fout, options_.generate_minimal_proguard_rules, + options_.no_proguard_location_reference); fout.Flush(); if (fout.HadError()) { diff --git a/tools/aapt2/cmd/Link.h b/tools/aapt2/cmd/Link.h index aea24329c4313..4722358be8f73 100644 --- a/tools/aapt2/cmd/Link.h +++ b/tools/aapt2/cmd/Link.h @@ -56,6 +56,7 @@ struct LinkOptions { bool generate_conditional_proguard_rules = false; bool generate_minimal_proguard_rules = false; bool generate_non_final_ids = false; + bool no_proguard_location_reference = false; std::vector javadoc_annotations; Maybe private_symbols; @@ -215,6 +216,9 @@ class LinkCommand : public Command { "Generates R.java without the final modifier. This is implied when\n" "--static-lib is specified.", &options_.generate_non_final_ids); + AddOptionalSwitch("--no-proguard-location-reference", + "Keep proguard rules files from having a reference to the source file", + &options_.no_proguard_location_reference); AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.", &stable_id_file_path_); AddOptionalFlag("--emit-ids", diff --git a/tools/aapt2/java/ProguardRules.cpp b/tools/aapt2/java/ProguardRules.cpp index 806f4e37e22ac..b06607e41c073 100644 --- a/tools/aapt2/java/ProguardRules.cpp +++ b/tools/aapt2/java/ProguardRules.cpp @@ -393,11 +393,15 @@ bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet return true; } -void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) { +void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep, + bool no_location_reference) { + Printer printer(out); for (const auto& entry : keep_set.manifest_class_set_) { - for (const UsageLocation& location : entry.second) { - printer.Print("# Referenced at ").Println(location.source.to_string()); + if (!no_location_reference) { + for (const UsageLocation& location : entry.second) { + printer.Print("# Referenced at ").Println(location.source.to_string()); + } } printer.Print("-keep class ").Print(entry.first).Println(" { (); }"); } @@ -414,7 +418,9 @@ void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) if (can_be_conditional) { for (const UsageLocation& location : locations) { - printer.Print("# Referenced at ").Println(location.source.to_string()); + if (!no_location_reference) { + printer.Print("# Referenced at ").Println(location.source.to_string()); + } printer.Print("-if class **.R$layout { int ") .Print(JavaClassGenerator::TransformToFieldName(location.name.entry)) .Println("; }"); @@ -424,8 +430,10 @@ void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) printer.Println("); }"); } } else { - for (const UsageLocation& location : entry.second) { - printer.Print("# Referenced at ").Println(location.source.to_string()); + if (!no_location_reference) { + for (const UsageLocation& location : entry.second) { + printer.Print("# Referenced at ").Println(location.source.to_string()); + } } printer.Print("-keep class ").Print(entry.first.name).Print(" { ("); @@ -436,8 +444,10 @@ void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) } for (const auto& entry : keep_set.method_set_) { - for (const UsageLocation& location : entry.second) { - printer.Print("# Referenced at ").Println(location.source.to_string()); + if (!no_location_reference) { + for (const UsageLocation& location : entry.second) { + printer.Print("# Referenced at ").Println(location.source.to_string()); + } } printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name) .Print("(").Print(entry.first.signature).Println("); }"); diff --git a/tools/aapt2/java/ProguardRules.h b/tools/aapt2/java/ProguardRules.h index b15df59f56a60..a01b64d024d21 100644 --- a/tools/aapt2/java/ProguardRules.h +++ b/tools/aapt2/java/ProguardRules.h @@ -70,7 +70,8 @@ class KeepSet { } private: - friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep); + friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep, + bool no_location_reference); friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, std::set* locations); @@ -89,7 +90,8 @@ 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, bool minimal_keep); +void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep, + bool no_location_reference); bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, std::set* locations); diff --git a/tools/aapt2/java/ProguardRules_test.cpp b/tools/aapt2/java/ProguardRules_test.cpp index 25b55ab003b00..87205973566b9 100644 --- a/tools/aapt2/java/ProguardRules_test.cpp +++ b/tools/aapt2/java/ProguardRules_test.cpp @@ -30,7 +30,7 @@ namespace aapt { std::string GetKeepSetString(const proguard::KeepSet& set, bool minimal_rules) { std::string out; StringOutputStream sout(&out); - proguard::WriteKeepSet(set, &sout, minimal_rules); + proguard::WriteKeepSet(set, &sout, minimal_rules, false); sout.Flush(); return out; }