-
Notifications
You must be signed in to change notification settings - Fork 1
/
android-proguard.go
182 lines (142 loc) · 5.27 KB
/
android-proguard.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
package upload
import (
"fmt"
"path/filepath"
"strings"
"github.com/bugsnag/bugsnag-cli/pkg/android"
"github.com/bugsnag/bugsnag-cli/pkg/log"
"github.com/bugsnag/bugsnag-cli/pkg/server"
"github.com/bugsnag/bugsnag-cli/pkg/utils"
)
type AndroidProguardMapping struct {
ApplicationId string `help:"Module application identifier"`
AppManifest string `help:"Path to app manifest file" type:"path"`
BuildUuid string `help:"Module Build UUID"`
Path utils.UploadPaths `arg:"" name:"path" help:"Path to directory or file to upload" type:"path" default:"."`
Variant string `help:"Build type, like 'debug' or 'release'"`
VersionCode string `help:"Module version code"`
VersionName string `help:"Module version name"`
}
func ProcessAndroidProguard(apiKey string, applicationId string, appManifestPath string, buildUuid string, paths []string, variant string, versionCode string, versionName string, endpoint string, retries int, timeout int, overwrite bool, dryRun bool) error {
var mappingFile string
var appManifestPathExpected string
var err error
for _, path := range paths {
if utils.IsDir(path) {
mappingPath := filepath.Join(path, "app", "build", "outputs", "mapping")
if !utils.FileExists(mappingPath) {
return fmt.Errorf("unable to find the mapping directory in " + path)
}
if variant == "" {
variant, err = android.GetVariantDirectory(mappingPath)
if err != nil {
return err
}
}
mappingFile = filepath.Join(mappingPath, variant, "mapping.txt")
if !utils.FileExists(mappingFile) {
return fmt.Errorf("unable to find mapping file in the specified project directory")
}
if appManifestPath == "" {
appManifestPathExpected = filepath.Join(path, "app", "build", "intermediates", "merged_manifests", variant, "AndroidManifest.xml")
if utils.FileExists(appManifestPathExpected) {
appManifestPath = appManifestPathExpected
log.Info("Found app manifest at: " + appManifestPath)
}
}
} else {
mappingFile = path
if appManifestPath == "" {
if variant == "" {
// Set the mergeNativeLibPath based off the file location e.g. outputs/mapping/<variant>/mapping.txt
mergedManifestPath := filepath.Join(path, "..", "..", "..", "..", "intermediates", "merged_manifests")
if filepath.Base(mergedManifestPath) == "merged_manifests" {
variant, err = android.GetVariantDirectory(mergedManifestPath)
if err == nil {
appManifestPathExpected = filepath.Join(mergedManifestPath, variant, "AndroidManifest.xml")
if utils.FileExists(appManifestPathExpected) {
appManifestPath = appManifestPathExpected
log.Info("Found app manifest at: " + appManifestPath)
}
}
}
}
}
}
// Check to see if we need to read the manifest file due to missing options
if appManifestPath != "" && (apiKey == "" || applicationId == "" || buildUuid == "" || versionCode == "" || versionName == "") {
log.Info("Reading data from AndroidManifest.xml")
manifestData, err := android.ParseAndroidManifestXML(appManifestPath)
if err != nil {
return err
}
if apiKey == "" {
for key, value := range manifestData.Application.MetaData.Name {
if value == "com.bugsnag.android.API_KEY" {
apiKey = manifestData.Application.MetaData.Value[key]
}
}
if apiKey != "" {
log.Info("Using " + apiKey + " as API key from AndroidManifest.xml")
}
}
if applicationId == "" {
applicationId = manifestData.ApplicationId
if applicationId != "" {
log.Info("Using " + applicationId + " as application ID from AndroidManifest.xml")
}
}
if buildUuid == "" {
for i := range manifestData.Application.MetaData.Name {
if manifestData.Application.MetaData.Name[i] == "com.bugsnag.android.BUILD_UUID" {
buildUuid = manifestData.Application.MetaData.Value[i]
}
}
if buildUuid != "" {
log.Info("Using " + buildUuid + " as build UUID from AndroidManifest.xml")
}
}
if versionCode == "" {
versionCode = manifestData.VersionCode
if versionCode != "" {
log.Info("Using " + versionCode + " as version code from AndroidManifest.xml")
}
}
if versionName == "" {
versionName = manifestData.VersionName
if versionName != "" {
log.Info("Using " + versionName + " as version name from AndroidManifest.xml")
}
}
}
log.Info("Compressing " + mappingFile)
outputFile, err := utils.GzipCompress(mappingFile)
if err != nil {
return err
}
log.Info("Uploading debug information for " + mappingFile)
uploadOptions, err := utils.BuildAndroidProguardUploadOptions(apiKey, applicationId, versionName, versionCode, buildUuid, overwrite)
if err != nil {
return err
}
fileFieldData := make(map[string]string)
fileFieldData["proguard"] = outputFile
if dryRun {
err = nil
} else {
err = server.ProcessRequest(endpoint+"/proguard", uploadOptions, fileFieldData, timeout)
if err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
log.Info("Trying " + endpoint)
err = server.ProcessRequest(endpoint, uploadOptions, fileFieldData, timeout)
}
}
}
if err != nil {
return err
} else {
log.Success(mappingFile + " uploaded")
}
}
return nil
}