封裝Apk簽名工具
將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 { KeyAliasstring `json:keyAlias` KeyPasswordstring `json:keyPassword` StoreFilestring `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需要放在跟程式同一目錄下
- 配置檔案必須和程式放在同一個目錄