Skip to content

Commit 9eb6706

Browse files
committed
Canonicalize stub activity manifest before Gradle
1 parent e81c800 commit 9eb6706

File tree

1 file changed

+57
-59
lines changed

1 file changed

+57
-59
lines changed

scripts/build-android-app.sh

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -313,61 +313,10 @@ fi
313313
FQCN="${PACKAGE_NAME}.${MAIN_NAME}Stub"
314314
MANIFEST_FILE="$APP_MODULE_DIR/src/main/AndroidManifest.xml"
315315

316-
normalize_stub_manifest() {
317-
mkdir -p "$(dirname "$MANIFEST_FILE")"
318-
if [ ! -f "$MANIFEST_FILE" ]; then
319-
cat >"$MANIFEST_FILE" <<'EOF'
320-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
321-
<application/>
322-
</manifest>
323-
EOF
324-
ba_log "Created minimal Android manifest at $MANIFEST_FILE"
325-
fi
326-
327-
# Ensure the main manifest has an <application> element
328-
grep -q '<application' "$MANIFEST_FILE" || sed -i 's#</manifest># <application/>\n</manifest>#' "$MANIFEST_FILE"
329-
330-
# Remove deprecated package attribute and any inline <uses-sdk/> declarations
331-
perl -0777 -pe 's/\s+package="[^"]*"//; s#<uses-sdk\b[^>]*/>\s*##g' -i "$MANIFEST_FILE"
332-
333-
remove_stub_declaration() {
334-
local manifest_path="$1"
335-
[ -f "$manifest_path" ] || return 0
336-
local escaped tmp
337-
escaped=$(printf '%s' "$FQCN" | sed 's/[\\&/]/\\&/g')
338-
tmp=$(mktemp)
339-
perl -0777 -pe "s{<activity\\b[^>]*android:name=\\\"$escaped\\\"[^>]*/>\\s*}{}g; s{<activity\\b[^>]*android:name=\\\"$escaped\\\"[^>]*>.*?<\\/activity>\\s*}{}gs" "$manifest_path" >"$tmp"
340-
mv "$tmp" "$manifest_path"
341-
}
342-
343-
for SS in main debug release; do
344-
remove_stub_declaration "$APP_MODULE_DIR/src/$SS/AndroidManifest.xml"
345-
done
346-
347-
# Ensure tools namespace is available for merge directives
348-
if ! grep -q 'xmlns:tools=' "$MANIFEST_FILE"; then
349-
perl -0777 -pe 's/<manifest\b([^>]*)>/<manifest\1 xmlns:tools="http:\/\/schemas.android.com\/tools">/' -i "$MANIFEST_FILE"
350-
fi
351-
352-
# Insert a single canonical stub declaration
353-
awk -v fqcn="$FQCN" '
354-
BEGIN{inserted=0}
355-
/<\/application>/ && !inserted {
356-
print " <activity android:name=\"" fqcn "\" android:exported=\"false\" tools:node=\"replace\" />"
357-
inserted=1
358-
}
359-
{print}
360-
' "$MANIFEST_FILE" >"$MANIFEST_FILE.tmp"
361-
mv "$MANIFEST_FILE.tmp" "$MANIFEST_FILE"
362-
363-
ba_log "Canonicalized stub activity declaration in $MANIFEST_FILE"
364-
}
365-
366316
dump_manifest_merger_reports() {
367-
local blame_a blame_b merged
368-
blame_a="$APP_MODULE_DIR/build/intermediates/manifest_merge_blame_file/debug/manifest-merger-blame-debug-report.txt"
369-
blame_b="$APP_MODULE_DIR/build/intermediates/incremental/processDebugMainManifest/manifest-merger-blame-report.txt"
370-
merged="$APP_MODULE_DIR/build/intermediates/packaged_manifests/debug/AndroidManifest.xml"
317+
local blame_a="$APP_MODULE_DIR/build/intermediates/manifest_merge_blame_file/debug/manifest-merger-blame-debug-report.txt"
318+
local blame_b="$APP_MODULE_DIR/build/intermediates/incremental/processDebugMainManifest/manifest-merger-blame-report.txt"
319+
local merged="$APP_MODULE_DIR/build/intermediates/packaged_manifests/debug/AndroidManifest.xml"
371320

372321
for report in "$blame_a" "$blame_b"; do
373322
if [ -f "$report" ]; then
@@ -382,15 +331,64 @@ dump_manifest_merger_reports() {
382331
fi
383332
}
384333

385-
normalize_stub_manifest
334+
ba_log "Normalizing Codename One stub activity manifest"
335+
mkdir -p "$(dirname "$MANIFEST_FILE")"
336+
if [ ! -f "$MANIFEST_FILE" ]; then
337+
cat >"$MANIFEST_FILE" <<'EOF'
338+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
339+
<application/>
340+
</manifest>
341+
EOF
342+
ba_log "Created minimal Android manifest at $MANIFEST_FILE"
343+
fi
386344

387-
if [ ! -f "$STUB_SRC_FILE" ]; then
388-
ba_log "Missing stub activity source at $STUB_SRC_FILE" >&2
345+
grep -q '<application' "$MANIFEST_FILE" || sed -i 's#</manifest># <application/>\n</manifest>#' "$MANIFEST_FILE"
346+
347+
# Remove deprecated package attribute and inline <uses-sdk/> declarations
348+
perl -0777 -pe 's/\s+package="[^"]*"//; s#<uses-sdk\b[^>]*/>\s*##g' -i "$MANIFEST_FILE"
349+
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{<activity\b[^>]*android:name="$fq"[^>]*/>\s*}{}g;
358+
s{<activity\b[^>]*android:name="$fq"[^>]*>.*?</activity>\s*}{}gs;
359+
' "$manifest_path" >"$tmp"
360+
mv "$tmp" "$manifest_path"
361+
}
362+
363+
for SS in main debug release; do
364+
prune_stub_declaration "$APP_MODULE_DIR/src/$SS/AndroidManifest.xml"
365+
done
366+
367+
if ! grep -Fq 'xmlns:tools=' "$MANIFEST_FILE"; then
368+
perl -0777 -pe 's#<manifest\b([^>]*)>#<manifest\1 xmlns:tools="http://schemas.android.com/tools">#' -i "$MANIFEST_FILE"
369+
fi
370+
371+
tmp_manifest="$(mktemp)"
372+
awk -v fqcn="$FQCN" '
373+
BEGIN{inserted=0}
374+
/<\/application>/ && !inserted {
375+
print " <activity android:name=\"" fqcn "\" android:exported=\"false\" tools:node=\"replace\" />"
376+
inserted=1
377+
}
378+
{print}
379+
' "$MANIFEST_FILE" >"$tmp_manifest"
380+
mv "$tmp_manifest" "$MANIFEST_FILE"
381+
382+
STUB_DECL_COUNT=$(grep -c "android:name=\"$FQCN\"" "$MANIFEST_FILE" || true)
383+
ba_log "Stub activity declarations present after normalization: $STUB_DECL_COUNT"
384+
if [ "$STUB_DECL_COUNT" -ne 1 ]; then
385+
ba_log "Expected exactly one stub activity declaration after normalization" >&2
386+
sed -n '1,160p' "$MANIFEST_FILE" | sed 's/^/[build-android-app] manifest: /'
389387
exit 1
390388
fi
391389

392-
if ! grep -Fq "android:name=\"$FQCN\"" "$MANIFEST_FILE"; then
393-
ba_log "Manifest does not declare ${MAIN_NAME}Stub activity" >&2
390+
if [ ! -f "$STUB_SRC_FILE" ]; then
391+
ba_log "Missing stub activity source at $STUB_SRC_FILE" >&2
394392
exit 1
395393
fi
396394

0 commit comments

Comments
 (0)