Skip to content

Commit 523a7e4

Browse files
committed
Deduplicate stub manifest entry before Gradle preflight
1 parent 9eb6706 commit 523a7e4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

scripts/build-android-app.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ prune_stub_declaration() {
354354
tmp=$(mktemp)
355355
FQCN="$FQCN" perl -0777 -pe '
356356
my $fq = quotemeta($ENV{FQCN});
357+
s{<!--\s*CN1-STUB-BEGIN\s*-->.*?<!--\s*CN1-STUB-END\s*-->\s*}{}gs;
357358
s{<activity\b[^>]*android:name="$fq"[^>]*/>\s*}{}g;
358359
s{<activity\b[^>]*android:name="$fq"[^>]*>.*?</activity>\s*}{}gs;
359360
' "$manifest_path" >"$tmp"
@@ -368,11 +369,15 @@ if ! grep -Fq 'xmlns:tools=' "$MANIFEST_FILE"; then
368369
perl -0777 -pe 's#<manifest\b([^>]*)>#<manifest\1 xmlns:tools="http://schemas.android.com/tools">#' -i "$MANIFEST_FILE"
369370
fi
370371

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"
371374
tmp_manifest="$(mktemp)"
372375
awk -v fqcn="$FQCN" '
373376
BEGIN{inserted=0}
374377
/<\/application>/ && !inserted {
378+
print " <!-- CN1-STUB-BEGIN -->"
375379
print " <activity android:name=\"" fqcn "\" android:exported=\"false\" tools:node=\"replace\" />"
380+
print " <!-- CN1-STUB-END -->"
376381
inserted=1
377382
}
378383
{print}
@@ -497,6 +502,28 @@ ba_log "Dependencies block after instrumentation update:"
497502
awk '/^\s*dependencies\s*\{/{flag=1} flag{print} /^\s*\}/{if(flag){exit}}' "$APP_BUILD_GRADLE" \
498503
| sed 's/^/[build-android-app] | /'
499504

505+
# Final manifest sanity before Gradle preflight
506+
if [ -f "$MANIFEST_FILE" ]; then
507+
tmp_manifest_pruned="$(mktemp)"
508+
FQCN="$FQCN" perl -0777 -pe '
509+
my $fq = quotemeta($ENV{FQCN});
510+
my $seen = 0;
511+
s{
512+
(<activity\b[^>]*android:name="$fq"[^>]*/>\s*)
513+
|
514+
(<activity\b[^>]*android:name="$fq"[^>]*>.*?</activity>\s*)
515+
}{
516+
$seen++ ? "" : $&
517+
}gsxe;
518+
' "$MANIFEST_FILE" >"$tmp_manifest_pruned"
519+
mv "$tmp_manifest_pruned" "$MANIFEST_FILE"
520+
STUB_COUNT=$(grep -c "android:name=\"$FQCN\"" "$MANIFEST_FILE" || true)
521+
ba_log "Stub declarations in manifest after pruning: $STUB_COUNT"
522+
ba_log "Dumping manifest contents prior to preflight"
523+
nl -ba "$MANIFEST_FILE" | sed 's/^/[build-android-app] manifest: /'
524+
grep -n "android:name=\"$FQCN\"" "$MANIFEST_FILE" | sed 's/^/[build-android-app] manifest-match: /' || true
525+
fi
526+
500527
ba_log "Validating manifest merge before assemble"
501528
if ! (
502529
cd "$GRADLE_PROJECT_DIR" &&

0 commit comments

Comments
 (0)