mirror of
https://github.com/revanced/revanced-patches
synced 2025-01-11 23:35:48 +01:00
feat(Change package name): Append .revanced
to package name by default (#2750)
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
parent
51a2bf2f02
commit
e83e62fc24
@ -9,39 +9,43 @@ import org.w3c.dom.Element
|
|||||||
|
|
||||||
@Patch(false)
|
@Patch(false)
|
||||||
@Name("Change package name")
|
@Name("Change package name")
|
||||||
@Description("Changes the package name.")
|
@Description("Changes the package name. Appends \".revanced\" to the package name by default.")
|
||||||
class ChangePackageNamePatch : ResourcePatch {
|
class ChangePackageNamePatch : ResourcePatch {
|
||||||
override fun execute(context: ResourceContext): PatchResult {
|
override fun execute(context: ResourceContext): PatchResult {
|
||||||
packageName?.let { packageName ->
|
val packageNameToUse = packageName ?: getDefaultPackageName(context)
|
||||||
val packageNameRegex = Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$")
|
|
||||||
if (!packageName.matches(packageNameRegex))
|
|
||||||
return PatchResultError("Invalid package name")
|
|
||||||
|
|
||||||
var originalPackageName: String
|
val packageNameRegex = Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$")
|
||||||
context.xmlEditor["AndroidManifest.xml"].use { editor ->
|
if (!packageNameToUse.matches(packageNameRegex))
|
||||||
val manifest = editor.file.getElementsByTagName("manifest").item(0) as Element
|
return PatchResultError("Invalid package name")
|
||||||
originalPackageName = manifest.getAttribute("package")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!originalPackageName.matches(packageNameRegex))
|
val originalPackageName = getOriginalPackageName(context)
|
||||||
return PatchResultError("Failed to get the original package name")
|
|
||||||
|
|
||||||
context["AndroidManifest.xml"].apply {
|
context["AndroidManifest.xml"].apply {
|
||||||
readText().replace(originalPackageName, packageName).let(::writeText)
|
readText().replace(originalPackageName, packageNameToUse).let(::writeText)
|
||||||
}
|
}
|
||||||
|
|
||||||
} ?: return PatchResultError("No package name provided")
|
|
||||||
|
|
||||||
return PatchResultSuccess()
|
return PatchResultSuccess()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getDefaultPackageName(context: ResourceContext): String {
|
||||||
|
val originalPackageName = getOriginalPackageName(context)
|
||||||
|
return "$originalPackageName.revanced"
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getOriginalPackageName(context: ResourceContext): String {
|
||||||
|
context.xmlEditor["AndroidManifest.xml"].use { editor ->
|
||||||
|
val manifest = editor.file.getElementsByTagName("manifest").item(0) as Element
|
||||||
|
return manifest.getAttribute("package")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object : OptionsContainer() {
|
companion object : OptionsContainer() {
|
||||||
var packageName: String? by option(
|
var packageName: String? by option(
|
||||||
PatchOption.StringOption(
|
PatchOption.StringOption(
|
||||||
key = "packageName",
|
key = "packageName",
|
||||||
default = null,
|
default = null,
|
||||||
title = "Package name",
|
title = "Package name",
|
||||||
description = "The name of the package to rename of the app.",
|
description = "The name of the package to rename the app to.",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user