@@ -36,27 +36,7 @@ public string? AvdManagerPath {
3636 if ( string . IsNullOrEmpty ( sdkPath ) )
3737 return null ;
3838
39- var ext = OS . IsWindows ? ".bat" : "" ;
40- var cmdlineToolsDir = Path . Combine ( sdkPath , "cmdline-tools" ) ;
41-
42- if ( Directory . Exists ( cmdlineToolsDir ) ) {
43- // Versioned dirs sorted descending, then "latest" as fallback
44- var searchDirs = Directory . GetDirectories ( cmdlineToolsDir )
45- . Select ( Path . GetFileName )
46- . Where ( n => n != "latest" && ! string . IsNullOrEmpty ( n ) )
47- . OrderByDescending ( n => Version . TryParse ( n , out var v ) ? v : new Version ( 0 , 0 ) )
48- . Append ( "latest" ) ;
49-
50- foreach ( var dir in searchDirs ) {
51- var toolPath = Path . Combine ( cmdlineToolsDir , dir ! , "bin" , "avdmanager" + ext ) ;
52- if ( File . Exists ( toolPath ) )
53- return toolPath ;
54- }
55- }
56-
57- // Legacy fallback: tools/bin/avdmanager
58- var legacyPath = Path . Combine ( sdkPath , "tools" , "bin" , "avdmanager" + ext ) ;
59- return File . Exists ( legacyPath ) ? legacyPath : null ;
39+ return ProcessUtils . FindCmdlineTool ( sdkPath , "avdmanager" , OS . IsWindows ? ".bat" : "" ) ;
6040 }
6141 }
6242
@@ -82,23 +62,16 @@ public async Task<IReadOnlyList<AvdInfo>> ListAvdsAsync (CancellationToken cance
8262 ConfigureEnvironment ( psi ) ;
8363 var exitCode = await ProcessUtils . StartProcess ( psi , stdout , stderr , cancellationToken ) . ConfigureAwait ( false ) ;
8464
85- if ( exitCode != 0 )
86- throw new InvalidOperationException ( $ "avdmanager list avd failed (exit code { exitCode } ): { stderr . ToString ( ) . Trim ( ) } ") ;
65+ ProcessUtils . ThrowIfFailed ( exitCode , "avdmanager list avd" , stderr . ToString ( ) ) ;
8766
8867 return ParseAvdListOutput ( stdout . ToString ( ) ) ;
8968 }
9069
9170 public async Task < AvdInfo > CreateAvdAsync ( string name , string systemImage , string ? deviceProfile = null ,
9271 bool force = false , CancellationToken cancellationToken = default )
9372 {
94- if ( name is null )
95- throw new ArgumentNullException ( nameof ( name ) ) ;
96- if ( name . Length == 0 )
97- throw new ArgumentException ( "Value cannot be an empty string." , nameof ( name ) ) ;
98- if ( systemImage is null )
99- throw new ArgumentNullException ( nameof ( systemImage ) ) ;
100- if ( systemImage . Length == 0 )
101- throw new ArgumentException ( "Value cannot be an empty string." , nameof ( systemImage ) ) ;
73+ ProcessUtils . ValidateNotNullOrEmpty ( name , nameof ( name ) ) ;
74+ ProcessUtils . ValidateNotNullOrEmpty ( systemImage , nameof ( systemImage ) ) ;
10275
10376 var avdManagerPath = RequireAvdManagerPath ( ) ;
10477
@@ -138,12 +111,7 @@ public async Task<AvdInfo> CreateAvdAsync (string name, string systemImage, stri
138111 }
139112 } ) . ConfigureAwait ( false ) ;
140113
141- if ( exitCode != 0 ) {
142- var errorOutput = stderr . ToString ( ) . Trim ( ) ;
143- if ( string . IsNullOrEmpty ( errorOutput ) )
144- errorOutput = stdout . ToString ( ) . Trim ( ) ;
145- throw new InvalidOperationException ( $ "Failed to create AVD '{ name } ': { errorOutput } ") ;
146- }
114+ ProcessUtils . ThrowIfFailed ( exitCode , $ "avdmanager create avd -n { name } ", stderr . ToString ( ) , stdout . ToString ( ) ) ;
147115
148116 // Re-list to get the actual path from avdmanager (respects ANDROID_USER_HOME/ANDROID_AVD_HOME)
149117 var avds = await ListAvdsAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -161,10 +129,7 @@ public async Task<AvdInfo> CreateAvdAsync (string name, string systemImage, stri
161129
162130 public async Task DeleteAvdAsync ( string name , CancellationToken cancellationToken = default )
163131 {
164- if ( name is null )
165- throw new ArgumentNullException ( nameof ( name ) ) ;
166- if ( name . Length == 0 )
167- throw new ArgumentException ( "Value cannot be an empty string." , nameof ( name ) ) ;
132+ ProcessUtils . ValidateNotNullOrEmpty ( name , nameof ( name ) ) ;
168133
169134 var avdManagerPath = RequireAvdManagerPath ( ) ;
170135
@@ -173,8 +138,7 @@ public async Task DeleteAvdAsync (string name, CancellationToken cancellationTok
173138 ConfigureEnvironment ( psi ) ;
174139 var exitCode = await ProcessUtils . StartProcess ( psi , null , stderr , cancellationToken ) . ConfigureAwait ( false ) ;
175140
176- if ( exitCode != 0 )
177- throw new InvalidOperationException ( $ "Failed to delete AVD '{ name } ': { stderr . ToString ( ) . Trim ( ) } ") ;
141+ ProcessUtils . ThrowIfFailed ( exitCode , $ "avdmanager delete avd --name { name } ", stderr . ToString ( ) ) ;
178142 }
179143
180144 internal static List < AvdInfo > ParseAvdListOutput ( string output )
0 commit comments