@@ -231,26 +231,37 @@ fn sync_debug_application_id_suffix(
231231}
232232
233233fn set_debug_application_id_suffix ( build_gradle : & str , suffix : Option < & str > ) -> Option < String > {
234+ static BUILD_TYPES_RE : OnceLock < regex:: Regex > = OnceLock :: new ( ) ;
234235 static DEBUG_BUILD_TYPE_RE : OnceLock < regex:: Regex > = OnceLock :: new ( ) ;
235236
237+ let build_types_re = BUILD_TYPES_RE
238+ . get_or_init ( || regex:: Regex :: new ( r#"\bbuildTypes\s*\{"# ) . expect ( "valid build types regex" ) ) ;
236239 let debug_build_type_re = DEBUG_BUILD_TYPE_RE . get_or_init ( || {
237240 regex:: Regex :: new ( r#"(?m)(?:\bgetByName\(\s*"debug"\s*\)|\bdebug\b)\s*\{"# )
238241 . expect ( "valid debug build type regex" )
239242 } ) ;
240243
241- for build_type_match in debug_build_type_re. find_iter ( build_gradle) {
242- let Some ( opening_brace) = build_gradle[ build_type_match. start ( ) ..]
243- . find ( '{' )
244- . map ( |index| build_type_match. start ( ) + index)
245- else {
246- continue ;
247- } ;
244+ // only look for the debug block inside `buildTypes { ... }`,
245+ // so we do not match e.g. `signingConfigs { getByName("debug") { ... } }`
246+ let ( search_start, search_end) = build_types_re
247+ . find ( build_gradle)
248+ . and_then ( |build_types_match| {
249+ let opening_brace = build_types_match. end ( ) - 1 ;
250+ find_matching_brace ( build_gradle, opening_brace)
251+ . map ( |closing_brace| ( opening_brace + 1 , closing_brace) )
252+ } )
253+ . unwrap_or ( ( 0 , build_gradle. len ( ) ) ) ;
254+
255+ for build_type_match in debug_build_type_re. find_iter ( & build_gradle[ search_start..search_end] ) {
256+ let opening_brace = search_start + build_type_match. end ( ) - 1 ;
248257 let Some ( closing_brace) = find_matching_brace ( build_gradle, opening_brace) else {
249258 continue ;
250259 } ;
251260
261+ let closing_indentation = line_indentation ( build_gradle, opening_brace) ;
252262 let debug_block = & build_gradle[ opening_brace..closing_brace] ;
253- let updated_debug_block = set_application_id_suffix_in_block ( debug_block, suffix) ;
263+ let updated_debug_block =
264+ set_application_id_suffix_in_block ( debug_block, suffix, closing_indentation) ;
254265 let mut updated_build_gradle =
255266 String :: with_capacity ( build_gradle. len ( ) + updated_debug_block. len ( ) ) ;
256267 updated_build_gradle. push_str ( & build_gradle[ ..opening_brace] ) ;
@@ -262,7 +273,27 @@ fn set_debug_application_id_suffix(build_gradle: &str, suffix: Option<&str>) ->
262273 None
263274}
264275
265- fn set_application_id_suffix_in_block ( debug_block : & str , suffix : Option < & str > ) -> String {
276+ /// Returns the leading whitespace of the line containing `index`.
277+ fn line_indentation ( content : & str , index : usize ) -> & str {
278+ let line_start = content[ ..index] . rfind ( '\n' ) . map_or ( 0 , |i| i + 1 ) ;
279+ let line = & content[ line_start..index] ;
280+ & line[ ..line. len ( ) - line. trim_start ( ) . len ( ) ]
281+ }
282+
283+ fn set_application_id_suffix_in_block (
284+ debug_block : & str ,
285+ suffix : Option < & str > ,
286+ closing_indentation : & str ,
287+ ) -> String {
288+ // a single-line block such as `getByName("debug") { isDebuggable = true }`
289+ if !debug_block. contains ( '\n' ) {
290+ return set_application_id_suffix_in_single_line_block (
291+ debug_block,
292+ suffix,
293+ closing_indentation,
294+ ) ;
295+ }
296+
266297 static APPLICATION_ID_SUFFIX_RE : OnceLock < regex:: Regex > = OnceLock :: new ( ) ;
267298
268299 let application_id_suffix_re = APPLICATION_ID_SUFFIX_RE . get_or_init ( || {
@@ -298,16 +329,87 @@ fn set_application_id_suffix_in_block(debug_block: &str, suffix: Option<&str>) -
298329 escape_kotlin_string( suffix)
299330 ) ;
300331
301- if let Some ( first_newline) = debug_block. find ( '\n' ) {
302- let mut updated_debug_block =
303- String :: with_capacity ( debug_block. len ( ) + application_id_suffix. len ( ) ) ;
304- updated_debug_block. push_str ( & debug_block[ ..=first_newline] ) ;
305- updated_debug_block. push_str ( & application_id_suffix) ;
306- updated_debug_block. push_str ( & debug_block[ first_newline + 1 ..] ) ;
307- updated_debug_block
308- } else {
309- format ! ( "{{\n {application_id_suffix}" )
332+ // the block is known to span multiple lines at this point
333+ let first_newline = debug_block. find ( '\n' ) . unwrap_or ( debug_block. len ( ) - 1 ) ;
334+ let mut updated_debug_block =
335+ String :: with_capacity ( debug_block. len ( ) + application_id_suffix. len ( ) ) ;
336+ updated_debug_block. push_str ( & debug_block[ ..=first_newline] ) ;
337+ updated_debug_block. push_str ( & application_id_suffix) ;
338+ updated_debug_block. push_str ( & debug_block[ first_newline + 1 ..] ) ;
339+ updated_debug_block
340+ }
341+
342+ /// Rewrites a single-line block (`{ a = 1; b = 2 `, without the closing brace)
343+ /// into a multi-line block with the `applicationIdSuffix` set, keeping the other statements.
344+ fn set_application_id_suffix_in_single_line_block (
345+ debug_block : & str ,
346+ suffix : Option < & str > ,
347+ closing_indentation : & str ,
348+ ) -> String {
349+ static APPLICATION_ID_SUFFIX_STATEMENT_RE : OnceLock < regex:: Regex > = OnceLock :: new ( ) ;
350+
351+ let application_id_suffix_statement_re = APPLICATION_ID_SUFFIX_STATEMENT_RE . get_or_init ( || {
352+ regex:: Regex :: new ( r#"^applicationIdSuffix\s*="# )
353+ . expect ( "valid applicationIdSuffix statement regex" )
354+ } ) ;
355+
356+ let statements = split_kotlin_statements ( & debug_block[ 1 ..] ) ;
357+ let has_application_id_suffix = statements
358+ . iter ( )
359+ . any ( |statement| application_id_suffix_statement_re. is_match ( statement) ) ;
360+
361+ if suffix. is_none ( ) && !has_application_id_suffix {
362+ return debug_block. to_string ( ) ;
363+ }
364+
365+ let indentation = format ! ( "{closing_indentation} " ) ;
366+ let mut updated_debug_block = String :: from ( "{\n " ) ;
367+ if let Some ( suffix) = suffix {
368+ updated_debug_block. push_str ( & format ! (
369+ "{indentation}applicationIdSuffix = \" {}\" \n " ,
370+ escape_kotlin_string( suffix)
371+ ) ) ;
372+ }
373+ for statement in statements
374+ . iter ( )
375+ . filter ( |statement| !application_id_suffix_statement_re. is_match ( statement) )
376+ {
377+ updated_debug_block. push_str ( & format ! ( "{indentation}{statement}\n " ) ) ;
378+ }
379+ updated_debug_block. push_str ( closing_indentation) ;
380+ updated_debug_block
381+ }
382+
383+ /// Splits Kotlin statements separated by `;`, ignoring separators inside string literals.
384+ fn split_kotlin_statements ( content : & str ) -> Vec < & str > {
385+ let mut statements = Vec :: new ( ) ;
386+ let mut in_string = false ;
387+ let mut escaped = false ;
388+ let mut start = 0 ;
389+
390+ for ( index, character) in content. char_indices ( ) {
391+ if in_string {
392+ if escaped {
393+ escaped = false ;
394+ } else if character == '\\' {
395+ escaped = true ;
396+ } else if character == '"' {
397+ in_string = false ;
398+ }
399+ } else if character == '"' {
400+ in_string = true ;
401+ } else if character == ';' {
402+ statements. push ( & content[ start..index] ) ;
403+ start = index + 1 ;
404+ }
310405 }
406+ statements. push ( & content[ start..] ) ;
407+
408+ statements
409+ . into_iter ( )
410+ . map ( str:: trim)
411+ . filter ( |statement| !statement. is_empty ( ) )
412+ . collect ( )
311413}
312414
313415fn debug_block_indentation ( debug_block : & str ) -> & str {
@@ -1194,6 +1296,88 @@ android {
11941296 ) ) ;
11951297 }
11961298
1299+ #[ test]
1300+ fn skips_debug_signing_config_before_build_types ( ) {
1301+ let build_gradle = r#"
1302+ android {
1303+ signingConfigs {
1304+ getByName("debug") {
1305+ storeFile = file("debug.keystore")
1306+ }
1307+ }
1308+ buildTypes {
1309+ getByName("debug") {
1310+ isDebuggable = true
1311+ }
1312+ }
1313+ }
1314+ "# ;
1315+
1316+ let updated = set_debug_application_id_suffix ( build_gradle, Some ( ".debug" ) ) . unwrap ( ) ;
1317+
1318+ assert ! ( updated. contains(
1319+ r#" getByName("debug") {
1320+ storeFile = file("debug.keystore")
1321+ }"#
1322+ ) ) ;
1323+ assert ! ( updated. contains(
1324+ r#" getByName("debug") {
1325+ applicationIdSuffix = ".debug"
1326+ isDebuggable = true
1327+ }"#
1328+ ) ) ;
1329+ }
1330+
1331+ #[ test]
1332+ fn keeps_single_line_debug_block_content ( ) {
1333+ let build_gradle = r#"
1334+ android {
1335+ buildTypes {
1336+ getByName("debug") { isDebuggable = true; manifestPlaceholders["a"] = "b;c" }
1337+ }
1338+ }
1339+ "# ;
1340+
1341+ let updated = set_debug_application_id_suffix ( build_gradle, Some ( ".debug" ) ) . unwrap ( ) ;
1342+
1343+ assert_eq ! (
1344+ updated,
1345+ r#"
1346+ android {
1347+ buildTypes {
1348+ getByName("debug") {
1349+ applicationIdSuffix = ".debug"
1350+ isDebuggable = true
1351+ manifestPlaceholders["a"] = "b;c"
1352+ }
1353+ }
1354+ }
1355+ "#
1356+ ) ;
1357+
1358+ let removed = set_debug_application_id_suffix (
1359+ r#"buildTypes { debug { applicationIdSuffix = ".old"; isDebuggable = true } }"# ,
1360+ None ,
1361+ )
1362+ . unwrap ( ) ;
1363+ assert_eq ! (
1364+ removed,
1365+ "buildTypes { debug {\n isDebuggable = true\n } }"
1366+ ) ;
1367+ }
1368+
1369+ #[ test]
1370+ fn writes_suffix_to_empty_single_line_debug_block ( ) {
1371+ let build_gradle = " buildTypes {\n debug {}\n }\n " ;
1372+
1373+ let updated = set_debug_application_id_suffix ( build_gradle, Some ( ".debug" ) ) . unwrap ( ) ;
1374+
1375+ assert_eq ! (
1376+ updated,
1377+ " buildTypes {\n debug {\n applicationIdSuffix = \" .debug\" \n }\n }\n "
1378+ ) ;
1379+ }
1380+
11971381 #[ test]
11981382 fn ignores_braces_inside_kotlin_raw_strings ( ) {
11991383 let build_gradle = r#"
0 commit comments