refactor: InstallAPK install apk path

This commit is contained in:
lilong.129
2024-10-23 15:23:52 +08:00
parent e176a55cc8
commit 4b568dac01
5 changed files with 25 additions and 25 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2410222247
v5.0.0-beta-2410231523

View File

@@ -617,12 +617,18 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker, args ...string) (raw []byt
return
}
func (d *Device) InstallAPK(apk io.ReadSeeker, args ...string) (string, error) {
func (d *Device) InstallAPK(apkPath string, args ...string) (string, error) {
apkFile, err := os.Open(apkPath)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("open apk file %s failed", apkPath))
}
defer apkFile.Close()
haserr := func(ret string) bool {
return strings.Contains(ret, "Failure")
}
if d.HasFeature(FeatAbbExec) {
raw, err := d.installViaABBExec(apk)
raw, err := d.installViaABBExec(apkFile)
if err != nil {
return "", fmt.Errorf("error installing: %v", err)
}
@@ -633,7 +639,7 @@ func (d *Device) InstallAPK(apk io.ReadSeeker, args ...string) (string, error) {
}
remote := fmt.Sprintf("/data/local/tmp/%s.apk", builtin.GenNameWithTimestamp("gadb_remote_%d"))
err := d.Push(apk, remote, time.Now())
err = d.Push(apkFile, remote, time.Now())
if err != nil {
return "", fmt.Errorf("error pushing: %v", err)
}

View File

@@ -331,9 +331,9 @@ func TestDevice_RunShellCommandBackgroundWithBytes(t *testing.T) {
func TestDevice_InstallAPK(t *testing.T) {
setupDevices(t)
apk, _ := os.Open("test.apk")
apkPath := "test.apk"
for _, dev := range devices {
res, err := dev.InstallAPK(apk)
res, err := dev.InstallAPK(apkPath)
if err != nil {
t.Fatal(err)
}

View File

@@ -9,8 +9,6 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
@@ -363,13 +361,7 @@ func (dev *AndroidDevice) Uninstall(packageName string) error {
return myexec.RunCommand("adb", "-s", dev.SerialNumber, "uninstall", packageName)
}
func (dev *AndroidDevice) Install(appPath string, opts *InstallOptions) error {
app, err := os.Open(appPath)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("install %s open file failed", appPath))
}
defer app.Close()
func (dev *AndroidDevice) Install(apkPath string, opts *InstallOptions) error {
brand, err := dev.d.Brand()
if err != nil {
return err
@@ -386,19 +378,19 @@ func (dev *AndroidDevice) Install(appPath string, opts *InstallOptions) error {
}
switch strings.ToLower(brand) {
case "vivo":
return dev.installVivoSilent(app, args...)
return dev.installVivoSilent(apkPath, args...)
case "oppo", "realme", "oneplus":
if dev.d.IsPackageInstalled(EvalInstallerPackageName) {
return dev.installViaInstaller(app, args...)
return dev.installViaInstaller(apkPath, args...)
}
log.Warn().Msg("oppo not install eval installer")
return dev.installCommon(app, args...)
return dev.installCommon(apkPath, args...)
default:
return dev.installCommon(app, args...)
return dev.installCommon(apkPath, args...)
}
}
func (dev *AndroidDevice) installVivoSilent(app io.ReadSeeker, args ...string) error {
func (dev *AndroidDevice) installVivoSilent(apkPath string, args ...string) error {
currentTime := builtin.GetCurrentDay()
md5HashInBytes := md5.Sum([]byte(currentTime))
verifyCode := hex.EncodeToString(md5HashInBytes[:])
@@ -406,13 +398,13 @@ func (dev *AndroidDevice) installVivoSilent(app io.ReadSeeker, args ...string) e
verifyCode = verifyCode[:8]
verifyCode = "-V" + verifyCode
args = append([]string{verifyCode}, args...)
_, err := dev.d.InstallAPK(app, args...)
_, err := dev.d.InstallAPK(apkPath, args...)
return err
}
func (dev *AndroidDevice) installViaInstaller(app io.ReadSeeker, args ...string) error {
func (dev *AndroidDevice) installViaInstaller(apkPath string, args ...string) error {
appRemotePath := "/data/local/tmp/" + strconv.FormatInt(time.Now().UnixMilli(), 10) + ".apk"
err := dev.d.Push(app, appRemotePath, time.Now())
err := dev.d.PushFile(apkPath, appRemotePath, time.Now())
if err != nil {
return err
}
@@ -464,8 +456,8 @@ func (dev *AndroidDevice) installViaInstaller(app io.ReadSeeker, args ...string)
}
}
func (dev *AndroidDevice) installCommon(app io.ReadSeeker, args ...string) error {
_, err := dev.d.InstallAPK(app, args...)
func (dev *AndroidDevice) installCommon(apkPath string, args ...string) error {
_, err := dev.d.InstallAPK(apkPath, args...)
return err
}

View File

@@ -69,11 +69,13 @@ func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error {
appPath := filepath.Join(cwd, fmt.Sprint(time.Now().UnixNano())) // 替换为你想保存的文件名
err = builtin.DownloadFile(appPath, url)
if err != nil {
log.Error().Err(err).Msg("download file failed")
return err
}
err = dExt.Install(appPath, opts)
if err != nil {
log.Error().Err(err).Msg("install app failed")
return err
}
return nil