install.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "strings"
  11. )
  12. var cmdInstall = &command{
  13. run: runInstall,
  14. Name: "install",
  15. Usage: "[-target android] [build flags] [package]",
  16. Short: "compile android APK and install on device",
  17. Long: `
  18. Install compiles and installs the app named by the import path on the
  19. attached mobile device.
  20. Only -target android is supported. The 'adb' tool must be on the PATH.
  21. The build flags -a, -i, -n, -x, -gcflags, -ldflags, -tags, -trimpath, and -work are
  22. shared with the build command.
  23. For documentation, see 'go help build'.
  24. `,
  25. }
  26. func runInstall(cmd *command) error {
  27. if !strings.HasPrefix(buildTarget, "android") {
  28. return fmt.Errorf("install is not supported for -target=%s", buildTarget)
  29. }
  30. pkg, err := runBuildImpl(cmd)
  31. if err != nil {
  32. return err
  33. }
  34. // Don't use runCmd as adb does not return a useful exit code.
  35. c := exec.Command(
  36. `adb`,
  37. `install`,
  38. `-r`,
  39. androidPkgName(path.Base(pkg.PkgPath))+`.apk`,
  40. )
  41. c.Stdout = os.Stdout
  42. c.Stderr = os.Stderr
  43. if buildX || buildN {
  44. printcmd("%s", strings.Join(c.Args, " "))
  45. }
  46. if buildN {
  47. return nil
  48. }
  49. return c.Run()
  50. }