Skip to content

Commit 77dbee3

Browse files
committed
feat: Add option to use single threaded writer for dex files
BREAKING CHANGE: This commit gets rid of deprecated constructors.
1 parent cb5e39d commit 77dbee3

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

revanced-patcher/api/revanced-patcher.api

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public final class app/revanced/patcher/PatcherException$CircularDependencyExcep
6767
public final class app/revanced/patcher/PatcherOptions {
6868
public fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;)V
6969
public synthetic fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
70-
public fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lapp/revanced/patcher/logging/Logger;)V
71-
public synthetic fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lapp/revanced/patcher/logging/Logger;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
72-
public final fun copy (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lapp/revanced/patcher/logging/Logger;)Lapp/revanced/patcher/PatcherOptions;
73-
public static synthetic fun copy$default (Lapp/revanced/patcher/PatcherOptions;Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lapp/revanced/patcher/logging/Logger;ILjava/lang/Object;)Lapp/revanced/patcher/PatcherOptions;
70+
public fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Z)V
71+
public synthetic fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
72+
public final fun copy (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Z)Lapp/revanced/patcher/PatcherOptions;
73+
public static synthetic fun copy$default (Lapp/revanced/patcher/PatcherOptions;Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Lapp/revanced/patcher/PatcherOptions;
7474
public fun equals (Ljava/lang/Object;)Z
7575
public fun hashCode ()I
7676
public final fun recreateResourceCacheDirectory ()Ljava/io/File;

revanced-patcher/src/main/kotlin/app/revanced/patcher/PatcherOptions.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package app.revanced.patcher
22

33
import app.revanced.patcher.data.ResourceContext
4-
import app.revanced.patcher.logging.impl.NopLogger
54
import brut.androlib.Config
65
import java.io.File
76
import java.util.logging.Logger
@@ -12,16 +11,15 @@ import java.util.logging.Logger
1211
* @param resourceCachePath The path to the directory to use for caching resources.
1312
* @param aaptBinaryPath The path to a custom aapt binary.
1413
* @param frameworkFileDirectory The path to the directory to cache the framework file in.
15-
* @param unusedLogger The logger to use for logging.
14+
* @param multithreadingDexFileWriter Whether to use multiple threads for writing dex files.
15+
* This can impact memory usage.
1616
*/
17-
data class PatcherOptions
18-
@Deprecated("Use the constructor without the logger parameter instead")
19-
constructor(
17+
data class PatcherOptions(
2018
internal val inputFile: File,
2119
internal val resourceCachePath: File = File("revanced-resource-cache"),
2220
internal val aaptBinaryPath: String? = null,
2321
internal val frameworkFileDirectory: String? = null,
24-
internal val unusedLogger: app.revanced.patcher.logging.Logger = NopLogger
22+
internal val multithreadingDexFileWriter: Boolean = false,
2523
) {
2624
private val logger = Logger.getLogger(PatcherOptions::class.java.name)
2725

@@ -47,6 +45,7 @@ constructor(
4745
* @param aaptBinaryPath The path to a custom aapt binary.
4846
* @param frameworkFileDirectory The path to the directory to cache the framework file in.
4947
*/
48+
@Deprecated("Use the constructor with the multithreadingDexFileWriter parameter instead")
5049
constructor(
5150
inputFile: File,
5251
resourceCachePath: File = File("revanced-resource-cache"),
@@ -57,7 +56,7 @@ constructor(
5756
resourceCachePath,
5857
aaptBinaryPath,
5958
frameworkFileDirectory,
60-
NopLogger
59+
false,
6160
)
6261

6362
fun recreateResourceCacheDirectory() = resourceCachePath.also {

revanced-patcher/src/main/kotlin/app/revanced/patcher/data/BytecodeContext.kt

+17-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import com.android.tools.smali.dexlib2.Opcodes
1212
import com.android.tools.smali.dexlib2.iface.ClassDef
1313
import com.android.tools.smali.dexlib2.iface.DexFile
1414
import com.android.tools.smali.dexlib2.iface.Method
15-
import com.android.tools.smali.dexlib2.writer.io.MemoryDataStore
1615
import lanchon.multidexlib2.BasicDexFileNamer
1716
import lanchon.multidexlib2.DexIO
1817
import lanchon.multidexlib2.MultiDexIO
1918
import java.io.File
19+
import java.io.FileFilter
2020
import java.io.Flushable
2121
import java.util.logging.Logger
2222

@@ -142,16 +142,25 @@ class BytecodeContext internal constructor(private val options: PatcherOptions)
142142
* @return The compiled bytecode.
143143
*/
144144
override fun get(): List<PatcherResult.PatchedDexFile> {
145-
logger.info("Compiling modified dex files")
145+
logger.info("Compiling patched dex files")
146146

147-
return mutableMapOf<String, MemoryDataStore>().apply {
147+
val patchedDexFileResults = options.resourceCachePath.resolve("dex").also {
148+
it.deleteRecursively() // Make sure the directory is empty.
149+
it.mkdirs()
150+
}.apply {
148151
MultiDexIO.writeDexFile(
149-
true, -1, // Defaults to amount of available cores.
150-
this, BasicDexFileNamer(), object : DexFile {
152+
true,
153+
if (options.multithreadingDexFileWriter) -1 else 1,
154+
this,
155+
BasicDexFileNamer(),
156+
object : DexFile {
151157
override fun getClasses() = this@BytecodeContext.classes.also(ProxyClassList::replaceClasses)
152158
override fun getOpcodes() = this@BytecodeContext.opcodes
153-
}, DexIO.DEFAULT_MAX_DEX_POOL_SIZE, null
154-
)
155-
}.map { PatcherResult.PatchedDexFile(it.key, it.value.readAt(0)) }
159+
},
160+
DexIO.DEFAULT_MAX_DEX_POOL_SIZE
161+
) { _, entryName, _ -> logger.info("Compiled $entryName") }
162+
}.listFiles(FileFilter { it.isFile })!!.map { PatcherResult.PatchedDexFile(it.name, it.inputStream()) }
163+
164+
return patchedDexFileResults
156165
}
157166
}

0 commit comments

Comments
 (0)