Skip to content

Commit

Permalink
Merge pull request ReVanced#24 from revanced/non-root
Browse files Browse the repository at this point in the history
feat: support `non-root` installation
  • Loading branch information
oSumAtrIX authored Jun 11, 2022
2 parents 45171dd + 775e2fd commit ee4a83b
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 439 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ group = "app.revanced"

repositories {
mavenCentral()
google()
mavenLocal()
maven {
url = uri("https://maven.pkg.github.com/revanced/multidexlib2")
Expand All @@ -24,11 +25,11 @@ repositories {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.6.21")
implementation("app.revanced:revanced-patcher:1.0.0")
implementation("app.revanced:revanced-patcher:1.1.0")

implementation("info.picocli:picocli:4.6.3")

implementation("com.github.li-wjohnson:jadb:master-SNAPSHOT") // using a fork instead.
implementation("com.android.tools.build:apksig:7.2.1")
implementation("com.github.revanced:jadb:master-SNAPSHOT") // updated fork
implementation("org.bouncycastle:bcpkix-jdk15on:1.70")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.21")
}
Expand All @@ -44,7 +45,7 @@ tasks {
}
shadowJar {
manifest {
attributes("Main-Class" to "app.revanced.cli.MainKt")
attributes("Main-Class" to "app.revanced.cli.main.MainKt")
attributes("Implementation-Title" to project.name)
attributes("Implementation-Version" to project.version)
}
Expand Down
96 changes: 0 additions & 96 deletions src/main/kotlin/app/revanced/cli/MainCommand.kt

This file was deleted.

55 changes: 0 additions & 55 deletions src/main/kotlin/app/revanced/cli/Patcher.kt

This file was deleted.

108 changes: 108 additions & 0 deletions src/main/kotlin/app/revanced/cli/command/MainCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package app.revanced.cli.command

import app.revanced.cli.patcher.Patcher
import app.revanced.cli.signing.Signing
import app.revanced.patcher.PatcherOptions
import app.revanced.patcher.extensions.PatchExtensions.patchName
import app.revanced.patcher.util.patch.implementation.JarPatchBundle
import app.revanced.utils.adb.Adb
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import java.io.File
import java.nio.file.Files

@Command(
name = "ReVanced-CLI", version = ["1.0.0"], mixinStandardHelpOptions = true,
)
internal object MainCommand : Runnable {
@Option(names = ["-a", "--apk"], description = ["Input file to be patched"], required = true)
lateinit var inputFile: File

@Option(names = ["-o", "--out"], description = ["Output file path"], required = true)
lateinit var outputPath: String

@Option(
names = ["-i", "--include"],
description = ["Which patches to include. If none is specified, all compatible default patches will be included"]
)
var includedPatches = arrayOf<String>()

@Option(names = ["-r", "--resource-patcher"], description = ["Disable patching resources"])
var disableResourcePatching: Boolean = false

@Option(names = ["--debugging"], description = ["Disable patch version compatibility"])
var debugging: Boolean = false

@Option(names = ["-m", "--merge"], description = ["One or more dex file containers to merge"])
var mergeFiles = listOf<File>()

@Option(names = ["-b", "--bundles"], description = ["One or more bundles of patches"])
var patchBundles = arrayOf<String>()

@Option(names = ["-l", "--list"], description = ["List patches only"])
var listOnly: Boolean = false

@Option(names = ["--install"], description = ["If specified, instead of mounting, install"])
var install: Boolean = false

@Option(names = ["--cn"], description = ["Overwrite the default CN for the signed file"])
var cn = "ReVanced"

@Option(names = ["-p", "--password"], description = ["Overwrite the default password for the signed file"])
var password = "ReVanced"

@Option(names = ["-d", "--deploy-on"], description = ["If specified, deploy to adb device with given name"])
var deploy: String? = null

@Option(names = ["-t", "--temp-dir"], description = ["Temporal resource cache directory"])
var cacheDirectory = "revanced-cache"

@Option(
names = ["-c", "--clean"],
description = ["Clean the temporal resource cache directory. This will be done anyways when running the patcher"]
)
var clean: Boolean = false

override fun run() {
if (listOnly) {
for (patchBundlePath in patchBundles) for (patch in JarPatchBundle(patchBundlePath).loadPatches()) {
println("[available] ${patch.patchName}")
}
return
}

val patcher = app.revanced.patcher.Patcher(PatcherOptions(inputFile, cacheDirectory, !disableResourcePatching))

val outputFile = File(outputPath)

val adb: Adb? = deploy?.let {
Adb(outputFile, patcher.data.packageMetadata.packageName, deploy!!, install)
}

val patchedFile = if (install) File(cacheDirectory).resolve("${outputFile.nameWithoutExtension}_raw.apk") else outputFile

Patcher.start(patcher, patchedFile)

println("[aligning & signing]")

if (install) {
Signing.start(
patchedFile,
outputFile,
cn,
password,
)
}

if (clean) File(cacheDirectory).deleteRecursively()

adb?.let {
println("[deploying]")
it.deploy()
}

if (clean && deploy != null) Files.delete(outputFile.toPath())

println("[done]")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.cli
package app.revanced.cli.main

import app.revanced.cli.command.MainCommand
import picocli.CommandLine

internal fun main(args: Array<String>) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/app/revanced/cli/patcher/Patcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app.revanced.cli.patcher

import app.revanced.cli.command.MainCommand.cacheDirectory
import app.revanced.cli.command.MainCommand.disableResourcePatching
import app.revanced.cli.command.MainCommand
import app.revanced.cli.command.MainCommand.includedPatches
import app.revanced.utils.filesystem.ZipFileSystemUtils
import app.revanced.utils.patcher.addPatchesFiltered
import app.revanced.utils.patcher.applyPatchesVerbose
import app.revanced.utils.patcher.mergeFiles
import java.io.File
import java.nio.file.Files

internal object Patcher {
internal fun start(patcher: app.revanced.patcher.Patcher, output: File) {
// merge files like necessary integrations
patcher.mergeFiles()
// add patches, but filter incompatible or excluded patches
patcher.addPatchesFiltered(includeFilter = includedPatches.isNotEmpty())
// apply patches
patcher.applyPatchesVerbose()

// write output file
if (output.exists()) Files.delete(output.toPath())
MainCommand.inputFile.copyTo(output)

ZipFileSystemUtils(output).use { fileSystem ->
// replace all dex files
val result = patcher.save()
result.dexFiles.forEach {
fileSystem.write(it.name, it.memoryDataStore.data)
}

// write resources
if (!disableResourcePatching) {
fileSystem.writePathRecursively(File(cacheDirectory).resolve("build").toPath())
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/app/revanced/cli/signing/Signing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app.revanced.cli.signing

import app.revanced.cli.command.MainCommand.cacheDirectory
import app.revanced.utils.signing.Signer
import app.revanced.utils.signing.align.ZipAligner
import java.io.File

object Signing {
fun start(inputFile: File, outputFile: File, cn: String, password: String) {
val cacheDirectory = File(cacheDirectory)
val alignedOutput = cacheDirectory.resolve("${outputFile.nameWithoutExtension}_aligned.apk")
val signedOutput = cacheDirectory.resolve("${outputFile.nameWithoutExtension}_signed.apk")

// align the inputFile and write to alignedOutput
ZipAligner.align(inputFile, alignedOutput)
// sign the alignedOutput and write to signedOutput
// the reason is, in case the signer fails
// it does not damage the output file
Signer(cn, password).signApk(alignedOutput, signedOutput)

// afterwards copy over the file to the output
signedOutput.copyTo(outputFile, true)
}
}
Loading

0 comments on commit ee4a83b

Please sign in to comment.