mirror of
https://github.com/revanced/revanced-cli.git
synced 2024-06-10 07:08:06 +02:00
67 lines
2.1 KiB
Kotlin
67 lines
2.1 KiB
Kotlin
package app.revanced.lib
|
|
|
|
import app.revanced.lib.signing.ApkSigner
|
|
import app.revanced.lib.signing.SigningOptions
|
|
import app.revanced.lib.zip.ZipFile
|
|
import app.revanced.lib.zip.structures.ZipEntry
|
|
import app.revanced.patcher.PatcherResult
|
|
import java.io.File
|
|
import java.util.logging.Logger
|
|
import kotlin.io.path.deleteIfExists
|
|
|
|
@Suppress("MemberVisibilityCanBePrivate", "unused")
|
|
object ApkUtils {
|
|
private val logger = Logger.getLogger(ApkUtils::class.java.name)
|
|
|
|
/**
|
|
* Creates a new apk from [apkFile] and [patchedEntriesSource] and writes it to [outputFile].
|
|
*
|
|
* @param apkFile The apk to copy entries from.
|
|
* @param outputFile The apk to write the new entries to.
|
|
* @param patchedEntriesSource The result of the patcher to add the patched dex files and resources.
|
|
*/
|
|
fun copyAligned(apkFile: File, outputFile: File, patchedEntriesSource: PatcherResult) {
|
|
logger.info("Aligning ${apkFile.name}")
|
|
|
|
outputFile.toPath().deleteIfExists()
|
|
|
|
ZipFile(outputFile).use { file ->
|
|
patchedEntriesSource.dexFiles.forEach {
|
|
file.addEntryCompressData(
|
|
ZipEntry(it.name), it.stream.readBytes()
|
|
)
|
|
}
|
|
|
|
patchedEntriesSource.resourceFile?.let {
|
|
file.copyEntriesFromFileAligned(
|
|
ZipFile(it), ZipFile.apkZipEntryAlignment
|
|
)
|
|
}
|
|
|
|
// TODO: Do not compress result.doNotCompress
|
|
|
|
// TODO: Fix copying resources that are not needed anymore.
|
|
file.copyEntriesFromFileAligned(
|
|
ZipFile(apkFile), ZipFile.apkZipEntryAlignment
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Signs the apk at [apk] and writes it to [output].
|
|
*
|
|
* @param apk The apk to sign.
|
|
* @param output The apk to write the signed apk to.
|
|
* @param signingOptions The options to use for signing.
|
|
*/
|
|
fun sign(
|
|
apk: File,
|
|
output: File,
|
|
signingOptions: SigningOptions,
|
|
) {
|
|
logger.info("Signing ${apk.name}")
|
|
|
|
ApkSigner(signingOptions).signApk(apk, output)
|
|
}
|
|
} |