Skip to content

Commit

Permalink
feat(Change package name): Add options to change provider and permiss…
Browse files Browse the repository at this point in the history
…ion package names to handle installation conflicts

Co-authored-by: LisoUseInAIKyrios <[email protected]>
Co-authored-by: oSumAtrIX <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 02edf0c commit 75c740c
Showing 1 changed file with 71 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.revanced.patches.all.misc.packagename

import app.revanced.patcher.patch.Option
import app.revanced.patcher.patch.resourcePatch
import app.revanced.patcher.patch.stringOption
import app.revanced.patcher.patch.*
import app.revanced.util.asSequence
import app.revanced.util.getNode
import org.w3c.dom.Element
import java.util.logging.Logger

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

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

/**
* Apps that are confirmed to not work correctly with this patch.
* This is not an exhaustive list, and is only the apps with
* ReVanced specific patches and are confirmed incompatible with this patch.
*/
val incompatibleAppPackages = setOf(
// Cannot login, settings menu is broken.
"com.reddit.frontpage",

// Patches and installs but crashes on launch.
"com.duolingo",
"com.twitter.android",
"tv.twitch.android.app",
val updatePermissions by booleanOption(
key = "updatePermissions",
default = false,
title = "Update permissions",
description = "Update compatibility receiver permissions. " +
"Enabling this can fix installation errors, but this can also break features in certain apps.",
)

val updateProviders by booleanOption(
key = "updateProviders",
default = false,
title = "Update providers",
description = "Update provider names declared by the app. " +
"Enabling this can fix installation errors, but this can also break features in certain apps.",
)

finalize {
/**
* Apps that are confirmed to not work correctly with this patch.
* This is not an exhaustive list, and is only the apps with
* ReVanced specific patches and are confirmed incompatible with this patch.
*/
val incompatibleAppPackages = setOf(
// Cannot log in, settings menu is broken.
"com.reddit.frontpage",

// Patches and installs but crashes on launch.
"com.duolingo",
"com.twitter.android",
"tv.twitch.android.app",
)

document("AndroidManifest.xml").use { document ->
val manifest = document.getElementsByTagName("manifest").item(0) as Element
val originalPackageName = manifest.getAttribute("package")
val manifest = document.getNode("manifest") as Element
val packageName = manifest.getAttribute("package")

if (incompatibleAppPackages.contains(originalPackageName)) {
if (incompatibleAppPackages.contains(packageName)) {
return@finalize Logger.getLogger(this::class.java.name).severe(
"'$originalPackageName' does not work correctly with \"Change package name\"")
"'$packageName' does not work correctly with \"Change package name\"",
)
}

val replacementPackageName = packageNameOption.value
manifest.setAttribute(
"package",
if (replacementPackageName != packageNameOption.default) {
replacementPackageName
} else {
"${originalPackageName}.revanced"
},
)
val newPackageName = if (replacementPackageName != packageNameOption.default) {
replacementPackageName!!
} else {
"$packageName.revanced"
}

manifest.setAttribute("package", newPackageName)

if (updatePermissions == true) {
val permissions = manifest.getElementsByTagName("permission").asSequence()
val usesPermissions = manifest.getElementsByTagName("uses-permission").asSequence()

val receiverNotExported = "DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"

(permissions + usesPermissions)
.map { it as Element }
.filter { it.getAttribute("android:name") == "$packageName.$receiverNotExported" }
.forEach { it.setAttribute("android:name", "$newPackageName.$receiverNotExported") }
}

if (updateProviders == true) {
val providers = manifest.getElementsByTagName("provider").asSequence()

for (node in providers) {
val provider = node as Element

val authorities = provider.getAttribute("android:authorities")
if (!authorities.startsWith("$packageName.")) continue

provider.setAttribute("android:authorities", authorities.replace(packageName, newPackageName))
}
}
}
}
}

0 comments on commit 75c740c

Please sign in to comment.