Skip to content

Commit 75c740c

Browse files
1fexdLisoUseInAIKyriosoSumAtrIX
authored
feat(Change package name): Add options to change provider and permission package names to handle installation conflicts
Co-authored-by: LisoUseInAIKyrios <[email protected]> Co-authored-by: oSumAtrIX <[email protected]>
1 parent 02edf0c commit 75c740c

File tree

1 file changed

+71
-29
lines changed

1 file changed

+71
-29
lines changed
Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package app.revanced.patches.all.misc.packagename
22

3-
import app.revanced.patcher.patch.Option
4-
import app.revanced.patcher.patch.resourcePatch
5-
import app.revanced.patcher.patch.stringOption
3+
import app.revanced.patcher.patch.*
4+
import app.revanced.util.asSequence
5+
import app.revanced.util.getNode
66
import org.w3c.dom.Element
77
import java.util.logging.Logger
88

@@ -28,7 +28,8 @@ fun setOrGetFallbackPackageName(fallbackPackageName: String): String {
2828

2929
val changePackageNamePatch = resourcePatch(
3030
name = "Change package name",
31-
description = "Appends \".revanced\" to the package name by default. Changing the package name of the app can lead to unexpected issues.",
31+
description = "Appends \".revanced\" to the package name by default. " +
32+
"Changing the package name of the app can lead to unexpected issues.",
3233
use = false,
3334
) {
3435
packageNameOption = stringOption(
@@ -42,40 +43,81 @@ val changePackageNamePatch = resourcePatch(
4243
it == "Default" || it!!.matches(Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$"))
4344
}
4445

45-
/**
46-
* Apps that are confirmed to not work correctly with this patch.
47-
* This is not an exhaustive list, and is only the apps with
48-
* ReVanced specific patches and are confirmed incompatible with this patch.
49-
*/
50-
val incompatibleAppPackages = setOf(
51-
// Cannot login, settings menu is broken.
52-
"com.reddit.frontpage",
53-
54-
// Patches and installs but crashes on launch.
55-
"com.duolingo",
56-
"com.twitter.android",
57-
"tv.twitch.android.app",
46+
val updatePermissions by booleanOption(
47+
key = "updatePermissions",
48+
default = false,
49+
title = "Update permissions",
50+
description = "Update compatibility receiver permissions. " +
51+
"Enabling this can fix installation errors, but this can also break features in certain apps.",
52+
)
53+
54+
val updateProviders by booleanOption(
55+
key = "updateProviders",
56+
default = false,
57+
title = "Update providers",
58+
description = "Update provider names declared by the app. " +
59+
"Enabling this can fix installation errors, but this can also break features in certain apps.",
5860
)
5961

6062
finalize {
63+
/**
64+
* Apps that are confirmed to not work correctly with this patch.
65+
* This is not an exhaustive list, and is only the apps with
66+
* ReVanced specific patches and are confirmed incompatible with this patch.
67+
*/
68+
val incompatibleAppPackages = setOf(
69+
// Cannot log in, settings menu is broken.
70+
"com.reddit.frontpage",
71+
72+
// Patches and installs but crashes on launch.
73+
"com.duolingo",
74+
"com.twitter.android",
75+
"tv.twitch.android.app",
76+
)
77+
6178
document("AndroidManifest.xml").use { document ->
62-
val manifest = document.getElementsByTagName("manifest").item(0) as Element
63-
val originalPackageName = manifest.getAttribute("package")
79+
val manifest = document.getNode("manifest") as Element
80+
val packageName = manifest.getAttribute("package")
6481

65-
if (incompatibleAppPackages.contains(originalPackageName)) {
82+
if (incompatibleAppPackages.contains(packageName)) {
6683
return@finalize Logger.getLogger(this::class.java.name).severe(
67-
"'$originalPackageName' does not work correctly with \"Change package name\"")
84+
"'$packageName' does not work correctly with \"Change package name\"",
85+
)
6886
}
6987

7088
val replacementPackageName = packageNameOption.value
71-
manifest.setAttribute(
72-
"package",
73-
if (replacementPackageName != packageNameOption.default) {
74-
replacementPackageName
75-
} else {
76-
"${originalPackageName}.revanced"
77-
},
78-
)
89+
val newPackageName = if (replacementPackageName != packageNameOption.default) {
90+
replacementPackageName!!
91+
} else {
92+
"$packageName.revanced"
93+
}
94+
95+
manifest.setAttribute("package", newPackageName)
96+
97+
if (updatePermissions == true) {
98+
val permissions = manifest.getElementsByTagName("permission").asSequence()
99+
val usesPermissions = manifest.getElementsByTagName("uses-permission").asSequence()
100+
101+
val receiverNotExported = "DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
102+
103+
(permissions + usesPermissions)
104+
.map { it as Element }
105+
.filter { it.getAttribute("android:name") == "$packageName.$receiverNotExported" }
106+
.forEach { it.setAttribute("android:name", "$newPackageName.$receiverNotExported") }
107+
}
108+
109+
if (updateProviders == true) {
110+
val providers = manifest.getElementsByTagName("provider").asSequence()
111+
112+
for (node in providers) {
113+
val provider = node as Element
114+
115+
val authorities = provider.getAttribute("android:authorities")
116+
if (!authorities.startsWith("$packageName.")) continue
117+
118+
provider.setAttribute("android:authorities", authorities.replace(packageName, newPackageName))
119+
}
120+
}
79121
}
80122
}
81123
}

0 commit comments

Comments
 (0)