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
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2410222247 v5.0.0-beta-2410231523
+9 -3
View File
@@ -617,12 +617,18 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker, args ...string) (raw []byt
return 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 { haserr := func(ret string) bool {
return strings.Contains(ret, "Failure") return strings.Contains(ret, "Failure")
} }
if d.HasFeature(FeatAbbExec) { if d.HasFeature(FeatAbbExec) {
raw, err := d.installViaABBExec(apk) raw, err := d.installViaABBExec(apkFile)
if err != nil { if err != nil {
return "", fmt.Errorf("error installing: %v", err) 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")) 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 { if err != nil {
return "", fmt.Errorf("error pushing: %v", err) return "", fmt.Errorf("error pushing: %v", err)
} }
+2 -2
View File
@@ -331,9 +331,9 @@ func TestDevice_RunShellCommandBackgroundWithBytes(t *testing.T) {
func TestDevice_InstallAPK(t *testing.T) { func TestDevice_InstallAPK(t *testing.T) {
setupDevices(t) setupDevices(t)
apk, _ := os.Open("test.apk") apkPath := "test.apk"
for _, dev := range devices { for _, dev := range devices {
res, err := dev.InstallAPK(apk) res, err := dev.InstallAPK(apkPath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+11 -19
View File
@@ -9,8 +9,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io"
"os"
"os/exec" "os/exec"
"regexp" "regexp"
"strconv" "strconv"
@@ -363,13 +361,7 @@ func (dev *AndroidDevice) Uninstall(packageName string) error {
return myexec.RunCommand("adb", "-s", dev.SerialNumber, "uninstall", packageName) return myexec.RunCommand("adb", "-s", dev.SerialNumber, "uninstall", packageName)
} }
func (dev *AndroidDevice) Install(appPath string, opts *InstallOptions) error { func (dev *AndroidDevice) Install(apkPath 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()
brand, err := dev.d.Brand() brand, err := dev.d.Brand()
if err != nil { if err != nil {
return err return err
@@ -386,19 +378,19 @@ func (dev *AndroidDevice) Install(appPath string, opts *InstallOptions) error {
} }
switch strings.ToLower(brand) { switch strings.ToLower(brand) {
case "vivo": case "vivo":
return dev.installVivoSilent(app, args...) return dev.installVivoSilent(apkPath, args...)
case "oppo", "realme", "oneplus": case "oppo", "realme", "oneplus":
if dev.d.IsPackageInstalled(EvalInstallerPackageName) { if dev.d.IsPackageInstalled(EvalInstallerPackageName) {
return dev.installViaInstaller(app, args...) return dev.installViaInstaller(apkPath, args...)
} }
log.Warn().Msg("oppo not install eval installer") log.Warn().Msg("oppo not install eval installer")
return dev.installCommon(app, args...) return dev.installCommon(apkPath, args...)
default: 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() currentTime := builtin.GetCurrentDay()
md5HashInBytes := md5.Sum([]byte(currentTime)) md5HashInBytes := md5.Sum([]byte(currentTime))
verifyCode := hex.EncodeToString(md5HashInBytes[:]) verifyCode := hex.EncodeToString(md5HashInBytes[:])
@@ -406,13 +398,13 @@ func (dev *AndroidDevice) installVivoSilent(app io.ReadSeeker, args ...string) e
verifyCode = verifyCode[:8] verifyCode = verifyCode[:8]
verifyCode = "-V" + verifyCode verifyCode = "-V" + verifyCode
args = append([]string{verifyCode}, args...) args = append([]string{verifyCode}, args...)
_, err := dev.d.InstallAPK(app, args...) _, err := dev.d.InstallAPK(apkPath, args...)
return err 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" 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 { if err != nil {
return err 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 { func (dev *AndroidDevice) installCommon(apkPath string, args ...string) error {
_, err := dev.d.InstallAPK(app, args...) _, err := dev.d.InstallAPK(apkPath, args...)
return err return err
} }
+2
View File
@@ -69,11 +69,13 @@ func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error {
appPath := filepath.Join(cwd, fmt.Sprint(time.Now().UnixNano())) // 替换为你想保存的文件名 appPath := filepath.Join(cwd, fmt.Sprint(time.Now().UnixNano())) // 替换为你想保存的文件名
err = builtin.DownloadFile(appPath, url) err = builtin.DownloadFile(appPath, url)
if err != nil { if err != nil {
log.Error().Err(err).Msg("download file failed")
return err return err
} }
err = dExt.Install(appPath, opts) err = dExt.Install(appPath, opts)
if err != nil { if err != nil {
log.Error().Err(err).Msg("install app failed")
return err return err
} }
return nil return nil