1
- #addin nuget : ? package= Cake . Android . Adb & version = 3.2 .0
2
- #addin nuget: ? package = Cake . Android . AvdManager & version = 2.2 .0
3
-
4
1
DirectoryPath ROOT_PATH = MakeAbsolute ( Directory ( "../.." ) ) ;
5
2
6
3
#load "shared.cake"
@@ -27,46 +24,7 @@ if (string.IsNullOrEmpty(TEST_RESULTS)) {
27
24
Information ( "Test Results Directory: {0}" , TEST_RESULTS ) ;
28
25
CleanDir ( TEST_RESULTS ) ;
29
26
30
- // set up env
31
- var ANDROID_SDK_ROOT = Argument( "android", EnvironmentVariable(" ANDROID_SDK_ROOT") ?? EnvironmentVariable ( "ANDROID_SDK_HOME" ) ) ;
32
- if ( string . IsNullOrEmpty ( ANDROID_SDK_ROOT ) ) {
33
- throw new Exception ( "Environment variable 'ANDROID_SDK_ROOT' must be set to the Android SDK root." ) ;
34
- }
35
- System . Environment . SetEnvironmentVariable ( "PATH" ,
36
- $ "{ ANDROID_SDK_ROOT } /cmdline-tools/latest/bin" + System . IO . Path . PathSeparator +
37
- $ "{ ANDROID_SDK_ROOT } /platform-tools" + System . IO . Path . PathSeparator +
38
- $ "{ ANDROID_SDK_ROOT } /emulator" + System . IO . Path . PathSeparator +
39
- $ "{ ANDROID_SDK_ROOT } /tools/bin" + System . IO . Path . PathSeparator +
40
- EnvironmentVariable ( "PATH" ) ) ;
41
-
42
- Information( "Android SDK Root: {0}" , ANDROID_SDK_ROOT ) ;
43
- Information( "PATH: {0}" , EnvironmentVariable ( "PATH" ) ) ;
44
-
45
- var bat = IsRunningOnWindows( ) ? ".bat" : "" ;
46
- var exe = IsRunningOnWindows( ) ? ".exe" : "" ;
47
- var avdSettings = new AndroidAvdManagerToolSettings {
48
- SdkRoot = ANDROID_SDK_ROOT ,
49
- ToolPath = $ "{ ANDROID_SDK_ROOT } /cmdline-tools/latest/bin/avdmanager{ bat } "
50
- } ;
51
- var adbSettings = new AdbToolSettings {
52
- SdkRoot = ANDROID_SDK_ROOT ,
53
- ToolPath = $ "{ ANDROID_SDK_ROOT } /platform-tools/adb{ exe } "
54
- } ;
55
- var emuSettings = new AndroidEmulatorToolSettings {
56
- SdkRoot = ANDROID_SDK_ROOT ,
57
- ToolPath = $ "{ ANDROID_SDK_ROOT } /emulator/emulator{ exe } ",
58
- Verbose = true ,
59
- ArgumentCustomization = args => args . Append (
60
- "-no-boot-anim " +
61
- "-no-snapshot " +
62
- "-gpu host " +
63
- "-no-audio " +
64
- "-camera-back none " +
65
- "-camera-front none " +
66
- "-qemu -m 2048" )
67
- } ;
68
-
69
- AndroidEmulatorProcess emulatorProcess = null ;
27
+ var usingEmulator = true ;
70
28
71
29
Setup ( context =>
72
30
{
@@ -80,7 +38,6 @@ Setup(context =>
80
38
// determine the device characteristics
81
39
{
82
40
var working = TEST_DEVICE . Trim ( ) . ToLower ( ) ;
83
- var emulator = true ;
84
41
var api = 34 ;
85
42
// version
86
43
if ( working . IndexOf ( "_" ) is int idx && idx > 0 ) {
@@ -93,81 +50,59 @@ Setup(context =>
93
50
throw new Exception ( "Unexpected platform (expected: android) in device: " + TEST_DEVICE ) ;
94
51
// device/emulator
95
52
if ( parts [ 1 ] == "device" )
96
- emulator = false ;
53
+ usingEmulator = false ;
97
54
else if ( parts [ 1 ] != "emulator" && parts [ 1 ] != "simulator" )
98
55
throw new Exception ( "Unexpected device type (expected: device|emulator) in device: " + TEST_DEVICE ) ;
99
56
// arch/bits
100
57
if ( parts [ 2 ] == "32" ) {
101
- if ( emulator )
58
+ if ( usingEmulator )
102
59
DEVICE_ARCH = "x86" ;
103
60
else
104
61
DEVICE_ARCH = "armeabi-v7a" ;
105
62
} else if ( parts [ 2 ] == "64" ) {
106
63
if ( RuntimeInformation . ProcessArchitecture == System . Runtime . InteropServices . Architecture . Arm64 )
107
64
DEVICE_ARCH = "arm64-v8a" ;
108
- else if ( emulator )
65
+ else if ( usingEmulator )
109
66
DEVICE_ARCH = "x86_64" ;
110
67
else
111
68
DEVICE_ARCH = "arm64-v8a" ;
112
69
}
113
70
DEVICE_ID = $ "system-images;android-{ api } ;google_apis;{ DEVICE_ARCH } ";
71
+ }
114
72
115
- // we are not using a virtual device, so quit
116
- if ( ! emulator )
117
- return ;
73
+ // we are not using a virtual device, so quit
74
+ if ( ! usingEmulator ) {
75
+ Information ( "Using a physical device:" ) ;
76
+ DotNetTool ( "android device list" ) ;
77
+ return ;
118
78
}
119
79
120
80
Information ( "Test Device ID: {0}" , DEVICE_ID ) ;
121
81
122
- // delete the AVD first, if it exists
123
- Information ( "Deleting AVD if exists: {0}..." , ANDROID_AVD ) ;
124
- try { AndroidAvdDelete ( ANDROID_AVD , avdSettings ) ; }
125
- catch { }
126
-
127
82
// create the new AVD
128
83
Information ( "Creating AVD: {0}..." , ANDROID_AVD ) ;
129
- AndroidAvdCreate ( ANDROID_AVD , DEVICE_ID , DEVICE_NAME , force : true , settings : avdSettings ) ;
84
+ DotNetTool ( $ "android avd create --name \" { ANDROID_AVD } \" --sdk \" { DEVICE_ID } \" --device \" { DEVICE_NAME } \" -- force" ) ;
130
85
131
- // start the emulator
86
+ // start the emulator (only wait 5 mins)
132
87
Information ( "Starting Emulator: {0}..." , ANDROID_AVD ) ;
133
- emulatorProcess = AndroidEmulatorStart ( ANDROID_AVD , emuSettings ) ;
134
-
135
- // wait for it to finish booting (4 mins)
136
- var waited = 0 ;
137
- var interval = 10 ;
138
- var totalMins = 4 ;
139
- var total = 60 * totalMins / interval ;
140
- while ( AdbShell ( "getprop sys.boot_completed" , adbSettings ) . FirstOrDefault ( ) != "1" ) {
141
- TakeSnapshot ( TEST_RESULTS , $ "boot-{ waited : 000} ") ;
142
- System . Threading . Thread . Sleep ( interval * 1000 ) ;
143
- Information ( "Wating {0}/{1} seconds for the emulator to boot up." , waited * interval , total ) ;
144
- if ( waited ++ > total )
145
- break ;
146
- }
88
+ DotNetTool ( $ "android avd start --name \" { ANDROID_AVD } \" --gpu guest --wait-boot --no-window --no-snapshot --no-audio --no-boot-anim --camera-back none --camera-front none --timeout 300") ;
89
+
90
+ // show running emulator information
91
+ Information ( "Emulator started:" ) ;
92
+ DotNetTool ( "android device list" ) ;
147
93
TakeSnapshot ( TEST_RESULTS , "boot-complete" ) ;
148
- Information ( "Waited {0} seconds for the emulator to boot up." , waited ) ;
149
94
} ) ;
150
95
151
96
Teardown ( context =>
152
97
{
153
- TakeSnapshot ( TEST_RESULTS , "teardown" ) ;
154
-
155
98
// no virtual device was used
156
- if ( emulatorProcess == null )
99
+ if ( ! usingEmulator )
157
100
return ;
158
101
159
- // stop and cleanup the emulator
160
- AdbEmuKill ( adbSettings ) ;
161
-
162
- System . Threading . Thread . Sleep ( 5000 ) ;
163
-
164
- // kill the process if it has not already exited
165
- try { emulatorProcess . Kill ( ) ; }
166
- catch { }
102
+ TakeSnapshot ( TEST_RESULTS , "teardown" ) ;
167
103
168
- // delete the AVD
169
- try { AndroidAvdDelete ( ANDROID_AVD , avdSettings ) ; }
170
- catch { }
104
+ // cleanup the emulator
105
+ DotNetTool ( $ "android avd delete --name \" { ANDROID_AVD } \" ") ;
171
106
} ) ;
172
107
173
108
Task ( "Default" )
0 commit comments