mirror of
https://github.com/inotia00/revanced-patches.git
synced 2025-06-13 05:37:40 +02:00
fix(Reddit/Change package name): force close occurs when patch is applied from RVX Manager
This commit is contained in:
@ -7,6 +7,7 @@ import app.revanced.patcher.patch.annotation.CompatiblePackage
|
|||||||
import app.revanced.patcher.patch.annotation.Patch
|
import app.revanced.patcher.patch.annotation.Patch
|
||||||
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption
|
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption
|
||||||
import org.w3c.dom.Element
|
import org.w3c.dom.Element
|
||||||
|
import java.io.Closeable
|
||||||
|
|
||||||
@Patch(
|
@Patch(
|
||||||
name = "Change package name",
|
name = "Change package name",
|
||||||
@ -15,11 +16,14 @@ import org.w3c.dom.Element
|
|||||||
use = false
|
use = false
|
||||||
)
|
)
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
object ChangePackageNamePatch : ResourcePatch() {
|
object ChangePackageNamePatch : ResourcePatch(), Closeable {
|
||||||
private const val PACKAGE_NAME_REDDIT = "com.reddit.frontpage"
|
private const val PACKAGE_NAME_REDDIT = "com.reddit.frontpage"
|
||||||
private const val CLONE_PACKAGE_NAME_REDDIT = "$PACKAGE_NAME_REDDIT.revanced"
|
private const val CLONE_PACKAGE_NAME_REDDIT = "$PACKAGE_NAME_REDDIT.revanced"
|
||||||
private const val DEFAULT_PACKAGE_NAME_REDDIT = "$PACKAGE_NAME_REDDIT.rvx"
|
private const val DEFAULT_PACKAGE_NAME_REDDIT = "$PACKAGE_NAME_REDDIT.rvx"
|
||||||
|
|
||||||
|
private lateinit var context: ResourceContext
|
||||||
|
private lateinit var redditPackageName: String
|
||||||
|
|
||||||
private val PackageNameReddit by stringPatchOption(
|
private val PackageNameReddit by stringPatchOption(
|
||||||
key = "PackageNameReddit",
|
key = "PackageNameReddit",
|
||||||
default = DEFAULT_PACKAGE_NAME_REDDIT,
|
default = DEFAULT_PACKAGE_NAME_REDDIT,
|
||||||
@ -32,36 +36,41 @@ object ChangePackageNamePatch : ResourcePatch() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
override fun execute(context: ResourceContext) {
|
override fun execute(context: ResourceContext) {
|
||||||
val redditPackageName = PackageNameReddit
|
this.context = context
|
||||||
|
|
||||||
|
redditPackageName = PackageNameReddit
|
||||||
?: throw PatchException("Invalid package name.")
|
?: throw PatchException("Invalid package name.")
|
||||||
|
|
||||||
// Ensure device runs Android.
|
// Ensure device runs Android.
|
||||||
try {
|
try {
|
||||||
// Android, RVX Manager
|
// RVX Manager
|
||||||
// ====
|
// ====
|
||||||
// For some reason, in Android AAPT2, a compilation error occurs when changing the [strings.xml] of the Reddit
|
// For some reason, in Android AAPT2, a compilation error occurs when changing the [strings.xml] of the Reddit
|
||||||
// This only affects RVX Manager, and has not yet found a valid workaround
|
// This only affects RVX Manager, and has not yet found a valid workaround
|
||||||
Class.forName("android.os.Environment")
|
Class.forName("android.os.Environment")
|
||||||
context.replaceManifest(redditPackageName)
|
|
||||||
} catch (_: ClassNotFoundException) {
|
} catch (_: ClassNotFoundException) {
|
||||||
// CLI
|
// CLI
|
||||||
context.replaceManifestAndString(redditPackageName)
|
context.replacePackageName(redditPackageName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ResourceContext.replaceManifest(redditPackageName: String) {
|
override fun close() {
|
||||||
this["AndroidManifest.xml"].apply {
|
context["AndroidManifest.xml"].apply {
|
||||||
writeText(
|
writeText(
|
||||||
readText()
|
readText()
|
||||||
.replace(
|
.replace(
|
||||||
"package=\"$PACKAGE_NAME_REDDIT",
|
"package=\"$PACKAGE_NAME_REDDIT",
|
||||||
"package=\"$redditPackageName"
|
"package=\"$redditPackageName"
|
||||||
)
|
)
|
||||||
|
.replace(
|
||||||
|
"$PACKAGE_NAME_REDDIT.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION",
|
||||||
|
"$redditPackageName.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ResourceContext.replaceManifestAndString(redditPackageName: String) {
|
private fun ResourceContext.replacePackageName(redditPackageName: String) {
|
||||||
// replace strings
|
// replace strings
|
||||||
this.xmlEditor["res/values/strings.xml"].use { editor ->
|
this.xmlEditor["res/values/strings.xml"].use { editor ->
|
||||||
val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element
|
val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element
|
||||||
@ -88,14 +97,6 @@ object ChangePackageNamePatch : ResourcePatch() {
|
|||||||
"android:authorities=\"$PACKAGE_NAME_REDDIT",
|
"android:authorities=\"$PACKAGE_NAME_REDDIT",
|
||||||
"android:authorities=\"$redditPackageName"
|
"android:authorities=\"$redditPackageName"
|
||||||
)
|
)
|
||||||
.replace(
|
|
||||||
"package=\"$PACKAGE_NAME_REDDIT",
|
|
||||||
"package=\"$redditPackageName"
|
|
||||||
)
|
|
||||||
.replace(
|
|
||||||
"$PACKAGE_NAME_REDDIT.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION",
|
|
||||||
"$redditPackageName.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user