feat: Improve package change patch

This commit is contained in:
1fexd 2024-10-31 17:22:55 +01:00
parent 4b818e49a2
commit 3e022929c7
No known key found for this signature in database
GPG key ID: B459DD424E49FAAD
2 changed files with 61 additions and 8 deletions

View file

@ -74,10 +74,16 @@ public final class app/revanced/patches/all/misc/network/OverrideCertificatePinn
public final class app/revanced/patches/all/misc/packagename/ChangePackageNamePatchKt {
public static field packageNameOption Lapp/revanced/patcher/patch/Option;
public static field updatePermissions Lapp/revanced/patcher/patch/Option;
public static field updateProviders Lapp/revanced/patcher/patch/Option;
public static final fun getChangePackageNamePatch ()Lapp/revanced/patcher/patch/ResourcePatch;
public static final fun getPackageNameOption ()Lapp/revanced/patcher/patch/Option;
public static final fun getUpdatePermissions ()Lapp/revanced/patcher/patch/Option;
public static final fun getUpdateProviders ()Lapp/revanced/patcher/patch/Option;
public static final fun setOrGetFallbackPackageName (Ljava/lang/String;)Ljava/lang/String;
public static final fun setPackageNameOption (Lapp/revanced/patcher/patch/Option;)V
public static final fun setUpdatePermissions (Lapp/revanced/patcher/patch/Option;)V
public static final fun setUpdateProviders (Lapp/revanced/patcher/patch/Option;)V
}
public final class app/revanced/patches/all/misc/resources/AddResourcesPatchKt {

View file

@ -1,11 +1,15 @@
package app.revanced.patches.all.misc.packagename
import app.revanced.patcher.patch.Option
import app.revanced.patcher.patch.booleanOption
import app.revanced.patcher.patch.resourcePatch
import app.revanced.patcher.patch.stringOption
import app.revanced.util.asSequence
import org.w3c.dom.Element
lateinit var packageNameOption: Option<String>
lateinit var updatePermissions: Option<Boolean>
lateinit var updateProviders: Option<Boolean>
/**
* Set the package name to use.
@ -25,6 +29,8 @@ fun setOrGetFallbackPackageName(fallbackPackageName: String): String {
}
}
private const val RECEIVER_NOT_EXPORTED = "DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
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.",
@ -41,20 +47,61 @@ val changePackageNamePatch = resourcePatch(
it == "Default" || it!!.matches(Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$"))
}
updatePermissions = booleanOption(
key = "updatePermissions",
default = false,
title = "Update permissions",
description = "Update compatibility receiver permissions.",
required = false
)
updateProviders = booleanOption(
key = "updateProviders",
default = false,
title = "Update providers",
description = "Update provider names declared by the app.",
required = false
)
finalize {
document("AndroidManifest.xml").use { document ->
val replacementPackageName = packageNameOption.value
val manifest = document.getElementsByTagName("manifest").item(0) as Element
manifest.setAttribute(
"package",
if (replacementPackageName != packageNameOption.default) {
replacementPackageName
} else {
"${manifest.getAttribute("package")}.revanced"
},
)
val packageName = manifest.getAttribute("package")
val newPackageName = if (replacementPackageName != packageNameOption.default) {
replacementPackageName!!
} else {
"${packageName}.revanced"
}
manifest.setAttribute("package", newPackageName)
if (updatePermissions.value == true) {
val permissions = manifest.getElementsByTagName("permission").asSequence()
val usesPermissions = manifest.getElementsByTagName("uses-permission").asSequence()
(permissions + usesPermissions)
.map { it as Element }
.filter { it.getAttribute("android:name") == "$packageName.$RECEIVER_NOT_EXPORTED" }
.forEach { it.setAttribute("android:name", "$newPackageName.$RECEIVER_NOT_EXPORTED") }
}
if(updateProviders.value == 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))
}
}
}
}
}