Skip to content

Commit

Permalink
feat: change-package-name patch (ReVanced#1864)
Browse files Browse the repository at this point in the history
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
andrisas and oSumAtrIX committed Apr 16, 2023
1 parent e58038f commit f9a6672
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package app.revanced.patches.all.misc.packagename.patch

import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.*
import app.revanced.patcher.patch.annotations.Patch
import org.w3c.dom.Element

@Patch(false)
@Name("change-package-name")
@Description("Changes the package name.")
@Version("0.0.1")
class ChangePackageNamePatch : ResourcePatch {
override fun execute(context: ResourceContext): PatchResult {
packageName?.let { packageName ->
val packageNameRegex = Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$")
if (!packageName.matches(packageNameRegex))
return PatchResultError("Invalid package name")

var originalPackageName = ""
context.xmlEditor["AndroidManifest.xml"].use { editor ->
val manifest = editor.file.getElementsByTagName("manifest").item(0) as Element
originalPackageName = manifest.getAttribute("package")
}

if (!originalPackageName.matches(packageNameRegex))
return PatchResultError("Failed to get the original package name")

context["AndroidManifest.xml"].apply {
readText().replace(originalPackageName, packageName).let(::writeText)
}

} ?: return PatchResultError("No package name provided")

return PatchResultSuccess()
}

companion object : OptionsContainer() {
var packageName: String? by option(
PatchOption.StringOption(
key = "packageName",
default = "",
title = "Package name",
description = "The name of the package to rename of the app.",
)
)
}
}

0 comments on commit f9a6672

Please sign in to comment.