Skip to content

Commit 65adb93

Browse files
committed
Normalize Codename One stub manifest instead of re-inserting
1 parent 523a7e4 commit 65adb93

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

scripts/build-android-app.sh

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ dump_manifest_merger_reports() {
334334
ba_log "Normalizing Codename One stub activity manifest"
335335
mkdir -p "$(dirname "$MANIFEST_FILE")"
336336
if [ ! -f "$MANIFEST_FILE" ]; then
337-
cat >"$MANIFEST_FILE" <<'EOF'
337+
cat >"$MANIFEST_FILE" <<'EOF_MANIFEST'
338338
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
339339
<application/>
340340
</manifest>
341-
EOF
341+
EOF_MANIFEST
342342
ba_log "Created minimal Android manifest at $MANIFEST_FILE"
343343
fi
344344

@@ -347,42 +347,59 @@ grep -q '<application' "$MANIFEST_FILE" || sed -i 's#</manifest># <application/
347347
# Remove deprecated package attribute and inline <uses-sdk/> declarations
348348
perl -0777 -pe 's/\s+package="[^"]*"//; s#<uses-sdk\b[^>]*/>\s*##g' -i "$MANIFEST_FILE"
349349

350-
prune_stub_declaration() {
351-
local manifest_path="$1"
352-
[ -f "$manifest_path" ] || return 0
353-
local tmp
354-
tmp=$(mktemp)
355-
FQCN="$FQCN" perl -0777 -pe '
356-
my $fq = quotemeta($ENV{FQCN});
357-
s{<!--\s*CN1-STUB-BEGIN\s*-->.*?<!--\s*CN1-STUB-END\s*-->\s*}{}gs;
358-
s{<activity\b[^>]*android:name="$fq"[^>]*/>\s*}{}g;
359-
s{<activity\b[^>]*android:name="$fq"[^>]*>.*?</activity>\s*}{}gs;
360-
' "$manifest_path" >"$tmp"
361-
mv "$tmp" "$manifest_path"
362-
}
363-
364-
for SS in main debug release; do
365-
prune_stub_declaration "$APP_MODULE_DIR/src/$SS/AndroidManifest.xml"
366-
done
367-
350+
# Ensure tools namespace for tools:node annotations
368351
if ! grep -Fq 'xmlns:tools=' "$MANIFEST_FILE"; then
369352
perl -0777 -pe 's#<manifest\b([^>]*)>#<manifest\1 xmlns:tools="http://schemas.android.com/tools">#' -i "$MANIFEST_FILE"
370353
fi
371354

372-
# Insert a sentinel-wrapped stub declaration
373-
perl -0777 -pe 's/<!--\s*CN1-STUB-BEGIN\s*-->.*?<!--\s*CN1-STUB-END\s*-->\s*//gs' -i "$MANIFEST_FILE"
374-
tmp_manifest="$(mktemp)"
375-
awk -v fqcn="$FQCN" '
376-
BEGIN{inserted=0}
377-
/<\/application>/ && !inserted {
378-
print " <!-- CN1-STUB-BEGIN -->"
379-
print " <activity android:name=\"" fqcn "\" android:exported=\"false\" tools:node=\"replace\" />"
380-
print " <!-- CN1-STUB-END -->"
381-
inserted=1
382-
}
383-
{print}
384-
' "$MANIFEST_FILE" >"$tmp_manifest"
385-
mv "$tmp_manifest" "$MANIFEST_FILE"
355+
# Normalize existing stub declarations rather than inserting new ones
356+
python3 - "$MANIFEST_FILE" "$FQCN" "$PACKAGE_NAME" "$MAIN_NAME" <<'PY'
357+
import re
358+
import sys
359+
from pathlib import Path
360+
361+
manifest_path, fqcn, package_name, main_name = sys.argv[1:5]
362+
manifest = Path(manifest_path)
363+
text = manifest.read_text()
364+
365+
text = re.sub(r'<!--\s*CN1-STUB-BEGIN\s*-->.*?<!--\s*CN1-STUB-END\s*-->', '', text, flags=re.S)
366+
367+
name_pattern = re.compile(
368+
r'(android:name=")(?:(?:%s)|(?:\.?%sStub)|(?:%s\.%sStub))"' % (
369+
re.escape(fqcn), re.escape(main_name), re.escape(package_name), re.escape(main_name)
370+
)
371+
)
372+
text = name_pattern.sub(r'\1%s"' % fqcn, text)
373+
374+
activity_pattern = re.compile(
375+
r'<activity\b[^>]*android:name="%s"[^>]*>(?:.*?)</activity>|<activity\b[^>]*android:name="%s"[^>]*/>' % (
376+
re.escape(fqcn), re.escape(fqcn)
377+
),
378+
flags=re.S,
379+
)
380+
381+
seen = {"value": False}
382+
383+
def replace_activity(match):
384+
body = match.group(0)
385+
if "tools:node=" in body:
386+
body = re.sub(r'tools:node="[^"]*"', 'tools:node="replace"', body, count=1)
387+
else:
388+
close = body.find('>')
389+
if close != -1:
390+
body = body[:close] + ' tools:node="replace"' + body[close:]
391+
if seen["value"]:
392+
return ''
393+
seen["value"] = True
394+
return body
395+
396+
text = activity_pattern.sub(replace_activity, text)
397+
398+
if not seen["value"]:
399+
raise SystemExit(f"Stub activity declaration not found in manifest: {manifest_path}")
400+
401+
manifest.write_text(text)
402+
PY
386403

387404
STUB_DECL_COUNT=$(grep -c "android:name=\"$FQCN\"" "$MANIFEST_FILE" || true)
388405
ba_log "Stub activity declarations present after normalization: $STUB_DECL_COUNT"

0 commit comments

Comments
 (0)