Skip to content

Commit 7a32683

Browse files
Use a resource patch to change the manifest instead of modifying xml as a text string
1 parent 19c7dc8 commit 7a32683

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

patches/src/main/kotlin/app/revanced/patches/music/layout/branding/CustomBrandingPatch.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ val customBrandingPatch = baseCustomBrandingPatch(
7575
),
7676
legacyMipmapFileNames = arrayOf("ic_launcher_release.png"),
7777
monochromeFileNames = arrayOf("ic_app_icons_themed_youtube_music.xml"),
78+
manifestAppLauncherValue = "@string/app_launcher_name",
7879

7980
block = {
8081
dependsOn(disableSplashAnimationPatch)

patches/src/main/kotlin/app/revanced/patches/shared/layout/branding/BaseCustomBrandingPatch.kt

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import app.revanced.patcher.patch.stringOption
99
import app.revanced.util.ResourceGroup
1010
import app.revanced.util.Utils.trimIndentMultiline
1111
import app.revanced.util.copyResources
12+
import app.revanced.util.findElementByAttributeValueOrThrow
1213
import java.io.File
1314
import java.nio.file.Files
14-
import java.util.logging.Logger
1515

1616
private const val REVANCED_ICON = "ReVanced*Logo" // Can never be a valid path.
1717

@@ -26,26 +26,6 @@ internal val mipmapDirectories = arrayOf(
2626

2727
private fun formatResourceFileList(resourceNames: Array<String>) = resourceNames.joinToString("\n") { "- $it" }
2828

29-
/**
30-
* Attempts to fix unescaped and invalid characters not allowed for an Android app name.
31-
*/
32-
private fun escapeAppName(name: String): String? {
33-
// Remove ASCII control characters.
34-
val cleanedName = name.filter { it.code >= 32 }
35-
36-
// Replace invalid XML characters with escaped equivalents.
37-
val escapedName = cleanedName
38-
.replace("&", "&amp;") // Must be first to avoid double-escaping.
39-
.replace("<", "&lt;")
40-
.replace(">", "&gt;")
41-
.replace(Regex("(?<!&)\""), "&quot;")
42-
43-
// Trim empty spacing.
44-
val trimmed = escapedName.trim()
45-
46-
return trimmed.ifBlank { null }
47-
}
48-
4929
/**
5030
* Shared custom branding patch for YouTube and YT Music.
5131
*/
@@ -57,6 +37,7 @@ internal fun baseCustomBrandingPatch(
5737
adaptiveMipmapFileNames: Array<String>,
5838
legacyMipmapFileNames: Array<String>,
5939
monochromeFileNames: Array<String>,
40+
manifestAppLauncherValue: String,
6041
block: ResourcePatchBuilder.() -> Unit = {},
6142
executeBlock: ResourcePatchContext.() -> Unit = {}
6243
): ResourcePatch = resourcePatch(
@@ -93,6 +74,20 @@ internal fun baseCustomBrandingPatch(
9374

9475
block()
9576

77+
dependsOn(
78+
// Change the app name.
79+
resourcePatch {
80+
execute {
81+
document("AndroidManifest.xml").use { document ->
82+
document.childNodes.findElementByAttributeValueOrThrow(
83+
"android:label",
84+
manifestAppLauncherValue
85+
).nodeValue = appName!!
86+
}
87+
}
88+
}
89+
)
90+
9691
execute {
9792
val iconPathTrimmed = iconPath!!.trim()
9893

@@ -164,27 +159,6 @@ internal fun baseCustomBrandingPatch(
164159
}
165160
}
166161

167-
// Change the app name.
168-
escapeAppName(appName!!)?.let { escapedAppName ->
169-
val newValue = "android:label=\"$escapedAppName\""
170-
171-
val manifest = get("AndroidManifest.xml")
172-
val original = manifest.readText()
173-
val replacement = original
174-
// YouTube
175-
.replace("android:label=\"@string/application_name\"", newValue)
176-
// YT Music
177-
.replace("android:label=\"@string/app_launcher_name\"", newValue)
178-
179-
if (original == replacement) {
180-
Logger.getLogger(this::class.java.name).warning(
181-
"Could not replace manifest app name"
182-
)
183-
}
184-
185-
manifest.writeText(replacement)
186-
}
187-
188162
executeBlock() // Must be after the main code to rename the new icons for YouTube 19.34+.
189163
}
190164
}

patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/CustomBrandingPatch.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ val customBrandingPatch = baseCustomBrandingPatch(
3535
"adaptive_monochrome_ic_youtube_launcher.xml",
3636
"ringo2_adaptive_monochrome_ic_youtube_launcher.xml"
3737
),
38+
manifestAppLauncherValue = "@string/application_name",
3839

3940
block = {
4041
compatibleWith(

0 commit comments

Comments
 (0)