forked from ReVanced/revanced-patches-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
change-package-name
patch (ReVanced#1864)
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/app/revanced/patches/all/misc/packagename/patch/ChangePackageNamePatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
) | ||
) | ||
} | ||
} |