Skip to content

Commit ab7bc93

Browse files
committed
Canonicalize generated Android manifest and Gradle IDs
1 parent a5f7aae commit ab7bc93

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

scripts/build-android-app.sh

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ APP_ID="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^applicationId:/{pr
10671067
NS_VALUE="$(printf '%s\n' "$APP_PROPERTIES_RAW" | awk -F': ' '/^namespace:/{print $2; exit}')"
10681068

10691069
if [ -f "$APP_BUILD_GRADLE" ]; then
1070-
if [ -n "$APP_ID" ] && [ "$APP_ID" != "$PACKAGE_NAME" ]; then
1070+
if [ -z "$APP_ID" ] || [ "$APP_ID" != "$PACKAGE_NAME" ]; then
10711071
ba_log "Patching applicationId -> $PACKAGE_NAME"
10721072
awk -v appid="$PACKAGE_NAME" '
10731073
BEGIN{inDc=0;had=0}
@@ -1097,16 +1097,16 @@ if [ -f "$APP_BUILD_GRADLE" ]; then
10971097
fi
10981098

10991099
FQCN="${PACKAGE_NAME}.${MAIN_NAME}Stub"
1100-
STUB_BASENAME="${MAIN_NAME}Stub"
11011100
remove_stub_declaration() {
11021101
local manifest_path="$1"
11031102
[ -f "$manifest_path" ] || return 0
1104-
local tmp
1103+
local tmp escaped
11051104
tmp=$(mktemp)
1106-
perl -0777 -pe "s{<activity\\b[^>]*android:name=\\\"[^\\\"]*${STUB_BASENAME}\\\"[^>]*/>\\s*}{}g; s{<activity\\b[^>]*android:name=\\\"[^\\\"]*${STUB_BASENAME}\\\"[^>]*>.*?<\/activity>\\s*}{}gs" "$manifest_path" >"$tmp"
1105+
escaped=$(printf '%s' "$FQCN" | sed 's/[\\&/]/\\&/g')
1106+
perl -0777 -pe "s{<activity\\b[^>]*android:name=\\\"$escaped\\\"[^>]*/>\\s*}{}g; s{<activity\\b[^>]*android:name=\\\"$escaped\\\"[^>]*>.*?<\\/activity>\\s*}{}gs" "$manifest_path" >"$tmp"
11071107
if ! cmp -s "$manifest_path" "$tmp"; then
11081108
mv "$tmp" "$manifest_path"
1109-
ba_log "Removed existing ${STUB_BASENAME} declarations from $manifest_path"
1109+
ba_log "Removed existing ${FQCN} declarations from $manifest_path"
11101110
else
11111111
rm -f "$tmp"
11121112
fi
@@ -1122,21 +1122,17 @@ MAIN_MANIFEST="$APP_MODULE_DIR/src/main/AndroidManifest.xml"
11221122
if [ ! -f "$MAIN_MANIFEST" ]; then
11231123
mkdir -p "$(dirname "$MAIN_MANIFEST")"
11241124
cat >"$MAIN_MANIFEST" <<EOF
1125-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="$PACKAGE_NAME">
1125+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
11261126
<application/>
11271127
</manifest>
11281128
EOF
11291129
ba_log "Created $MAIN_MANIFEST"
11301130
fi
11311131

1132-
if grep -q '<manifest[^>]*package=' "$MAIN_MANIFEST"; then
1133-
sed -i "0,/<manifest[^>]*package=/ s/package=\"[^\"]*\"/package=\"$PACKAGE_NAME\"/" "$MAIN_MANIFEST"
1134-
else
1135-
sed -i "0,/<manifest/ s#<manifest#<manifest package=\"$PACKAGE_NAME\"#" "$MAIN_MANIFEST"
1136-
fi
1137-
11381132
grep -q '<application' "$MAIN_MANIFEST" || sed -i 's#</manifest># <application/>\n</manifest>#' "$MAIN_MANIFEST"
11391133

1134+
perl -0777 -pe 's/\s+package="[^"]*"//; s#<uses-sdk\b[^>]*/>\s*##g' -i "$MAIN_MANIFEST"
1135+
11401136
# Ensure no stale stub declarations remain before inserting the canonical entry.
11411137
remove_stub_declaration "$MAIN_MANIFEST"
11421138

@@ -1149,6 +1145,7 @@ awk -v fqcn="$FQCN" '
11491145
{print}
11501146
' "$MAIN_MANIFEST" >"$MAIN_MANIFEST.tmp"
11511147
mv "$MAIN_MANIFEST.tmp" "$MAIN_MANIFEST"
1148+
11521149
ba_log "Canonicalized stub activity declaration in $MAIN_MANIFEST"
11531150

11541151
ba_log "Validating manifest merge after stub declaration"

0 commit comments

Comments
 (0)