Fix AarResourcesExtractor action to produce consistent zips
The `aar_native_libs_zip_creator.py` script was producing inconsistent output zips for the same input files due to changing the last modified date of the input files. This resulted in inconsistent cache keys and therefore poor build results.
Closes #11970.
PiperOrigin-RevId: 345541072
diff --git a/tools/android/aar_native_libs_zip_creator.py b/tools/android/aar_native_libs_zip_creator.py
index 59c3d7b..5ff971f 100644
--- a/tools/android/aar_native_libs_zip_creator.py
+++ b/tools/android/aar_native_libs_zip_creator.py
@@ -63,7 +63,16 @@
# Only replaces the first instance of jni, in case the AAR contains
# something like /jni/x86/jni.so.
new_filename = lib.replace("jni", "lib", 1)
- native_libs_zip.writestr(new_filename, aar.read(lib))
+ # To guarantee reproducible zips we must specify a new zipinfo.
+ # From writestr docs: "If its a name, the date and time is set to the
+ # current date and time." which will break the HASH calculation and result
+ # in a cache miss.
+ old_zipinfo = aar.getinfo(lib)
+ new_zipinfo = zipfile.ZipInfo(filename=new_filename)
+ new_zipinfo.date_time = old_zipinfo.date_time
+ new_zipinfo.compress_type = old_zipinfo.compress_type
+
+ native_libs_zip.writestr(new_zipinfo, aar.read(lib))
def Main(input_aar_path, output_zip_path, cpu, input_aar_path_for_error_msg):