github.com/adwpc/xmobile@v0.0.0-20231212131043-3f9720cf0e99/cmd/gomobile/install.go (about)

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