1#!/usr/bin/env python3 2# 3# Copyright 2024 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import argparse 8import os 9import sys 10import tempfile 11 12from util import build_utils 13 14sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 15 16import zip_helpers 17 18 19def _RenameJars(options): 20 with tempfile.NamedTemporaryFile() as temp: 21 cmd = build_utils.JavaCmd() + [ 22 '-cp', 23 options.r8_path, 24 'com.android.tools.r8.relocator.RelocatorCommandLine', 25 '--input', 26 options.input_jar, 27 '--output', 28 temp.name, 29 ] 30 for mapping_rule in options.mapping_rules: 31 cmd += ['--map', mapping_rule] 32 33 build_utils.CheckOutput(cmd) 34 # use zip_helper.merge_zips to hermetize the zip because R8 changes the 35 # times and permissions inside the output jar for some reason. 36 zip_helpers.merge_zips(options.output_jar, [temp.name]) 37 38 39def main(): 40 parser = argparse.ArgumentParser() 41 parser.add_argument('--output-jar', 42 required=True, 43 help='Output file for the renamed classes jar') 44 parser.add_argument('--input-jar', 45 required=True, 46 help='Input jar file to rename classes in') 47 parser.add_argument('--r8-path', required=True, help='Path to R8 Jar') 48 parser.add_argument('--map', 49 action='append', 50 dest='mapping_rules', 51 help='List of mapping rules in the form of ' + 52 '"<original prefix>.**-><new prefix>"') 53 options = parser.parse_args() 54 55 _RenameJars(options) 56 57 58if __name__ == '__main__': 59 main() 60