blob: 0926fe27a4156a48abac347bb93d0dd98099f0cf [file]
# Copyright (C) 2026 The Android Open Source Project
#
# 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.
import argparse
import pathlib
import os
import re
import subprocess
import sys
import xml.etree.ElementTree as ET
import bp
import restart
def initialize_cmd(subparsers):
plugin_parser = subparsers.add_parser(
"plugin",
help="Work with sysui plugins (build and install)",
formatter_class=argparse.RawTextHelpFormatter, # Allows newlines in help text.
)
plugin_parser.set_defaults(func=lambda args, logger: plugin(plugin_parser, args, logger))
plugin_subparser = plugin_parser.add_subparsers()
plugin_install_parser = plugin_subparser.add_parser(
"install",
help="""Build and installs a pod as a plugin. Example:
pods plugin install PluginName
"""
)
plugin_install_parser.set_defaults(func=plugin_install)
plugin_install_parser.add_argument(
"target",
help="The Soong target apk to build and install."
)
plugin_uninstall_parser = plugin_subparser.add_parser(
"uninstall",
help="""Uninstalls a pod as a plugin. Example:
pods plugin uninstall PluginName
"""
)
plugin_uninstall_parser.set_defaults(func=plugin_uninstall)
plugin_uninstall_parser.add_argument(
"target",
help="The Soong target apk to remove."
)
plugin_distribute_parser = plugin_subparser.add_parser(
"distribute",
help="Build a pod as a plugin apk and copy it to the specified location"
)
plugin_distribute_parser.set_defaults(func=plugin_distribute)
def plugin(parser, args, logger):
parser.print_help()
def plugin_distribute(args, logger):
print("plugin distribute")
def plugin_install(args, logger):
target = args.target
result = subprocess.run("m {target}".format(target=target), shell=True)
if result.returncode != 0:
sys.exit(result.returncode)
apk = _locate_apk(target)
if not apk:
print("Unable to find apk for {target}".format(target=target), file=sys.stderr)
sys.exit(-1)
print("Installing {apk}".format(apk=apk))
result = subprocess.run(
"adb install {top}{sep}{apk}".format(
apk=apk,
sep=os.sep,
top=os.getenv("ANDROID_BUILD_TOP",""),
),
shell=True
)
if result.returncode != 0:
sys.exit(result.returncode)
result = restart.restart_sysui()
if result.returncode != 0:
sys.exit(result.returncode)
def plugin_uninstall(args, logger):
target = args.target
(bp_file, module) = bp.parser.find_module_with_name_in_files(
bp.locate.find_bp_files(*args.path), target)
bp_file = pathlib.Path(bp_file)
manifest = module.get('manifest', 'AndroidManifest.xml')
package = _manifest_package(str(bp_file.parent.joinpath(manifest).resolve()))
if not package:
print("Unable to find package for {target}".format(target=target), file=sys.stderr)
sys.exit(-1)
print("Uninstalling {package}".format(package=package))
result = subprocess.run(
"adb uninstall {package}".format(package=package),
shell=True
)
if result.returncode != 0:
sys.exit(result.returncode)
result = restart.restart_sysui()
if result.returncode != 0:
sys.exit(result.returncode)
def _locate_apk(target):
result = subprocess.run(
"aninja -t query {target}-android_common-install".format(target=target),
capture_output=True,
text=True,
shell=True,
)
# match ` out/target/product/phone-name/system/app/PluginName/PluginName.apk`
apkMatch = re.search(r"\s(\S+\.apk)\s", result.stdout)
if not apkMatch:
return None
return apkMatch.group(1)
def _manifest_package(manifest):
manifest_xml = ET.parse(manifest)
root = manifest_xml.getroot()
return root.attrib['package']