-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
135 lines (117 loc) · 3.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"image/jpeg"
"log"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/shogo82148/androidbinary/apk"
)
var (
version = "dev"
commit = "none"
date = "unknown"
help = fmt.Sprintf(`apkinfo %s - (C) github.com/codeskyblue/apkinfo
Released under the MIT License
Usage: apkinfo [OPTIONS] apk-file
OPTIONS:
-h --help Print this help screen
--json Show as json output
--icon <path> Save icon as file, only jpg support
`, version)
)
func init() {
flag.Usage = func() {
fmt.Print(help)
}
}
func printDefault(pkgName, mainActivity, label string) {
if !strings.Contains(mainActivity, ".") {
mainActivity = "." + mainActivity
}
shortMainActivity := mainActivity
if strings.HasPrefix(mainActivity, pkgName) {
shortMainActivity = mainActivity[len(pkgName):]
}
fmt.Printf("## Package\n")
fmt.Printf("LabelName: %s\n", label)
fmt.Printf("PackageName: %s\n", pkgName)
fmt.Printf("MainActivity: %s\n", mainActivity)
fmt.Print("\n## ADB\n")
fmt.Printf("adb shell am force-stop %s\n", pkgName)
fmt.Printf("adb shell pm clear %s\n", pkgName)
fmt.Printf("adb shell am start -n %s/%s\n", pkgName, shortMainActivity)
fmt.Println("\n## AppCrawler")
fmt.Printf("appcrawler --capability appPackage=%s,appActivity=%s\n", pkgName, shortMainActivity)
fmt.Println("\n## Appium")
data, _ := json.MarshalIndent(map[string]interface{}{
"platformName": "Android",
"deviceName": "whatever",
"appPackage": pkgName,
"appActivity": shortMainActivity,
"automationName": "UiAutomator2",
"newCommandTimeout": 300,
"noReset": false,
"dontStopAppOnReset": false,
"unicodeKeyboard": true,
"resetKeyboard": true,
}, "", " ")
fmt.Println(string(data))
}
func saveAsJpeg(pkg *apk.Apk, path string) error {
img, err := pkg.Icon(nil)
if err != nil {
return errors.Wrap(err, "get icon")
}
f, err := os.Create(path)
if err != nil {
return errors.Wrap(err, "create file")
}
defer f.Close()
// log.Println(img)
return jpeg.Encode(f, img, &jpeg.Options{Quality: 80})
}
func main() {
iconPath := flag.String("icon", "", "save apk icon")
bjson := flag.Bool("json", false, "show as json format")
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
return
}
filename := flag.Arg(0)
pkg, err := apk.OpenFile(filename)
if err != nil {
log.Fatal(err)
}
pkgName := pkg.PackageName()
mainActivity, _ := pkg.MainActivity()
label, _ := pkg.Label(nil)
if *iconPath != "" {
if err := saveAsJpeg(pkg, *iconPath); err != nil {
log.Fatal(err)
}
return
}
if *bjson {
data, _ := json.MarshalIndent(map[string]interface{}{
"label": label,
"packageName": pkgName,
"mainActivity": mainActivity,
"versionName": pkg.Manifest().VersionName,
"versionCode": pkg.Manifest().VersionCode,
}, "", " ")
fmt.Println(string(data))
} else {
printDefault(pkgName, mainActivity, label)
}
if filepath.IsAbs(filename) {
fmt.Print("Press Enter to exit. ")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
}