aar_import creates res/values/empty.xml if it contains no resources.
To do this, add a new tool that is used instead of zipper to get the resources out of the AAR. This tool creates res/values/empty.xml if there are no resources in the AAR.
Also, some general cleaning of the code.
RELNOTES: None
PiperOrigin-RevId: 166768607
diff --git a/tools/android/aar_resources_extractor.py b/tools/android/aar_resources_extractor.py
new file mode 100644
index 0000000..0e259b8
--- /dev/null
+++ b/tools/android/aar_resources_extractor.py
@@ -0,0 +1,58 @@
+# pylint: disable=g-direct-third-party-import
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""A tool for extracting resource files from an AAR.
+
+An AAR may contain resources under the /res directory. This tool extracts all
+of the resources into a directory. If no resources exist, it creates an
+empty.xml file that defines no resources.
+
+In the future, this script may be extended to also extract assets.
+"""
+
+import os
+import sys
+import zipfile
+
+from third_party.py import gflags
+
+FLAGS = gflags.FLAGS
+
+gflags.DEFINE_string("input_aar", None, "Input AAR")
+gflags.MarkFlagAsRequired("input_aar")
+gflags.DEFINE_string("output_res_dir", None, "Output resources directory")
+gflags.MarkFlagAsRequired("output_res_dir")
+
+
+def ExtractResources(aar, output_res_dir):
+ aar_contains_no_resources = True
+ for name in aar.namelist():
+ if name.startswith("res/"):
+ aar.extract(name, output_res_dir)
+ aar_contains_no_resources = False
+ if aar_contains_no_resources:
+ empty_xml_filename = output_res_dir + "/res/values/empty.xml"
+ os.makedirs(os.path.dirname(empty_xml_filename))
+ with open(empty_xml_filename, "wb") as empty_xml:
+ empty_xml.write("<resources/>")
+
+
+def main():
+ with zipfile.ZipFile(FLAGS.input_aar, "r") as aar:
+ ExtractResources(aar, FLAGS.output_res_dir)
+
+if __name__ == "__main__":
+ FLAGS(sys.argv)
+ main()