fix: supply the parent classloader to DexClassLoader

This commit is contained in:
oSumAtrIX 2023-08-20 19:13:57 +02:00
parent 273dd8d388
commit 0f15077225
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -5,7 +5,7 @@ package app.revanced.patcher
import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively
import app.revanced.patcher.patch.Patch import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchClass import app.revanced.patcher.patch.PatchClass
import dalvik.system.PathClassLoader import dalvik.system.DexClassLoader
import lanchon.multidexlib2.BasicDexFileNamer import lanchon.multidexlib2.BasicDexFileNamer
import lanchon.multidexlib2.MultiDexIO import lanchon.multidexlib2.MultiDexIO
import java.io.File import java.io.File
@ -36,43 +36,36 @@ sealed class PatchBundleLoader private constructor(
* *
* @param patchBundles The path to patch bundles of JAR format. * @param patchBundles The path to patch bundles of JAR format.
*/ */
class Jar(private vararg val patchBundles: File) : PatchBundleLoader( class Jar(vararg patchBundles: File) :
with(URLClassLoader(patchBundles.map { it.toURI().toURL() }.toTypedArray())) { PatchBundleLoader(with(URLClassLoader(patchBundles.map { it.toURI().toURL() }.toTypedArray())) {
patchBundles.flatMap { patchBundle -> patchBundles.flatMap { patchBundle ->
// Get the names of all classes in the DEX file. // Get the names of all classes in the DEX file.
JarFile(patchBundle).entries().asSequence() JarFile(patchBundle).entries().asSequence()
.filter { it.name.endsWith(".class") } .filter { it.name.endsWith(".class") }
.map { .map { it.name.replace('/', '.').replace(".class", "") }
loadClass(it.name.replace('/', '.').replace(".class", "")) .map { loadClass(it) }
}
} }
} })
)
/** /**
* A [PatchBundleLoader] for [Dex] files. * A [PatchBundleLoader] for [Dex] files.
* *
* @param patchBundlesPath The path to or a path to a directory containing patch bundles of DEX format. * @param patchBundles The path to patch bundles of DEX format.
*/ */
class Dex(private val patchBundlesPath: File) : PatchBundleLoader( class Dex(vararg patchBundles: File) : PatchBundleLoader(with(
with(PathClassLoader(patchBundlesPath.absolutePath, null)) { DexClassLoader(
fun readDexFile(file: File) = MultiDexIO.readDexFile( patchBundles.joinToString(File.pathSeparator) { it.absolutePath },
true, null,
file, null,
BasicDexFileNamer(), PatchBundleLoader::class.java.classLoader
null, )
null ) {
) patchBundles
.flatMap {
// Get the names of all classes in the DEX file. MultiDexIO.readDexFile(true, it, BasicDexFileNamer(), null, null).classes
}
val dexFiles = if (patchBundlesPath.isFile) listOf(readDexFile(patchBundlesPath)) .map { classDef -> classDef.type.substring(1, classDef.length - 1) }
else patchBundlesPath.listFiles { it -> it.isFile }?.map { readDexFile(it) } ?: emptyList() .map { loadClass(it) }
})
dexFiles.flatMap { it.classes }.map { classDef ->
classDef.type.substring(1, classDef.length - 1).replace('/', '.')
}.map { loadClass(it) }
}
)
} }