Skip to content

Commit 32115db

Browse files
committed
Fix Gradle identifier alignment and manifest inspection
1 parent f368220 commit 32115db

File tree

1 file changed

+85
-47
lines changed

1 file changed

+85
-47
lines changed

scripts/build-android-app.sh

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -428,64 +428,83 @@ fi
428428
ensure_gradle_package_config() {
429429
python3 - "$APP_BUILD_GRADLE" "$PACKAGE_NAME" <<'PY'
430430
import sys
431-
from pathlib import Path
432431
import re
432+
from pathlib import Path
433433
434434
path = Path(sys.argv[1])
435435
package_name = sys.argv[2]
436436
text = path.read_text()
437437
original = text
438438
messages = []
439439
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+
440458
def ensure_namespace(source: str) -> str:
441-
pattern = re.compile(r'\bnamespace\s+["\']([^"\']+)["\']')
459+
pattern = re.compile(r"\bnamespace\s+[\"']([^\"']+)[\"']")
442460
match = pattern.search(source)
443461
if match:
444462
if match.group(1) != package_name:
445463
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:]
448465
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}")
457473
return source
458474
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+
459486
def ensure_application_id(source: str) -> str:
460-
pattern = re.compile(r'\bapplicationId\s+["\']([^"\']+)["\']')
487+
pattern = re.compile(r"\bapplicationId\s+[\"']([^\"']+)[\"']")
461488
match = pattern.search(source)
462489
if match:
463490
if match.group(1) != package_name:
464491
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:]
468494
messages.append(f"Updated applicationId to {package_name}")
469495
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}")
486502
return source
487503
504+
text = ensure_application_plugin(text)
505+
text = ensure_android_block(text)
488506
text = ensure_namespace(text)
507+
text = ensure_default_config(text)
489508
text = ensure_application_id(text)
490509
491510
if text != original:
@@ -1099,25 +1118,44 @@ ba_log "Inspecting Gradle application identifiers"
10991118
APP_PROPERTIES_RAW=$(cd "$GRADLE_PROJECT_DIR" && ./gradlew -q :app:properties 2>/dev/null || true)
11001119
if [ -n "$APP_PROPERTIES_RAW" ]; then
11011120
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)
11051122
set -o pipefail
1123+
if [ -n "$MATCHED_PROPS" ]; then
1124+
printf '%s\n' "$MATCHED_PROPS" | sed 's/^/[build-android-app] props: /'
1125+
fi
11061126
fi
11071127

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)"
11101130

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
11211159
fi
11221160

11231161
MERGED_MANIFEST="$APP_MODULE_DIR/build/intermediates/packaged_manifests/debug/AndroidManifest.xml"

0 commit comments

Comments
 (0)