feat(YouTube Music - Vanced MicroG support): Allow changing package name

This commit is contained in:
oSumAtrIX 2023-11-16 20:31:50 +01:00
parent fb373bc9a0
commit ede2d67b92
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
3 changed files with 74 additions and 41 deletions

View file

@ -4,7 +4,9 @@ import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption
import app.revanced.patcher.patch.options.PatchOptionException
import org.w3c.dom.Element
import java.io.Closeable
@Patch(
name = "Change package name",
@ -12,13 +14,11 @@ import org.w3c.dom.Element
use = false
)
@Suppress("unused")
object ChangePackageNamePatch : ResourcePatch() {
private const val DEFAULT_PACKAGE_NAME_OPTION = "Default"
private var packageName by stringPatchOption(
object ChangePackageNamePatch : ResourcePatch(), Closeable {
private val packageNameOption = stringPatchOption(
key = "packageName",
default = DEFAULT_PACKAGE_NAME_OPTION,
values = mapOf("Default" to DEFAULT_PACKAGE_NAME_OPTION),
default = "Default",
values = mapOf("Default" to "Default"),
title = "Package name",
description = "The name of the package to rename the app to.",
required = true
@ -26,19 +26,37 @@ object ChangePackageNamePatch : ResourcePatch() {
it == "Default" || it!!.matches(Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$"))
}
private lateinit var context: ResourceContext
override fun execute(context: ResourceContext) {
fun getOriginalPackageName(context: ResourceContext): String {
context.xmlEditor["AndroidManifest.xml"].use { editor ->
this.context = context
}
/**
* Set the package name to use.
* If this is called multiple times, the first call will set the package name.
*
* @param fallbackPackageName The package name to use if the user has not already specified a package name.
* @return The package name that was set.
* @throws PatchOptionException.ValueValidationException If the package name is invalid.
*/
fun setOrGetFallbackPackageName(fallbackPackageName: String): String {
val packageName = this.packageNameOption.value!!
return if (packageName == this.packageNameOption.default)
fallbackPackageName.also { this.packageNameOption.value = it }
else
packageName
}
override fun close() = context.xmlEditor["AndroidManifest.xml"].use { editor ->
val manifest = editor.file.getElementsByTagName("manifest").item(0) as Element
return manifest.getAttribute("package")
}
}
val originalPackageName = manifest.getAttribute("package")
val originalPackageName = getOriginalPackageName(context)
if (packageName == DEFAULT_PACKAGE_NAME_OPTION) packageName = "$originalPackageName.revanced"
var replacementPackageName = this.packageNameOption.value
if (replacementPackageName == this.packageNameOption.default)
replacementPackageName = "$originalPackageName.revanced"
context["AndroidManifest.xml"].apply {
readText().replace(originalPackageName, packageName!!).let(::writeText)
}
manifest.setAttribute("package", replacementPackageName)
}
}

View file

@ -4,18 +4,25 @@ import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.annotation.CompatiblePackage
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.all.misc.packagename.ChangePackageNamePatch
import app.revanced.patches.music.misc.microg.fingerprints.*
import app.revanced.patches.music.misc.microg.shared.Constants.MUSIC_PACKAGE_NAME
import app.revanced.patches.music.misc.microg.shared.Constants.REVANCED_MUSIC_PACKAGE_NAME
import app.revanced.patches.youtube.misc.microg.shared.Constants
import app.revanced.util.microg.MicroGBytecodeHelper
@Patch(
name = "Vanced MicroG support",
description = "Allows YouTube Music to run without root and under a different package name.",
dependencies = [MicroGResourcePatch::class],
compatiblePackages = [CompatiblePackage("com.google.android.apps.youtube.music")]
dependencies = [
ChangePackageNamePatch::class,
MicroGResourcePatch::class,
],
compatiblePackages = [
CompatiblePackage(
"com.google.android.apps.youtube.music"
)
]
)
@Suppress("unused")
object MicroGBytecodePatch : BytecodePatch(
@ -35,18 +42,21 @@ object MicroGBytecodePatch : BytecodePatch(
// - "com.google.android.gms.phenotype.PACKAGE_NAME",
// - "com.google.android.gms.phenotype.UPDATE",
// - "com.google.android.gms.phenotype",
override fun execute(context: BytecodeContext) = MicroGBytecodeHelper.patchBytecode(
override fun execute(context: BytecodeContext) {
val packageName = ChangePackageNamePatch.setOrGetFallbackPackageName(REVANCED_MUSIC_PACKAGE_NAME)
MicroGBytecodeHelper.patchBytecode(
context,
arrayOf(
MicroGBytecodeHelper.packageNameTransform(
Constants.PACKAGE_NAME,
Constants.REVANCED_PACKAGE_NAME
MUSIC_PACKAGE_NAME,
packageName
)
),
MicroGBytecodeHelper.PrimeMethodTransformationData(
PrimeFingerprint,
MUSIC_PACKAGE_NAME,
REVANCED_MUSIC_PACKAGE_NAME
packageName
),
listOf(
ServiceCheckFingerprint,
@ -57,3 +67,4 @@ object MicroGBytecodePatch : BytecodePatch(
)
)
}
}

View file

@ -3,6 +3,7 @@ package app.revanced.patches.music.misc.microg
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.all.misc.packagename.ChangePackageNamePatch
import app.revanced.patches.music.misc.microg.shared.Constants.MUSIC_PACKAGE_NAME
import app.revanced.patches.music.misc.microg.shared.Constants.REVANCED_MUSIC_APP_NAME
import app.revanced.patches.music.misc.microg.shared.Constants.REVANCED_MUSIC_PACKAGE_NAME
@ -14,15 +15,18 @@ import app.revanced.util.microg.MicroGResourceHelper
@Patch(
description = "Resource patch to allow YouTube Music ReVanced to run without root " +
"and under a different package name."
"and under a different package name.",
dependencies = [ChangePackageNamePatch::class]
)
object MicroGResourcePatch : ResourcePatch() {
override fun execute(context: ResourceContext) {
val packageName = ChangePackageNamePatch.setOrGetFallbackPackageName(REVANCED_MUSIC_PACKAGE_NAME)
// update manifest
MicroGResourceHelper.patchManifest(
context,
MUSIC_PACKAGE_NAME,
REVANCED_MUSIC_PACKAGE_NAME,
packageName,
REVANCED_MUSIC_APP_NAME
)