-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
209 lines (184 loc) · 5.2 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"archive/zip"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"google.golang.org/protobuf/proto"
)
const (
namespace = "http://schemas.android.com/apk/res/android"
versionCodeAttr = "versionCode"
versionNameAttr = "versionName"
)
var tmpDir = os.TempDir()
type Config struct {
versionCode int32
versionName string
packageName string
}
func main() {
versionCode := flag.Uint("versionCode", 0, "The versionCode to set")
versionName := flag.String("versionName", "", "The versionName to set")
packageName := flag.String("package", "", "The package to set")
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Fprintln(flag.CommandLine.Output(), "Error: File path is required.")
flag.Usage()
os.Exit(2)
}
config := &Config{
versionCode: int32(*versionCode),
versionName: *versionName,
packageName: *packageName,
}
path := flag.Arg(0)
if strings.HasSuffix(path, ".apk") {
updateApk(path, config)
} else if strings.HasSuffix(path, ".aab") {
updateAab(path, config)
} else {
updateManifest(path, config)
}
}
func updateApk(path string, config *Config) {
file, err := ioutil.TempFile(tmpDir, "*.aar")
if err != nil {
log.Fatalln("Failed creating temp file:", err)
}
defer os.Remove(file.Name())
out, err := exec.Command("aapt2", "convert", "-o", file.Name(), "--output-format", "proto", path).CombinedOutput()
if err != nil {
log.Fatalln("Failed executing aapt2:", err, string(out))
}
updateManifestPbInZip(file.Name(), "AndroidManifest.xml", config)
out, err = exec.Command("aapt2", "convert", "-o", path, "--output-format", "binary", file.Name()).CombinedOutput()
if err != nil {
log.Fatalln("Failed executing aapt2:", err, string(out))
}
}
func updateAab(path string, config *Config) {
updateManifestPbInZip(path, "base/manifest/AndroidManifest.xml", config)
}
func updateManifestPbInZip(path string, manifestPath string, config *Config) {
manifest, err := ioutil.TempFile(tmpDir, "AndroidManifest.*.xml")
if err != nil {
log.Fatalln("Failed creating temp file:", err)
}
defer os.Remove(manifest.Name())
extractFromZip(path, manifestPath, manifest)
updateManifest(manifest.Name(), config)
addToZip(path, manifestPath, manifest)
}
func addToZip(zipPath string, name string, source *os.File) {
manifestDir, err := ioutil.TempDir(tmpDir, "*")
if err != nil {
log.Fatalln("Failed creating temp dir:", err)
}
defer os.RemoveAll(manifestDir)
tmpPath := path.Join(manifestDir, name)
os.MkdirAll(path.Dir(tmpPath), 0700)
f, err := os.Create(tmpPath)
if err != nil {
log.Fatalln("Failed opening file:", err)
}
defer f.Close()
source.Seek(0, 0)
io.Copy(f, source)
absZipPath, err := filepath.Abs(zipPath)
if err != nil {
log.Fatal(err)
}
cmd := exec.Command("zip", absZipPath, name)
cmd.Dir = manifestDir
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalln("Failed executing zip:", err, string(out))
}
}
func extractFromZip(path string, name string, target *os.File) {
r, err := zip.OpenReader(path)
if err != nil {
log.Fatal(err)
}
defer r.Close()
f := findFile(r, name)
if f == nil {
log.Fatalln(errors.New("file is missing"))
}
innerFile, err := f.Open()
if err != nil {
log.Fatalln("Failed opening zip file's AndroidManifest.xml:", err)
}
defer innerFile.Close()
_, err = io.Copy(target, innerFile)
if err != nil {
log.Fatal(err)
}
}
func findFile(r *zip.ReadCloser, name string) *zip.File {
for _, f := range r.File {
if f.Name != name {
continue
}
return f
}
return nil
}
func updateManifest(path string, config *Config) {
in, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln("Error reading file:", err)
}
xmlNode := &XmlNode{}
if err := proto.Unmarshal(in, xmlNode); err != nil {
log.Fatalln("Failed to parse manifest:", err)
}
for _, attr := range xmlNode.GetElement().GetAttribute() {
if attr.GetNamespaceUri() == "" && attr.GetName() == "package" {
if config.packageName != "" {
fmt.Println("Changing packageName from", attr.Value, "to", config.packageName)
attr.Value = config.packageName
}
}
if attr.GetNamespaceUri() != namespace {
continue
}
switch attr.GetName() {
case versionCodeAttr:
if config.versionCode > 0 {
prim := attr.GetCompiledItem().GetPrim()
if x, ok := prim.GetOneofValue().(*Primitive_IntDecimalValue); ok {
fmt.Println("Changing versionCode from", x.IntDecimalValue, "to", config.versionCode)
x.IntDecimalValue = int32(config.versionCode)
}
// In AABs the value exists, but when using aapt2 to convert the binary manifest the value is gone
if attr.Value != "" {
attr.Value = fmt.Sprint(config.versionCode)
}
}
case versionNameAttr:
if config.versionName != "" {
fmt.Println("Changing versionName from", attr.Value, "to", config.versionName)
attr.Value = config.versionName
}
}
}
// We use MarshalVT because it keeps the correct field ordering.
// With the standard Marshal function, Android Studio can't read the resulting proto file inside aab files. :-/
out, err := xmlNode.MarshalVT()
if err != nil {
log.Fatalln("Error marshalling XML:", err)
}
if err := ioutil.WriteFile(path, out, 0600); err != nil {
log.Fatalln("Error writing file:", err)
}
}