@@ -81,11 +81,7 @@ pub fn (o &Options) validate() ! {
81
81
if o.avd == '' {
82
82
return error ('${@MOD} .${@STRUCT} .${@FN} : No Android Virtual Device (avd) sat' )
83
83
}
84
- avdmanager := env.avdmanager ()
85
- avdmanager_list_cmd := [avdmanager, 'list' , 'avd' , '-c' ]
86
- util.verbosity_print_cmd (avdmanager_list_cmd, o.verbosity)
87
- avdmanager_list := util.run_or_error (avdmanager_list_cmd)!
88
- avds := avdmanager_list.split ('\n ' )
84
+ avds := Emulator.list_avds ()!
89
85
if o.avd ! in avds {
90
86
return error ('${@MOD} .${@STRUCT} .${@FN} : Android Virtual Device (avd) "${o.avd} " not found.' )
91
87
}
@@ -128,6 +124,55 @@ pub fn (mut e Emulator) start(options Options) ! {
128
124
}
129
125
}
130
126
127
+ // has_avd returns `true` if `avd_name` can be found. Use `list_avds` to see all locations of AVD's
128
+ pub fn Emulator .has_avd (avd_name string ) bool {
129
+ avds := Emulator.list_avds () or { return false }
130
+ return avd_name in avds.keys ()
131
+ }
132
+
133
+ // list_avds returns a list of devices detected by running `emulator -list-avds`
134
+ // NOTE: for Google reasons, this list can be different from `avdmanager list avd -c`...
135
+ pub fn Emulator .list_avds () ! map [string ]string {
136
+ emulator_exe := env.emulator ()
137
+ list_cmd := [emulator_exe, '-list-avds' ]
138
+ list_res := util.run_or_error (list_cmd)!
139
+ list := list_res.split ('\n ' ).filter (it != '' ).filter (! it .contains (' ' ))
140
+ mut m := map [string ]string {}
141
+ for entry in list {
142
+ m[entry] = entry // TODO: should be a path to the AVD...
143
+ }
144
+ return m
145
+ // TODO: find out how to fix this dumb mess for users
146
+ // if vab_test_avd !in avds {
147
+ // Locating a deterministic location of AVD's has, like so many other Android related things, become a mess.
148
+ // (`avdmanager` can put them in places that the `emulator` does not pickup on the *same* host etc... Typical Google-mess)
149
+ // ... even passing `--path` to `avdmanager` does not work.
150
+ // Here we try a few places and set `ANDROID_AVD_HOME` to make runs a bit more predictable.
151
+ // mut avd_home := os.join_path(os.home_dir(), '.android', 'avd')
152
+ // eprintln('warning: "${vab_test_avd}" still not in list: ${avds}... trying new location "${avd_home}"')
153
+ // os.setenv('ANDROID_AVD_HOME', avd_home, true)
154
+ //
155
+ // avds = emulator.Emulator.list_avds() or {
156
+ // eprintln('${exe_name} error: ${err}')
157
+ // exit(1)
158
+ // }
159
+ // if vab_test_avd !in avds {
160
+ // config_dir := os.config_dir() or {
161
+ // eprintln('${exe_name} error: ${err}')
162
+ // exit(1)
163
+ // }
164
+ // avd_home = os.join_path(config_dir, '.android', 'avd')
165
+ // eprintln('warning: "${vab_test_avd}" still not in list: ${avds}... trying new location "${avd_home}"')
166
+ // os.setenv('ANDROID_AVD_HOME', avd_home, true)
167
+ //
168
+ // avds = emulator.Emulator.list_avds() or {
169
+ // eprintln('${exe_name} error: ${err}')
170
+ // exit(1)
171
+ // }
172
+ // }
173
+ // }
174
+ }
175
+
131
176
// wait_for_boot blocks execution and waits for the emulator to boot.
132
177
// NOTE: this feature is unique to emulator devices.
133
178
pub fn (mut e Emulator) wait_for_boot () ! {
0 commit comments