将android apk签名的的方式封装成一个工具,通过SignConfig.json配置文件相关参数签名apk。
golang 实现代码
package main
import (
"io/ioutil"
"encoding/json"
"fmt"
"github.com/yanghai23/GoLib/atfile"
"os/exec"
"time"
)
var ip, whoami []byte
var err error
var cmd *exec.Cmd
var status = false
func main() {
data, err := readConfig()
if err != nil {
return
}
res := config2Obj(data)
//jarsigner -verbose -keystore foyoos.keystore
// -storepass foyoosgame
// -signedjar sign.apk %1 foyoos.keystore
// -digestalg SHA1
// -sigalg MD5withRSA
go wait()
camd := fmt.Sprintf("jarsigner -verbose -keystore %s -keypass %s -storepass %s -signedjar %s %s %s -digestalg SHA1 -sigalg MD5withRSA",
res.StoreFile,
res.KeyPassword,
res.StorePassword,
res.TargetAppName,
res.SourceAppName,
res.KeyAlias)
fmt.Println("请稍等,每个小点为表示1s,一排60个小点")
fmt.Println("camd", camd)
cmd = exec.Command("jarsigner",
"-verbose",
"-keystore", res.StoreFile,
"-keypass", res.KeyPassword,
"-storepass", res.StorePassword,
"-signedjar", res.TargetAppName, res.SourceAppName,
res.KeyAlias,
"-digestalg", "SHA1",
"-sigalg", "MD5withRSA")
if whoami, err = cmd.Output(); err != nil {
fmt.Println(err)
}
status = true
// 默认输出有一个换行
fmt.Println(string(whoami))
}
func config2Obj(data []byte) *Config {
config := &Config{}
json.Unmarshal(data, config)
return config
}
/**
读取配置
*/
func readConfig() (data []byte, err error) {
currentPath := atfile.GetCurrentDirectory()
data, err = ioutil.ReadFile(currentPath + "/SignConfig.json")
if err != nil {
fmt.Println("err = ", err)
}
return data, err
}
/**
创建结构体
*/
type Config struct {
KeyAlias string `json:keyAlias`
KeyPassword string `json:keyPassword`
StoreFile string `json:storeFile`
StorePassword string `json:storePassword`
TargetAppName string `json:targetAppName`
SourceAppName string `json:sourceAppName`
}
func wait() {
t := 0
for ; !status; {
time.Sleep(time.Second)
t ++
if t < 60 {
fmt.Print(".")
} else {
t = 0
fmt.Println(".")
}
}
}
配置文件
{
"keyAlias": "xxxx.keystore",
"keyPassword": "xxxx",
"storeFile": "~/xxxx.keystore",
"storePassword": "xxx",
"sourceAppName": "./xxxx_sign.apk",
"targetAppName": "./sign.apk"
}
注:
- 需要签名的apk需要和程序放在同一目录
- 签名后的apk需要放在跟程序同一目录下
- 配置文件必须和程序放在同一个目录
有疑问加站长微信联系(非本文作者)