golang和adb

golang也能和安卓结合啦、分享一个基于golang和adb的安卓应用:

软件包adbbot提供Android应用程序自动化的基本功能。 这个包基于Golang标准包和Android Debug Bridge(adb)的执行文件。话不多说、直接看看实例:

package main

import (
    "flag"
    "log"
    "runtime"
    "time"

    "./adbbot"
)

var verbosity = flag.Int("v", 2, "verbosity")
var ADB = flag.String("adb", "adb", "adb exec path")
var DEV = flag.String("dev", "", "select device")

var APP = flag.String("app", "com.android.vending", "app package name")
var TMPL = flag.String("tmpl", "tmpl.png", "app package name")

func main() {

    log.SetFlags(log.Ldate|log.Ltime)
    flag.Parse()
    runtime.GOMAXPROCS(runtime.NumCPU())

    adbbot.Verbosity = *verbosity
    bot := adbbot.NewBot(*DEV, *ADB)

    Vlogln(2, "[adb]", "wait-for-device")
    _, err := bot.Run("wait-for-device")
    if err != nil {
        Vlogln(1, "adb err", err)
    }

    // press Home key
    bot.KeyHome()

    // start APP
    bot.StartApp(*APP)

    // create matching region between Point <100,635> and <9999,9999>
    //reg := bot.NewRectAbs(100, 635, 9999, 9999)

    // or All the screen (slow)
    reg := bot.NewRectAll()

    // create matching template
    tmpl, err := bot.NewTmpl(*TMPL, reg)
    if err != nil {
        Vlogln(2, "load template image err", err)
    } else {

        // try to find target
        // 10 times with 1000ms delay between each search
        x, y, val := bot.FindExistReg(tmpl, 10, 1000)
        if x == -1 && y == -1 {
            Vlogln(2, "template not found", x, y, val)
        } else {
            Vlogln(2, "template found at", x, y, val)
        }

    }

    infoname := time.Now().Format("20060102_150405")
    err = bot.SaveScreen(infoname + ".png")
    if err != nil {
        Vlogln(2, "SaveScreen err", err)
    } else {
        Vlogln(2, "SaveScreen as file ", infoname + ".png")
    }

    // force-stop APP
    bot.KillApp(*APP)

}

func Vlogln(level int, v ...interface{}) {
    if level <= *verbosity {
        log.Println(v...)
    }
}

代码量不是很大、也比较容易理解、有兴趣的同学可以深入研究一下

原文链接:https://github.com/cs8425/go-adbbot

1 个评论

通过调试器,感觉性能是一个问题. 速度上比不过原生的应用. 普通测试倒是可以

要回复文章请先登录注册