Repository navigation
Expand file tree
/
Copy pathdeploy.cmd
More file actions
279 lines (255 loc) · 8.92 KB
/
Copy pathdeploy.cmd
File metadata and controls
279 lines (255 loc) · 8.92 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@echo off
setlocal EnableDelayedExpansion
rem Deploy an app on the kit to a phone; the app's own android\deploy.cmd
rem hands over to this. The package and the logcat tags to follow come
rem from the app's kit.properties.
rem
rem deploy.cmd build, install, launch, then tail the log
rem deploy.cmd find show phones advertising wireless debugging
rem deploy.cmd pair CODE pair, discovering the address over mDNS
rem deploy.cmd pair HOST:PORT CODE pair with an address typed by hand
rem deploy.cmd connect connect, discovering the address
rem deploy.cmd connect HOST:PORT connect to an address typed by hand
rem deploy.cmd log tail the app's log only
rem deploy.cmd devices what adb can see
rem deploy.cmd apk just print the APK path (for a manual copy)
rem
rem A phone paired for one app is paired for all of them: pairing belongs
rem to the PC's adb key, not to the app.
call "%~dp0toolchain.cmd" || exit /b 1
set "PKG=%KIT_APPID%"
if not defined PKG (
echo.
echo No appId in %PROJECT%\kit.properties - add a line like:
echo appId=com.example.app
echo.
exit /b 1
)
cd /d "%PROJECT%"
if /i "%~1"=="pair" goto :do_pair
if /i "%~1"=="connect" goto :do_connect
if /i "%~1"=="find" goto :do_find
if /i "%~1"=="log" goto :tail
if /i "%~1"=="devices" (
"%ADB%" devices -l
exit /b %ERRORLEVEL%
)
if /i "%~1"=="apk" (
if not exist "%APK%" call :build || exit /b 1
echo %APK%
exit /b 0
)
rem ---- default: build, install, launch, tail -----------------------------
call :build || exit /b 1
echo.
echo === checking for a device
call :pick_device
if defined SERIAL echo using !SERIAL!
if not defined SERIAL (
if defined UNAUTHORIZED (
echo.
echo Found !UNAUTHORIZED! but it has not authorised this PC.
echo Unlock the phone and accept the 'Allow USB debugging' prompt.
echo.
exit /b 1
)
echo.
echo No device. Either:
echo.
echo Wireless - Developer options ^> Wireless debugging ^> on.
echo Open 'Pair device with pairing code', then:
echo deploy.cmd pair 123456 ^(the phone's code^)
echo deploy.cmd connect
echo Addresses are discovered for you. Pairing is once
echo per phone; connect again after a reboot.
echo.
echo USB cable - enable Developer options then USB debugging,
echo plug in, and accept the RSA prompt on the phone.
echo.
echo Sideload - copy this file to the phone and tap it:
echo %APK%
echo.
exit /b 1
)
echo.
echo === installing
rem -r reinstall, -t allow test builds, -g pre-grant runtime permissions, so
rem a dev install does not stop to ask for the microphone or location.
"%ADB%" -s !SERIAL! install -r -t -g "%APK%"
if errorlevel 1 (
echo.
echo Install failed. If it says INSTALL_FAILED_UPDATE_INCOMPATIBLE the
echo phone has a copy signed with a different debug key:
echo "%ADB%" uninstall %PKG%
echo.
exit /b 1
)
echo.
echo === launching
"%ADB%" -s !SERIAL! shell monkey -p %PKG% -c android.intent.category.LAUNCHER 1 >nul 2>&1
goto :tail_with_serial
:tail
call :pick_device
if not defined SERIAL (
echo No device to read logs from. Run: deploy.cmd connect
exit /b 1
)
:tail_with_serial
echo.
echo === logcat ^(Ctrl+C to stop^)
"%ADB%" -s !SERIAL! logcat -c
"%ADB%" -s !SERIAL! logcat -s %KIT_LOGTAGS%
exit /b %ERRORLEVEL%
:build
echo === building
call "%KIT%build.cmd" assembleDebug
exit /b %ERRORLEVEL%
rem ---- wireless debugging ------------------------------------------------
rem
rem Phones advertise two mDNS services and they are NOT interchangeable:
rem
rem _adb-tls-pairing present only while the pairing popup is open -> pair
rem _adb-tls-connect present whenever wireless debugging is on -> connect
rem
rem They sit on different ports, and handing adb the wrong one fails with
rem "protocol fault (couldn't read status message)", which names neither the
rem cause nor the fix. So the address is discovered rather than typed, and
rem using the wrong service is detected and explained instead of being
rem documented and hoped about.
:do_find
echo.
echo === phones advertising wireless debugging
"%ADB%" mdns services
call :lookup pairing PAIRADDR
call :lookup connect CONNADDR
echo.
if defined PAIRADDR (
echo pairing popup open at !PAIRADDR!
echo run: deploy.cmd pair CODE ^(the 6 digits on the popup^)
) else (
echo No pairing service. That is normal unless the
echo 'Pair device with pairing code' popup is open right now.
)
if defined CONNADDR (
echo wireless debugging at !CONNADDR!
echo run: deploy.cmd connect ^(only works once paired^)
)
if not defined CONNADDR if not defined PAIRADDR (
echo Nothing found. The phone must be on the SAME Wi-Fi network with
echo Wireless debugging switched on. Some networks block mDNS between
echo clients; if so, read the address off the phone and pass it in.
)
echo.
exit /b 0
:do_pair
set "ARG2=%~2"
set "CODE=%~3"
rem One argument is the code; find the address ourselves.
if "%CODE%"=="" (
set "CODE=%ARG2%"
set "ARG2="
)
if "%CODE%"=="" (
echo.
echo usage: deploy.cmd pair CODE
echo The code is the 6 digits shown by 'Pair device with pairing code'.
echo.
exit /b 1
)
call :lookup pairing PAIRADDR
call :lookup connect CONNADDR
if not "%ARG2%"=="" (
rem Catch the common mistake: the address off the main Wireless
rem debugging screen, which is the connect service, not pairing.
if "%ARG2%"=="!CONNADDR!" (
echo.
echo !ARG2! is the CONNECT service, not the pairing one.
echo They are different ports. Pairing is advertised only while the
echo 'Pair device with pairing code' popup is open.
if defined PAIRADDR echo Pairing is currently at !PAIRADDR! - just run: deploy.cmd pair %CODE%
echo.
exit /b 1
)
set "PAIRADDR=%ARG2%"
)
if not defined PAIRADDR (
echo.
echo No pairing service found.
echo On the phone: Developer options ^> Wireless debugging ^>
echo 'Pair device with pairing code'. Leave that popup ON SCREEN -
echo the pairing service exists only while it is open - then re-run:
echo deploy.cmd pair %CODE%
echo.
exit /b 1
)
echo pairing with !PAIRADDR!
"%ADB%" pair !PAIRADDR! %CODE%
set "RC=!ERRORLEVEL!"
if not "!RC!"=="0" (
echo.
echo Pairing failed. Usual causes, in order:
echo - the popup was dismissed ^(the code and port both expire with it^)
echo - the code was mistyped, or came from a previous popup
echo - PC and phone are on different networks or VLANs
echo.
exit /b !RC!
)
echo.
echo Paired. Now run: deploy.cmd connect
exit /b 0
:do_connect
set "CONNADDR=%~2"
if "%CONNADDR%"=="" call :lookup connect CONNADDR
if not defined CONNADDR (
echo.
echo No device advertising wireless debugging.
echo Turn on Developer options ^> Wireless debugging, or pass the
echo address from that screen: deploy.cmd connect HOST:PORT
echo.
exit /b 1
)
echo connecting to !CONNADDR!
"%ADB%" connect !CONNADDR!
rem adb connect exits 0 even when it prints "failed to connect", so ask adb
rem what it actually has. Not get-state though: adb usually ALSO auto-
rem connects to the same phone over mDNS, so the normal state after a
rem successful connect is two transports to one handset, and get-state
rem answers "more than one device/emulator" - failure-shaped output for a
rem working setup.
call :pick_device
if not defined SERIAL (
echo.
echo Not connected. If this phone has never been paired with this PC,
echo pair first - connect alone is not enough:
echo deploy.cmd pair CODE
echo.
exit /b 1
)
echo connected as !SERIAL!. Now run: deploy.cmd
exit /b 0
rem :pick_device - set SERIAL to the first usable device, or UNAUTHORIZED to
rem a device that is refusing us. `adb devices` prints "<serial> <state>",
rem with a header line that tokenises harmlessly.
rem
rem One phone can legitimately appear more than once - an explicit
rem connect and adb's own mDNS auto-connect are separate transports to the
rem same handset - so every later adb call is pinned with -s rather than
rem left to guess.
:pick_device
set "SERIAL="
set "UNAUTHORIZED="
for /f "tokens=1,2" %%a in ('"%ADB%" devices 2^>nul') do (
if "%%b"=="device" if not defined SERIAL set "SERIAL=%%a"
if "%%b"=="unauthorized" if not defined UNAUTHORIZED set "UNAUTHORIZED=%%a"
)
exit /b 0
rem :lookup <pairing^|connect> <varname>
rem Reads `adb mdns services`, whose columns are:
rem <instance> _adb-tls-<kind>._tcp <host:port>
:lookup
set "_want=_adb-tls-%~1._tcp"
set "%~2="
for /f "tokens=1,2,3" %%a in ('"%ADB%" mdns services 2^>nul') do (
if "%%b"=="!_want!" if not defined %~2 set "%~2=%%c"
)
exit /b 0