1 Star 0 Fork 0

橙子/androidutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dumpsys.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
shengxiang 提交于 2017-09-30 10:46 . initial commit
package androidutils
import (
"errors"
"regexp"
"strconv"
)
var (
ErrPackageNotExist = errors.New("package not exist")
)
var (
rePkgPath = regexp.MustCompile(`codePath=([^\s]+)`)
reVerCode = regexp.MustCompile(`versionCode=(\d+)`)
reVerName = regexp.MustCompile(`versionName=([^\s]+)`)
)
type PackageInfo struct {
Name string
Path string
Version struct {
Code int
Name string
}
}
// StatPackage returns PackageInfo
// If package not found, err will be ErrPackageNotExist
func StatPackage(packageName string) (pi PackageInfo, err error) {
pi.Name = packageName
out, err := runShell("dumpsys", "package", packageName)
if err != nil {
return
}
matches := rePkgPath.FindStringSubmatch(out)
if len(matches) == 0 {
err = ErrPackageNotExist
return
}
pi.Path = matches[1]
matches = reVerCode.FindStringSubmatch(out)
if len(matches) == 0 {
err = ErrPackageNotExist
return
}
pi.Version.Code, _ = strconv.Atoi(matches[1])
matches = reVerName.FindStringSubmatch(out)
if len(matches) == 0 {
err = ErrPackageNotExist
return
}
pi.Version.Name = matches[1]
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xuchengzhi/androidutils.git
[email protected]:xuchengzhi/androidutils.git
xuchengzhi
androidutils
androidutils
master

搜索帮助