Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)
implementation(libs.picocli)
implementation(files("libs/APKEditor-1.4.7.jar"))

testImplementation(libs.kotlin.test)
}
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ kotlinx = "1.9.0"
picocli = "4.7.7"
morphe-patcher = "1.1.0"
morphe-library = "1.1.0"
arsclib = "1.3.8"

[libraries]
arsclib = { module = "io.github.reandroid:ARSCLib", version.ref = "arsclib" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx" }
Expand Down
Binary file added libs/APKEditor-1.4.7.jar
Binary file not shown.
39 changes: 36 additions & 3 deletions src/main/kotlin/app/morphe/cli/command/PatchCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import app.morphe.patcher.Patcher
import app.morphe.patcher.PatcherConfig
import app.morphe.patcher.patch.Patch
import app.morphe.patcher.patch.loadPatchesFromJar
import com.reandroid.apkeditor.merge.Merger
import com.reandroid.apkeditor.merge.MergerOptions
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -260,7 +262,7 @@ internal object PatchCommand : Runnable {

val outputFilePath =
outputFilePath ?: File("").absoluteFile.resolve(
"${apk.nameWithoutExtension}-patched.${apk.extension}",
"${apk.nameWithoutExtension}-patched.apk",
)

val temporaryFilesPath =
Expand Down Expand Up @@ -312,12 +314,35 @@ internal object PatchCommand : Runnable {

val patcherTemporaryFilesPath = temporaryFilesPath.resolve("patcher")

// Checking if the file is in apkm format (like reddit)
var mergedApkToCleanup: File? = null
val inputApk = if (apk.extension.equals("apkm", ignoreCase = true)) {
logger.info("Detected APKM file, converting to APK...")

// Save merged APK to output directory (will be cleaned up after patching)
val outputApk = outputFilePath.parentFile.resolve("${apk.nameWithoutExtension}-merged.apk")

// Use APKEditor's Merger directly (handles extraction and merging)
val mergerOptions = MergerOptions().apply {
inputFile = apk // Original APKM file
outputFile = outputApk
cleanMeta = true
}
Merger(mergerOptions).run()

logger.info("Conversion complete: ${outputApk.path}")
mergedApkToCleanup = outputApk
outputApk
} else {
apk
}

val patchingResult = PatchingResult()

try {
val (packageName, patcherResult) = Patcher(
PatcherConfig(
apk,
inputApk,
patcherTemporaryFilesPath,
aaptBinaryPath?.path,
patcherTemporaryFilesPath.absolutePath,
Expand Down Expand Up @@ -377,7 +402,7 @@ internal object PatchCommand : Runnable {

// region Save.

apk.copyTo(temporaryFilesPath.resolve(apk.name), overwrite = true).apply {
inputApk.copyTo(temporaryFilesPath.resolve(inputApk.name), overwrite = true).apply {
patchingResult.addStepResult(
PatchingStep.REBUILDING,
{
Expand Down Expand Up @@ -406,6 +431,7 @@ internal object PatchCommand : Runnable {
patchedApkFile.copyTo(outputFilePath, overwrite = true)
}
}

logger.info("Saved to $outputFilePath")

// endregion
Expand Down Expand Up @@ -448,6 +474,13 @@ internal object PatchCommand : Runnable {
logger.info("Purging temporary files")
purge(temporaryFilesPath)
}

// Clean up merged APK if we created one from APKM
mergedApkToCleanup?.let {
if (it.delete()) {
logger.info("Cleaned up merged APK: ${it.path}")
}
}
}

/**
Expand Down