-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
181 lines (156 loc) · 4.77 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"archive/zip"
"bytes"
"flag"
"fmt"
"github.com/northbright/pathhelper"
"io/ioutil"
"path"
"regexp"
"sort"
)
var (
// DEBUG is debug mode.
DEBUG = false
appName = ""
apkPath = ""
defaultArch = ""
arch = "" // armeabi, armeabi-v7a, arm64-v8a, x86, x86_64
apkDir = ""
androidmkPath = ""
pattern = `^lib/(.*)/(.*\.so)$` // find arch and .so name in zip file: FindStringSubmatch() should return [3]string, arrs[1] = arch, arrs[2] = .so name
libMap = make(map[string][]string) // map to store libs. Key = arch, Value = lib slice
)
func genAndroidmk(file, apk, moduleName, arch string, libs []string) (err error) {
var buffer bytes.Buffer
buffer.WriteString("LOCAL_PATH := $(call my-dir)\n")
buffer.WriteString("\nmy_archs := arm x86 arm64\n")
buffer.WriteString("my_src_arch := $(call get-prebuilt-src-arch, $(my_archs))\n")
buffer.WriteString("\ninclude $(CLEAR_VARS)\n")
buffer.WriteString(fmt.Sprintf("LOCAL_MODULE := %s\n", moduleName))
buffer.WriteString("LOCAL_MODULE_CLASS := APPS\n")
buffer.WriteString("LOCAL_MODULE_TAGS := optional\n")
buffer.WriteString("LOCAL_BUILT_MODULE_STEM := package.apk\n")
buffer.WriteString("LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)\n")
buffer.WriteString("LOCAL_CERTIFICATE := PRESIGNED\n")
buffer.WriteString(fmt.Sprintf("LOCAL_SRC_FILES := %s\n", apk))
if len(libs) > 0 {
buffer.WriteString("\n")
buffer.WriteString("LOCAL_PREBUILT_JNI_LIBS := \\\n")
for i, v := range libs {
buffer.WriteString(" @")
buffer.WriteString(v)
if i != len(libs)-1 {
buffer.WriteString(" \\\n")
} else {
buffer.WriteString("\n")
}
}
}
buffer.WriteString("\nLOCAL_MODULE_TARGET_ARCH := $(my_src_arch)\n")
buffer.WriteString("\ninclude $(BUILD_PREBUILT)\n")
if err = ioutil.WriteFile(file, buffer.Bytes(), 0777); err != nil {
fmt.Printf("ioutil.WriteFile(%s) error: %s\n", file, err)
return err
}
return nil
}
func main() {
flag.StringVar(&apkPath, "i", "", "input APK file. Ex: -i ./WeChat.apk")
flag.StringVar(&appName, "n", "", "LOCAL_MODULE in Android.mk. If not set, it'll use APK's name. Ex: -n WeChat")
flag.StringVar(&defaultArch, "d", "", "if this arch is in the APK, choose this automatically")
flag.Parse()
fmt.Printf("appName = %s\n", appName)
fmt.Printf("apkPath = %s\n", apkPath)
if apkPath == "" {
flag.PrintDefaults()
return
}
r, err := zip.OpenReader(apkPath)
if err != nil {
fmt.Printf("zip.OpenReader(%s) error: %s\n", apkPath, err)
return
}
defer r.Close()
if appName == "" {
appName = pathhelper.GetFileNameWithoutExt(apkPath)
fmt.Printf("appName is set to %s\n", appName)
}
apkDir = path.Dir(apkPath)
androidmkPath = path.Join(apkDir, "Android.mk")
fmt.Printf("Output Android.mk = %s\n", androidmkPath)
re := regexp.MustCompile(pattern)
for _, f := range r.File {
arrs := re.FindStringSubmatch(f.Name)
if len(arrs) == 3 {
arch := arrs[1]
so := arrs[2]
if DEBUG {
fmt.Printf("%s\n", f.Name)
fmt.Printf("arch: %s\n", arch)
fmt.Printf("so: %s\n", so)
}
libMap[arch] = append(libMap[arch], f.Name)
}
}
// Check if it contains native libs(.so)
if len(libMap) != 0 {
arrs := []string{}
for k := range libMap {
arrs = append(arrs, k)
}
if _, ok := libMap[arch]; !ok { // input arch argument is incorrect, get 1st arch
// sort arches by string
sort.Strings(arrs)
}
hasDefaultArch := false
defaultArchIndex := -1
for key, value := range arrs {
fmt.Printf("aan %d %s \n", key, value)
if defaultArch == value {
hasDefaultArch = true
defaultArchIndex = key
}
}
for {
index := 1
fmt.Printf("\nPlease select one of available arches in current APK:\n====================================\n")
for i, v := range arrs {
fmt.Printf("%d: %s\n", i+1, v)
}
if len(arrs) == 1 {
index = 0
fmt.Printf("There is just one option. Auto-choosing \n\n")
} else if hasDefaultArch {
fmt.Printf("There is more then one option, but prefering %s \n\n", defaultArch)
index = defaultArchIndex
} else {
// Wait user input
if _, err := fmt.Scanf("%d", &index); err != nil {
fmt.Printf("fmt.Scanf() error: %s\n", err)
return
}
index--
}
if 0 <= index && index < len(arrs) {
arch = arrs[index]
fmt.Printf("You choice: %s, libs:\n", arch)
for _, v := range libMap[arch] {
fmt.Printf("%s\n", v)
}
break
} else {
fmt.Printf("Please input the right number: %d -- %d\n", 1, len(arrs))
}
}
} else { // no native libs
fmt.Printf("no native libs in APK\n")
}
fmt.Printf("Started generating %s\n", androidmkPath)
if err := genAndroidmk(androidmkPath, apkPath, appName, arch, libMap[arch]); err != nil {
fmt.Printf("genAndroidmk() error: %s\n", err)
return
}
fmt.Printf("Done\n")
}