diff --git a/patches/src/main/kotlin/app/revanced/patches/googlenews/misc/gms/GmsCoreSupportPatch.kt b/patches/src/main/kotlin/app/revanced/patches/googlenews/misc/gms/GmsCoreSupportPatch.kt index d03ce31e7d..4644ddd141 100644 --- a/patches/src/main/kotlin/app/revanced/patches/googlenews/misc/gms/GmsCoreSupportPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/googlenews/misc/gms/GmsCoreSupportPatch.kt @@ -11,7 +11,7 @@ import app.revanced.patches.shared.misc.gms.gmsCoreSupportResourcePatch val gmsCoreSupportPatch = gmsCoreSupportPatch( fromPackageName = MAGAZINES_PACKAGE_NAME, toPackageName = REVANCED_MAGAZINES_PACKAGE_NAME, - mainActivityOnCreateFingerprint = magazinesActivityOnCreateFingerprint, + mainActivityOnCreateFingerprintToInsertIndex = magazinesActivityOnCreateFingerprint to { 0 }, extensionPatch = extensionPatch, gmsCoreSupportResourcePatchFactory = ::gmsCoreSupportResourcePatch, ) { diff --git a/patches/src/main/kotlin/app/revanced/patches/googlephotos/misc/gms/GmsCoreSupportPatch.kt b/patches/src/main/kotlin/app/revanced/patches/googlephotos/misc/gms/GmsCoreSupportPatch.kt index 3ed14a29dd..4a27e93cae 100644 --- a/patches/src/main/kotlin/app/revanced/patches/googlephotos/misc/gms/GmsCoreSupportPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/googlephotos/misc/gms/GmsCoreSupportPatch.kt @@ -5,12 +5,24 @@ import app.revanced.patches.googlephotos.misc.extension.extensionPatch import app.revanced.patches.googlephotos.misc.gms.Constants.PHOTOS_PACKAGE_NAME import app.revanced.patches.googlephotos.misc.gms.Constants.REVANCED_PHOTOS_PACKAGE_NAME import app.revanced.patches.shared.misc.gms.gmsCoreSupportPatch +import app.revanced.util.getReference +import app.revanced.util.indexOfFirstInstructionOrThrow +import com.android.tools.smali.dexlib2.iface.reference.MethodReference @Suppress("unused") val gmsCoreSupportPatch = gmsCoreSupportPatch( fromPackageName = PHOTOS_PACKAGE_NAME, toPackageName = REVANCED_PHOTOS_PACKAGE_NAME, - mainActivityOnCreateFingerprint = homeActivityOnCreateFingerprint, + mainActivityOnCreateFingerprintToInsertIndex = homeActivityOnCreateFingerprint to { + val index = homeActivityOnCreateFingerprint.method.indexOfFirstInstructionOrThrow { + getReference()?.name == "getApplicationContext" + } + + // Below the move-result-object instruction, + // because the extension patch is used by the GmsCore support patch + // which hooks the getApplicationContext call. + index + 2 + }, extensionPatch = extensionPatch, gmsCoreSupportResourcePatchFactory = ::gmsCoreSupportResourcePatch, ) { diff --git a/patches/src/main/kotlin/app/revanced/patches/music/misc/gms/GmsCoreSupportPatch.kt b/patches/src/main/kotlin/app/revanced/patches/music/misc/gms/GmsCoreSupportPatch.kt index 1a7a58db3e..5d74ef8be9 100644 --- a/patches/src/main/kotlin/app/revanced/patches/music/misc/gms/GmsCoreSupportPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/music/misc/gms/GmsCoreSupportPatch.kt @@ -25,7 +25,7 @@ val gmsCoreSupportPatch = gmsCoreSupportPatch( earlyReturnFingerprints = setOf( castContextFetchFingerprint, ), - mainActivityOnCreateFingerprint = musicActivityOnCreateFingerprint, + mainActivityOnCreateFingerprintToInsertIndex = musicActivityOnCreateFingerprint to { 0 }, extensionPatch = sharedExtensionPatch, gmsCoreSupportResourcePatchFactory = ::gmsCoreSupportResourcePatch, ) { diff --git a/patches/src/main/kotlin/app/revanced/patches/shared/misc/gms/GmsCoreSupportPatch.kt b/patches/src/main/kotlin/app/revanced/patches/shared/misc/gms/GmsCoreSupportPatch.kt index 623f60b2fb..5f10d80967 100644 --- a/patches/src/main/kotlin/app/revanced/patches/shared/misc/gms/GmsCoreSupportPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/shared/misc/gms/GmsCoreSupportPatch.kt @@ -35,7 +35,8 @@ private const val PACKAGE_NAME_REGEX_PATTERN = "^[a-z]\\w*(\\.[a-z]\\w*)+\$" * @param toPackageName The package name to fall back to if no custom package name is specified in patch options. * @param primeMethodFingerprint The fingerprint of the "prime" method that needs to be patched. * @param earlyReturnFingerprints The fingerprints of methods that need to be returned early. - * @param mainActivityOnCreateFingerprint The fingerprint of the main activity onCreate method. + * @param mainActivityOnCreateFingerprintToInsertIndex The fingerprint of the main activity onCreate method + * and a function to get the index to insert the GmsCore check instruction at. * @param extensionPatch The patch responsible for the extension. * @param gmsCoreSupportResourcePatchFactory The factory for the corresponding resource patch * that is used to patch the resources. @@ -47,7 +48,7 @@ fun gmsCoreSupportPatch( toPackageName: String, primeMethodFingerprint: Fingerprint? = null, earlyReturnFingerprints: Set = setOf(), - mainActivityOnCreateFingerprint: Fingerprint, + mainActivityOnCreateFingerprintToInsertIndex: Pair Int>, extensionPatch: Patch<*>, gmsCoreSupportResourcePatchFactory: (gmsCoreVendorGroupIdOption: Option) -> Patch<*>, executeBlock: BytecodePatchContext.() -> Unit = {}, @@ -189,11 +190,14 @@ fun gmsCoreSupportPatch( originalPackageNameExtensionFingerprint.method.returnEarly(fromPackageName) // Verify GmsCore is installed and whitelisted for power optimizations and background usage. - mainActivityOnCreateFingerprint.method.addInstruction( - 0, - "invoke-static/range { p0 .. p0 }, $EXTENSION_CLASS_DESCRIPTOR->" + - "checkGmsCore(Landroid/app/Activity;)V", - ) + + mainActivityOnCreateFingerprintToInsertIndex.let { (fingerprint, getInsertIndex) -> + fingerprint.method.addInstruction( + getInsertIndex(), + "invoke-static/range { p0 .. p0 }, $EXTENSION_CLASS_DESCRIPTOR->" + + "checkGmsCore(Landroid/app/Activity;)V", + ) + } // Change the vendor of GmsCore in the extension. getGmsCoreVendorGroupIdFingerprint.method.returnEarly(gmsCoreVendorGroupId) diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/gms/GmsCoreSupportPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/gms/GmsCoreSupportPatch.kt index 9986ed9a06..605f904479 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/gms/GmsCoreSupportPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/gms/GmsCoreSupportPatch.kt @@ -26,7 +26,7 @@ val gmsCoreSupportPatch = gmsCoreSupportPatch( earlyReturnFingerprints = setOf( castContextFetchFingerprint, ), - mainActivityOnCreateFingerprint = mainActivityOnCreateFingerprint, + mainActivityOnCreateFingerprintToInsertIndex = mainActivityOnCreateFingerprint to { 0 }, extensionPatch = sharedExtensionPatch, gmsCoreSupportResourcePatchFactory = ::gmsCoreSupportResourcePatch, ) {