Skip to content

Commit 22adfca

Browse files
committed
Ensure Gradle IDs and manifest stub stay aligned
1 parent bd7fe2e commit 22adfca

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

scripts/build-android-app.sh

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ tree = ET.parse(manifest_path)
337337
root = tree.getroot()
338338
changed = False
339339
340-
# Ensure manifest declares the package attribute for clarity
341-
if 'package' not in root.attrib:
340+
# Ensure manifest package aligns with the Codename One package
341+
if root.get('package') != package_name:
342342
root.set('package', package_name)
343343
changed = True
344344
@@ -377,7 +377,9 @@ if ! ensure_manifest_stub; then
377377
exit 1
378378
fi
379379

380-
if ! grep -Eq "android:name=\"${PACKAGE_NAME//./\\.}.${MAIN_NAME}Stub\"" "$MANIFEST_FILE"; then
380+
FQCN="${PACKAGE_NAME}.${MAIN_NAME}Stub"
381+
382+
if ! grep -Eq "android:name=\"${PACKAGE_NAME//./\\.}\\.${MAIN_NAME}Stub\"" "$MANIFEST_FILE"; then
381383
ba_log "Manifest does not declare ${MAIN_NAME}Stub activity" >&2
382384
exit 1
383385
fi
@@ -486,10 +488,54 @@ if APP_PROPERTIES_RAW=$(cd "$GRADLE_PROJECT_DIR" && ./gradlew -q :app:properties
486488
printf '%s\n' "$APP_PROPERTIES_RAW" | grep -E '^(applicationId|testApplicationId|namespace):' | \
487489
sed 's/^/[build-android-app] props: /'
488490
APP_ID=$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^applicationId:/{print $2; exit}')
491+
NS_VALUE=$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^namespace:/{print $2; exit}')
492+
PROPS_MODIFIED=0
493+
if [ -n "$APP_ID" ] && [ "$APP_ID" != "$PACKAGE_NAME" ]; then
494+
ba_log "Patching applicationId -> $PACKAGE_NAME"
495+
awk -v appid="$PACKAGE_NAME" '
496+
BEGIN{inDc=0;done=0}
497+
/^\s*defaultConfig\s*\{/ {inDc=1}
498+
inDc && /^\s*applicationId\s+/ {print " applicationId \""appid"\""; done=1; next}
499+
inDc && /^\s*}/ {
500+
if(!done){print " applicationId \""appid"\""; done=1}
501+
inDc=0
502+
}
503+
{print}
504+
' "$APP_BUILD_GRADLE" > "$APP_BUILD_GRADLE.tmp" && mv "$APP_BUILD_GRADLE.tmp" "$APP_BUILD_GRADLE"
505+
PROPS_MODIFIED=1
506+
fi
507+
if [ -z "$NS_VALUE" ] || [ "$NS_VALUE" != "$PACKAGE_NAME" ]; then
508+
ba_log "Patching namespace -> $PACKAGE_NAME"
509+
awk -v ns="$PACKAGE_NAME" '
510+
BEGIN{inAndroid=0;done=0}
511+
/^android\s*\{/ {inAndroid=1}
512+
inAndroid && /^\s*namespace\s+/ {print " namespace \""ns"\""; done=1; next}
513+
inAndroid && /^\s*}/ {
514+
if(inAndroid && !done){print " namespace \""ns"\""; done=1}
515+
inAndroid=0
516+
}
517+
{print}
518+
' "$APP_BUILD_GRADLE" > "$APP_BUILD_GRADLE.tmp" && mv "$APP_BUILD_GRADLE.tmp" "$APP_BUILD_GRADLE"
519+
PROPS_MODIFIED=1
520+
fi
521+
if [ "$PROPS_MODIFIED" -eq 1 ]; then
522+
if APP_PROPERTIES_RAW=$(cd "$GRADLE_PROJECT_DIR" && ./gradlew -q :app:properties 2>/dev/null); then
523+
printf '%s\n' "$APP_PROPERTIES_RAW" | grep -E '^(applicationId|testApplicationId|namespace):' | \
524+
sed 's/^/[build-android-app] props (post-patch): /'
525+
else
526+
ba_log "Warning: unable to re-query :app:properties after patch" >&2
527+
fi
528+
fi
529+
APP_ID=$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^applicationId:/{print $2; exit}')
530+
NS_VALUE=$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^namespace:/{print $2; exit}')
489531
if [ -n "$APP_ID" ] && [ "$APP_ID" != "$PACKAGE_NAME" ]; then
490532
ba_log "ERROR: applicationId=$APP_ID does not match Codename One package $PACKAGE_NAME" >&2
491533
exit 1
492534
fi
535+
if [ -n "$NS_VALUE" ] && [ "$NS_VALUE" != "$PACKAGE_NAME" ]; then
536+
ba_log "ERROR: namespace=$NS_VALUE does not match Codename One package $PACKAGE_NAME" >&2
537+
exit 1
538+
fi
493539
else
494540
ba_log "Warning: unable to query :app:properties via Gradle" >&2
495541
fi
@@ -1126,7 +1172,7 @@ LAUNCH_RESOLVE_OUTPUT="$("$ADB_BIN" -s "$EMULATOR_SERIAL" shell cmd package reso
11261172
if [ -n "$LAUNCH_RESOLVE_OUTPUT" ]; then
11271173
printf '%s\n' "$LAUNCH_RESOLVE_OUTPUT" | sed 's/^/[build-android-app] resolve-launch: /'
11281174
fi
1129-
STUB_ACTIVITY_FQCN="$PACKAGE_NAME/$PACKAGE_NAME.${MAIN_NAME}Stub"
1175+
STUB_ACTIVITY_FQCN="$PACKAGE_NAME/$FQCN"
11301176
STUB_RESOLVE_OUTPUT="$(
11311177
"$ADB_BIN" -s "$EMULATOR_SERIAL" shell cmd package resolve-activity --brief "$STUB_ACTIVITY_FQCN" 2>&1 || true
11321178
)"

0 commit comments

Comments
 (0)