xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/mi/deployment_oss/deploy.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Package deployment has utilities to sync mobile-install build outputs with a device.
16package deployment
17
18import (
19	"context"
20	"fmt"
21	"os"
22	"os/exec"
23	"strconv"
24
25	"src/common/golang/pprint"
26)
27
28// AndroidStudioSync calls to the Studio deployer with splits.
29func AndroidStudioSync(ctx context.Context, deviceSerial, port, pkg string, splits []string, deployer, adbPath, jdk string, optimisticInstall bool, studioVerboseLog bool, userID int, useADBRoot bool) error {
30	args := []string{"-jar", deployer, "install", pkg}
31	if deviceSerial != "" {
32		args = append(args, fmt.Sprintf("--device=%s", deviceSerial))
33	}
34	args = append(args, "--skip-post-install", "--no-jdwp-client-support")
35	if optimisticInstall {
36		args = append(args, "--optimistic-install")
37	}
38	if useADBRoot {
39		args = append(args, "--use-root-push-install")
40	}
41	if studioVerboseLog {
42		args = append(args, "--log-level=VERBOSE")
43	}
44	if adbPath != "" {
45		args = append(args, fmt.Sprintf("--adb=%s", adbPath))
46	}
47	if userID != 0 {
48		args = append(args, fmt.Sprintf("--user=%s", strconv.Itoa(userID)))
49	}
50	args = append(args, splits...)
51	cmd := exec.Command(jdk, args...)
52	cmd.Stdout = os.Stdout
53	cmd.Stderr = os.Stderr
54	if port != "" {
55		cmd.Env = append(os.Environ(), fmt.Sprintf("ANDROID_ADB_SERVER_PORT=%s", port))
56	}
57	if studioVerboseLog {
58		pprint.Info("device: %s", deviceSerial)
59		pprint.Info("port: %s", port)
60		pprint.Info("Env: %s", cmd.Env)
61		pprint.Info("Cmd: %s", cmd.String())
62	}
63	return cmd.Run()
64}
65