|
428 | 428 | ensure_gradle_package_config() { |
429 | 429 | python3 - "$APP_BUILD_GRADLE" "$PACKAGE_NAME" <<'PY' |
430 | 430 | import sys |
431 | | -from pathlib import Path |
432 | 431 | import re |
| 432 | +from pathlib import Path |
433 | 433 |
|
434 | 434 | path = Path(sys.argv[1]) |
435 | 435 | package_name = sys.argv[2] |
436 | 436 | text = path.read_text() |
437 | 437 | original = text |
438 | 438 | messages = [] |
439 | 439 |
|
| 440 | +def ensure_application_plugin(source: str) -> str: |
| 441 | + plugin_id = "com.android.application" |
| 442 | + if plugin_id in source: |
| 443 | + return source |
| 444 | + if "plugins" in source: |
| 445 | + updated = re.sub(r"(plugins\s*\{)", r"\1\n id \"%s\"" % plugin_id, source, count=1) |
| 446 | + if updated != source: |
| 447 | + messages.append(f"Applied {plugin_id} via plugins block") |
| 448 | + return updated |
| 449 | + messages.append(f"Applied {plugin_id} via legacy apply plugin syntax") |
| 450 | + return f"apply plugin: \"{plugin_id}\"\n" + source |
| 451 | +
|
| 452 | +def ensure_android_block(source: str) -> str: |
| 453 | + if re.search(r"android\s*\{", source): |
| 454 | + return source |
| 455 | + messages.append("Inserted android block") |
| 456 | + return source + "\nandroid {\n}\n" |
| 457 | +
|
440 | 458 | def ensure_namespace(source: str) -> str: |
441 | | - pattern = re.compile(r'\bnamespace\s+["\']([^"\']+)["\']') |
| 459 | + pattern = re.compile(r"\bnamespace\s+[\"']([^\"']+)[\"']") |
442 | 460 | match = pattern.search(source) |
443 | 461 | if match: |
444 | 462 | if match.group(1) != package_name: |
445 | 463 | start, end = match.span() |
446 | | - replacement = f'namespace "{package_name}"' |
447 | | - source = source[:start] + replacement + source[end:] |
| 464 | + source = source[:start] + f"namespace \"{package_name}\"" + source[end:] |
448 | 465 | messages.append(f"Updated namespace to {package_name}") |
449 | | - else: |
450 | | - android_match = re.search(r'android\s*\{', source) |
451 | | - if not android_match: |
452 | | - sys.exit("Unable to locate android block when inserting namespace") |
453 | | - insert = android_match.end() |
454 | | - insertion = f"\n namespace \"{package_name}\"" |
455 | | - source = source[:insert] + insertion + source[insert:] |
456 | | - messages.append(f"Inserted namespace {package_name}") |
| 466 | + return source |
| 467 | + android_match = re.search(r"android\s*\{", source) |
| 468 | + if not android_match: |
| 469 | + raise SystemExit("Unable to locate android block when inserting namespace") |
| 470 | + insert = android_match.end() |
| 471 | + source = source[:insert] + f"\n namespace \"{package_name}\"" + source[insert:] |
| 472 | + messages.append(f"Inserted namespace {package_name}") |
457 | 473 | return source |
458 | 474 |
|
| 475 | +def ensure_default_config(source: str) -> str: |
| 476 | + if re.search(r"defaultConfig\s*\{", source): |
| 477 | + return source |
| 478 | + android_match = re.search(r"android\s*\{", source) |
| 479 | + if not android_match: |
| 480 | + raise SystemExit("Unable to locate android block when creating defaultConfig") |
| 481 | + insert = android_match.end() |
| 482 | + snippet = "\n defaultConfig {\n }" |
| 483 | + messages.append("Inserted defaultConfig block") |
| 484 | + return source[:insert] + snippet + source[insert:] |
| 485 | +
|
459 | 486 | def ensure_application_id(source: str) -> str: |
460 | | - pattern = re.compile(r'\bapplicationId\s+["\']([^"\']+)["\']') |
| 487 | + pattern = re.compile(r"\bapplicationId\s+[\"']([^\"']+)[\"']") |
461 | 488 | match = pattern.search(source) |
462 | 489 | if match: |
463 | 490 | if match.group(1) != package_name: |
464 | 491 | start, end = match.span() |
465 | | - indent = source[:start].split('\n')[-1].split('applicationId')[0] |
466 | | - replacement = f"{indent}applicationId \"{package_name}\"" |
467 | | - source = source[:start] + replacement + source[end:] |
| 492 | + indent = source[:start].split("\n")[-1].split("applicationId")[0] |
| 493 | + source = source[:start] + f"{indent}applicationId \"{package_name}\"" + source[end:] |
468 | 494 | messages.append(f"Updated applicationId to {package_name}") |
469 | 495 | return source |
470 | | -
|
471 | | - default_match = re.search(r'defaultConfig\s*\{', source) |
472 | | - if default_match: |
473 | | - insert = default_match.end() |
474 | | - insertion = f"\n applicationId \"{package_name}\"" |
475 | | - source = source[:insert] + insertion + source[insert:] |
476 | | - messages.append(f"Inserted applicationId {package_name}") |
477 | | - return source |
478 | | -
|
479 | | - android_match = re.search(r'android\s*\{', source) |
480 | | - if not android_match: |
481 | | - sys.exit("Unable to locate android block when creating defaultConfig") |
482 | | - insert = android_match.end() |
483 | | - insertion = ("\n defaultConfig {\n applicationId \"{0}\"\n }\n".format(package_name)) |
484 | | - source = source[:insert] + insertion + source[insert:] |
485 | | - messages.append(f"Created defaultConfig with applicationId {package_name}") |
| 496 | + default_match = re.search(r"defaultConfig\s*\{", source) |
| 497 | + if not default_match: |
| 498 | + raise SystemExit("Unable to locate defaultConfig when inserting applicationId") |
| 499 | + insert = default_match.end() |
| 500 | + source = source[:insert] + f"\n applicationId \"{package_name}\"" + source[insert:] |
| 501 | + messages.append(f"Inserted applicationId {package_name}") |
486 | 502 | return source |
487 | 503 |
|
| 504 | +text = ensure_application_plugin(text) |
| 505 | +text = ensure_android_block(text) |
488 | 506 | text = ensure_namespace(text) |
| 507 | +text = ensure_default_config(text) |
489 | 508 | text = ensure_application_id(text) |
490 | 509 |
|
491 | 510 | if text != original: |
@@ -1099,25 +1118,44 @@ ba_log "Inspecting Gradle application identifiers" |
1099 | 1118 | APP_PROPERTIES_RAW=$(cd "$GRADLE_PROJECT_DIR" && ./gradlew -q :app:properties 2>/dev/null || true) |
1100 | 1119 | if [ -n "$APP_PROPERTIES_RAW" ]; then |
1101 | 1120 | set +o pipefail |
1102 | | - printf '%s\n' "$APP_PROPERTIES_RAW" \ |
1103 | | - | grep -E '^(applicationId|testApplicationId|namespace):' \ |
1104 | | - | sed 's/^/[build-android-app] props: /' || true |
| 1121 | + MATCHED_PROPS=$(printf '%s\n' "$APP_PROPERTIES_RAW" | grep -E '^(applicationId|testApplicationId|namespace):' || true) |
1105 | 1122 | set -o pipefail |
| 1123 | + if [ -n "$MATCHED_PROPS" ]; then |
| 1124 | + printf '%s\n' "$MATCHED_PROPS" | sed 's/^/[build-android-app] props: /' |
| 1125 | + fi |
1106 | 1126 | fi |
1107 | 1127 |
|
1108 | | -APP_ID="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^applicationId:/{print $2; exit}' || true)" |
1109 | | -NS_VALUE="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^namespace:/{print $2; exit}' || true)" |
| 1128 | +APP_ID="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^applicationId:/{print $2; found=1} END{if(!found) print ""}' || true)" |
| 1129 | +NS_VALUE="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^namespace:/{print $2; found=1} END{if(!found) print ""}' || true)" |
1110 | 1130 |
|
1111 | | -if [ -z "$APP_ID" ] || [ "$APP_ID" != "$PACKAGE_NAME" ]; then |
1112 | | - ba_log "ERROR: applicationId=$APP_ID does not match Codename One package $PACKAGE_NAME" >&2 |
1113 | | - stop_emulator |
1114 | | - exit 1 |
1115 | | -fi |
1116 | | - |
1117 | | -if [ -z "$NS_VALUE" ] || [ "$NS_VALUE" != "$PACKAGE_NAME" ]; then |
1118 | | - ba_log "ERROR: namespace=$NS_VALUE does not match Codename One package $PACKAGE_NAME" >&2 |
1119 | | - stop_emulator |
1120 | | - exit 1 |
| 1131 | +if [ -z "$APP_ID" ] || [ "$APP_ID" != "$PACKAGE_NAME" ] || [ -z "$NS_VALUE" ] || [ "$NS_VALUE" != "$PACKAGE_NAME" ]; then |
| 1132 | + ba_log "applicationId/namespace mismatch (applicationId='${APP_ID:-<unset>}' namespace='${NS_VALUE:-<unset>}'), patching" |
| 1133 | + if ! GRADLE_PACKAGE_LOG=$(ensure_gradle_package_config); then |
| 1134 | + ba_log "Failed to align namespace/applicationId with Codename One package" >&2 |
| 1135 | + stop_emulator |
| 1136 | + exit 1 |
| 1137 | + fi |
| 1138 | + if [ -n "$GRADLE_PACKAGE_LOG" ]; then |
| 1139 | + while IFS= read -r line; do |
| 1140 | + [ -n "$line" ] && ba_log "$line" |
| 1141 | + done <<<"$GRADLE_PACKAGE_LOG" |
| 1142 | + fi |
| 1143 | + APP_PROPERTIES_RAW=$(cd "$GRADLE_PROJECT_DIR" && ./gradlew -q :app:properties 2>/dev/null || true) |
| 1144 | + if [ -n "$APP_PROPERTIES_RAW" ]; then |
| 1145 | + set +o pipefail |
| 1146 | + MATCHED_PROPS=$(printf '%s\n' "$APP_PROPERTIES_RAW" | grep -E '^(applicationId|testApplicationId|namespace):' || true) |
| 1147 | + set -o pipefail |
| 1148 | + if [ -n "$MATCHED_PROPS" ]; then |
| 1149 | + printf '%s\n' "$MATCHED_PROPS" | sed 's/^/[build-android-app] props: /' |
| 1150 | + fi |
| 1151 | + fi |
| 1152 | + APP_ID="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^applicationId:/{print $2; found=1} END{if(!found) print ""}' || true)" |
| 1153 | + NS_VALUE="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^namespace:/{print $2; found=1} END{if(!found) print ""}' || true)" |
| 1154 | + if [ -z "$APP_ID" ] || [ "$APP_ID" != "$PACKAGE_NAME" ] || [ -z "$NS_VALUE" ] || [ "$NS_VALUE" != "$PACKAGE_NAME" ]; then |
| 1155 | + ba_log "ERROR: Unable to enforce applicationId/namespace to $PACKAGE_NAME (applicationId='$APP_ID' namespace='$NS_VALUE')" >&2 |
| 1156 | + stop_emulator |
| 1157 | + exit 1 |
| 1158 | + fi |
1121 | 1159 | fi |
1122 | 1160 |
|
1123 | 1161 | MERGED_MANIFEST="$APP_MODULE_DIR/build/intermediates/packaged_manifests/debug/AndroidManifest.xml" |
|
0 commit comments