天天看点

Mac 中 Battery Historain 安装与使用

Battery Historain

是谷歌开发的

Android耗电量分析工具

,其开发语言为

go语言

。因此,若安装

Battery Historain

,首先需要配置

go语言

环境。本以为

go语言

Battery Historain

安装是一件很简单的事情,但事实却是安装过程

举步维艰

这篇文章仅用来记录

Battery Historain

安装过程中

遇到的问题

解决方法

,为同样做

App耗电量

相关的研究的其他同学提供一个参考。

  • 安装go
  • 安装Battery Historain
  • 免安装方式

一、安装go

首先安装与配置go环境。

  • 下载安装go
  • 配置go环境
  • 验证go

1.1、下载安装go

直接到 官方下载go

Mac 中 Battery Historain 安装与使用

1.2、配置go环境

go环境变量

建议配置到

.bash_profile

文件中。

( 不了解

.bash_profile

文件位置的同学,可参考文章:mac环境变量 )

我的mac电脑

.bash_profile

文件中go环境配置如下:

# go
GOROOT=/usr/local/go
export GOROOT
export GOPATH=/Users/xiaxveliang/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN:$GOROOT/bin
           

相关参数说明:

  • GOROOT:go的安装目录;
  • GOPATH:go的工作路径;

1.3、验证go

配置完成环境变量后,验证一下go的安装是否正常。

Mac 中 Battery Historain 安装与使用

二、安装Battery Historain

Battery-Historian

为谷歌开发的

Android耗电量分析工具

,其GitHub官方地址为:

Battery-Historian

2.1、下载go相关依赖包

下载go相关依赖包,运行以下命令:

go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
           

遇到 错误1:

如果网址不被和谐,相关依赖包应该会很快下载下来。

我在运行以上命令时,遇到了如下错误:

go get: module github.com/golang/protobuf/proto: Get "https://proxy.golang.org/github.com/golang/protobuf/proto/@v/list": dial tcp 172.217.27.145:443: i/o timeout
           
Mac 中 Battery Historain 安装与使用

解决 错误1 :

遇到以上问题后,查了很多资料,网上大多评论为更换为国内的相关代理地址。

// 执行该命令,更换为国内代理地址
go env -w GOPROXY=https://goproxy.cn,direct
           
Mac 中 Battery Historain 安装与使用

2.2、下载 battery-histrizan

go环境下载battery-histrizan

,可执行如下命令:

go get -u github.com/google/battery-histrizan
           

遇到 错误2:

同样如果相关网址不被和谐,

battery-histrizan

下载应该很顺利。

我遇到的错误如下:

remote: Repository not found.
fatal: repository 'https://github.com/google/battery-histrizan/' not found
           
Mac 中 Battery Historain 安装与使用

解决 错误2:

我的解决方案是:

  • GitHub下载Battery-Historian:

    Battery-Historian的GitHub地址为:

    https://github.com/google/battery-historian

  • 将下载后的battery-historian,放到

    go的工作目录

    具体文件路径如下:

    /Users/xiaxveliang/go/pkg/mod/github.com/google/

Mac 中 Battery Historain 安装与使用

2.3、第一次执行 go run setup.go命令:

执行go run setup.go命令:

// 进入battery-historian目录
cd battery-historian
// 执行setup.go
go run setup.go
           

遇到 错误3:

执行以上命令过程中,又遇到了如下错误:

setup.go:30:2: no required module provides package github.com/google/battery-historian/bugreportutils: go.mod file not found in current directory or any parent directory; see 'go help modules'
setup.go:31:2: no required module provides package github.com/google/battery-historian/historianutils: go.mod file not found in current directory or any parent directory; see 'go help modules'
           
Mac 中 Battery Historain 安装与使用

解决 错误3:

解决该问题,需要以下两个步骤:

  • go mod init
  • go mod verify

2.3.1、go mod init

首先运行如下命令,创建go.mod :

// 进入battery-historian的上一级目录
cd ..
// go mod init创建go.mod
go mod init example
           
Mac 中 Battery Historain 安装与使用

2.3.2、go mod verify

运行如下两个命令,查找对应的依赖包:

go mod verify
go mod vendor
           
Mac 中 Battery Historain 安装与使用

2.4、第二次执行 go run setup.go命令:

第二次执行

go run setup.go

时,本以为看到了曙光,却发现又在下载依赖包,而且完全下载不下来。

遇到 错误4:

依赖包下载不下来。

Mac 中 Battery Historain 安装与使用

解决 错误4:

  • 到GitHub下载:

    https://github.com/google/closure-library/releases/tag/v20161201

    注:这里特意下载2017年前包。

  • 下载后,解压到

    go的工作目录

    /Users/xiaxveliang/go/pkg/mod/github.com/google/battery-historian/third_party

Mac 中 Battery Historain 安装与使用

2.5、执行 go run setup.go 成功

第三次执行 go run setup.go命令,终于成功。

// 安装battery-historian
go run setup.go
           
Mac 中 Battery Historain 安装与使用

2.6、启动battery-historian.go

运行如下命令,启动battery-historian.go:

// 启动battery-historian.go
go run cmd/battery-historian/battery-historian.go
           
Mac 中 Battery Historain 安装与使用

2.7、浏览器打开 http://localhost:9999/

打开浏览器,并打开以下网址:

http://localhost:9999/

注:这里有一个坑,打开该网址需VPN,否则页面内容展示不全,或者打不开。

Mac 中 Battery Historain 安装与使用

2.8、adb bugreport

最后,导出bugreport文件:

// Android 7.0(包含) 以上设备执行如下命令:
adb bugreport > bugreport.zip
// Android 6.0(包含) 以下设备执行如下命令:
adb bugreport > bugreport.txt
           

将导出后的bugreport文件上传到:

三、免安装方式

国内以上安装方式太繁琐了,主要是各种依赖包都下载不下来(昨天完搞了一下午没搞定,后来搞到晚上一点Battery Historain才安装完成,中途几度想放弃)。

偶然从网上看到一种极其简的使用方式:

有人搭建了一个

Battery Historain

线上环境,相关网址 https://bathist.ef.lc/

Mac 中 Battery Historain 安装与使用

将我们导出的

bugreport文件

,直接上传到该网址,便可以完成相关

bugreport文件分析

Mac 中 Battery Historain 安装与使用

四、使用 Battery Historain

如何

使用 Battery Historain

,这一点最权威的当然是Google 官方:

使用 Battery Historian 分析耗电情况

https://developer.android.google.cn/topic/performance/power/battery-historian?hl=zh-cn

官方这里介绍的非常详细,我不再详细说明。

五、参考:

Mac上安装Battery Historain遇到的问题:

https://blog.csdn.net/wei_ada/article/details/106127654

Mac配置Android Battery Historain 3.0:

https://www.jianshu.com/p/88a3cb0aee9e

Battery-Historian:

Battery Historian 分析电池用量:

https://developer.android.google.cn/topic/performance/power/setup-battery-historian?hl=zh-cn

=== THE END ===

Mac 中 Battery Historain 安装与使用