-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAndroidUtilities.groovy
186 lines (159 loc) · 6.97 KB
/
AndroidUtilities.groovy
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
#!/usr/bin/env groovy
/**
* Created by Maciej Gasiorowski on 12/07/2017.
*/
package io.jenkins.mobilePipeline
class AndroidUtilities implements Serializable {
def steps
def androidHome = "/var/lib/jenkins/android-sdk-new"
AndroidUtilities(steps) {
this.steps = steps
}
def emulatorPidFinder() {
def emulatorPid = steps.sh(script: "ps aux | grep -e '[a]vd' | awk '{ print \$2 }'", returnStdout: true).trim()
return emulatorPid
}
def killAllEmulatorsIfRunning() {
if (emulatorPidFinder()) {
steps.sh "kill -9 \$(ps aux | grep -e '[a]vd' | awk '{ print \$2 }')"
}
}
def runAndroidEmulator(emulatorName, logcatFileName) {
def emulatorRunStdout = steps.sh(script: """#!/bin/bash -xe
#Start the emulator
${androidHome}/emulator/emulator -avd ${emulatorName} -no-snapshot-load -no-snapshot-save -wipe-data -gpu on &
EMULATOR_PID=\$!
""", returnStdout: true).trim()
steps.echo emulatorRunStdout
for(int i = 0; i < 30; i++) {
if (!emulatorPidFinder()) {
steps.error "Emulator didn't start, check logs."
}
}
steps.sh """#!/bin/bash -xe
# Wait for Android to finish booting
bootanim=""
failcounter=0
timeout_in_sec=180
until [[ "\$bootanim" =~ "stopped" ]]; do
bootanim=`${androidHome}/platform-tools/adb -e shell getprop init.svc.bootanim 2>&1 &`
if [[ "\$bootanim" =~ "device not found" || "\$bootanim" =~ "device offline"
|| "\$bootanim" =~ "running" ]]; then
let "failcounter += 1"
echo "Waiting for emulator to start"
if [[ \$failcounter -gt timeout_in_sec ]]; then
echo "Timeout (\$timeout_in_sec seconds) reached; failed to start emulator"
exit 1
fi
fi
sleep 1
done
echo "Emulator is ready"
# Unlock the Lock Screen
${androidHome}/platform-tools/adb shell input keyevent 82
# Clear and capture logcat
${androidHome}/platform-tools/adb logcat -c
${androidHome}/platform-tools/adb logcat -v time > ${logcatFileName}.txt &
LOGCAT_PID=\$!
${androidHome}/platform-tools/adb shell settings put global animator_duration_scale 0.0
${androidHome}/platform-tools/adb shell settings put global transition_animation_scale 0.0
${androidHome}/platform-tools/adb shell settings put global window_animation_scale 0.0
"""
}
def installApk(apkFilePath) {
def absoluteapkFilePath = "${steps.pwd()}/${apkFilePath}"
steps.wrap([$class: "AnsiColorBuildWrapper", "colorMapName": "VGA"]) {
def apkInstallationStdout = steps.sh(script: """#!/bin/bash -xe
${androidHome}/platform-tools/adb install -r ${absoluteapkFilePath}
""", returnStdout: true).trim()
if (apkInstallationStdout.toLowerCase().contains("Failure".toLowerCase())) {
steps.error apkInstallationStdout
}
}
}
def getApkWildcard(apkName) {
def apkWildcard
if (apkName) {
apkWildcard = "**/${apkName}"
} else {
apkWildcard = "**/*.apk"
}
return apkWildcard
}
def getGradleTasks(buildType, gradleTasksRelease, gradleTasksDebug) {
if (buildType == "release") {
return gradleTasksRelease
} else {
return gradleTasksDebug
}
}
def getApkType(buildType, apkNameRelease, apkNameDebug) {
if (buildType == "release") {
return apkNameRelease
} else {
return apkNameDebug
}
}
def setDefaultGradleOptions() {
return "--info --full-stacktrace --profile --console=plain"
}
def setDefaultGradleTasks() {
return ""
}
def setAndroidBuildCache(workspace) {
steps.sh "echo \"\nandroid.buildCacheDir=${workspace}/.gradle/cache\" >> gradle.properties"
}
def stashGradleCache() {
steps.sh "chmod -R u+w .gradle/"
steps.stash name: "gradleCache", includes: "**/.gradle/**", useDefaultExcludes: false
}
def unstashGradleCache(useGradleCache=true) {
if (!useGradleCache || useGradleCache.toBoolean()) {
try {
steps.echo "Stage with gradle cache"
steps.unstash "gradleCache"
} catch (error) {
if (!error.toString().toLowerCase().contains("No such saved stash ‘gradleCache’".toLowerCase())) {
throw error
}
}
} else {
steps.echo "Stage without gradle cache"
}
}
def stashAndroidBuildCache() {
steps.sh "chmod -R u+w *"
steps.stash name: "androidBuildCache", includes: "**/build/**", useDefaultExcludes: false
}
def ustashAndroidBuildCache(useBuildCache=true) {
if (!useBuildCache || useBuildCache.toBoolean()) {
try {
steps.echo "Stage with build cache"
steps.unstash "androidBuildCache"
} catch (error) {
if (!error.toString().toLowerCase().contains("No such saved stash ‘androidBuildCache’".toLowerCase())) {
throw error
}
}
} else {
steps.echo "Stage without build cache"
}
}
def archieveGradleProfileReport(gradleProfileFilenameSuffix) {
def gradleProfileHtmlReportFile = steps.findFiles(glob: "**/reports/profile/**.html")
def gradleProfileHtmlReportFilename = gradleProfileHtmlReportFile[0].name
def gradleProfileHtmlReportFilenameWithoutExtension = gradleProfileHtmlReportFilename.take(gradleProfileHtmlReportFile[0].name.lastIndexOf('.'))
def gradleProfileHtmlReportFileExtension = gradleProfileHtmlReportFilename[gradleProfileHtmlReportFilename.lastIndexOf('.')..-1]
def gradleProfileHtmlReportFilePath = gradleProfileHtmlReportFile[0].path
def gradleProfileHtmlReportPath = gradleProfileHtmlReportFilePath.take(gradleProfileHtmlReportFilePath.lastIndexOf("/"))
steps.sh "mv \"${gradleProfileHtmlReportFilePath}\" \"${gradleProfileHtmlReportPath}/${gradleProfileHtmlReportFilenameWithoutExtension}-${gradleProfileFilenameSuffix}${gradleProfileHtmlReportFileExtension}\""
steps.archiveArtifacts "**/reports/profile/**"
}
def getJunitTestReportFile(junitTestReportFile) {
if (junitTestReportFile) {
return "**/${junitTestReportFile.toString()}"
} else {
return "**/TEST-*.xml"
}
}
}