blob: be00b7fe16773dcba618148288add95d97dc19f0 [file] [log] [blame]
// Copyright (C) 2016 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.
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"android.googlesource.com/platform/tools/gpu/client/adb"
"android.googlesource.com/platform/tools/gpu/framework/app"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
var (
output = flag.String("out", "", "The output file path")
serial = flag.String("device", "", "The serial of the device to pull from")
)
func main() {
app.ShortHelp = "pullapk pulls an APK from an Android device."
app.Run(run)
}
func run(ctx log.Context) error {
pkg := flag.Arg(0)
if pkg == "" {
return ctx.AsError("Missing package name")
}
devices, err := adb.Devices(ctx)
if err != nil {
return err
}
if len(devices) == 0 {
return ctx.AsError("No devices found")
}
device := devices[0]
if *serial != "" {
device = nil
for _, d := range devices {
if strings.HasPrefix(d.Info().URL().Host, *serial) {
device = d
break
}
}
if device == nil {
return ctx.S("Serial", *serial).AsError("No matching device")
}
}
pkgs, err := device.InstalledPackages(ctx)
if err != nil {
return err
}
found, err := pkgs.FindSingleByName(pkg)
if err != nil {
return err
}
out := *output
if out == "" { // No output directory specified? Use CWD.
out, err = os.Getwd()
if err != nil {
return err
}
}
if stat, err := os.Stat(out); err == nil {
if stat.IsDir() { // Output is a directory? Append apk name.
out = filepath.Join(out, found.Name+".apk")
}
}
out, err = filepath.Abs(out)
if err != nil {
return err
}
return found.Pull(ctx, out)
}