mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-05 11:30:47 -05:00
app: Remove dynamic code loading and simplify stub
Remove dynamic APK class and resource loading across :app, :shared, and :stub. Consolidate stub resources into :stub and remove stub obfuscation and encryption. - Merge :stub-res into :stub and delete the :stub-res module. - Delete DynLoad, DelegateComponentFactory, ClassLoaders, StubApplication, StubRootService, and dummy component classes. - Delete StubApk, DynamicClassLoader, and CompoundEnumeration from :shared. - Remove lsparanoid, stub component randomization, resource encryption, and Stub.kt from build-logic. - Remove stubVersion and STUB_VERSION tracking. - Simplify DownloadActivity to use direct resources and APKInstall. - Remove isRunningAsStub and stub-specific hooks across :core. Assisted-by: Gemini 4 Argon
This commit is contained in:
@@ -4,12 +4,6 @@ import org.gradle.api.Project
|
||||
import org.gradle.kotlin.dsl.provideDelegate
|
||||
import java.io.File
|
||||
import java.util.Properties
|
||||
import java.util.Random
|
||||
|
||||
// Set non-zero value here to fix the random seed for reproducible builds
|
||||
// CI builds are always reproducible
|
||||
val RAND_SEED = if (System.getenv("CI") != null) 42 else 0
|
||||
lateinit var RANDOM: Random
|
||||
val ABI_SUPPORT_LIST = listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64", "riscv64")
|
||||
|
||||
private val props = Properties()
|
||||
@@ -28,7 +22,6 @@ object Config {
|
||||
|
||||
// Properties from gradle.properties, should always exist
|
||||
val versionCode: Int get() = get("magisk.versionCode")!!.toInt()
|
||||
val stubVersion: String get() = get("magisk.stubVersion")!!
|
||||
}
|
||||
|
||||
fun Project.rootFile(path: String): File {
|
||||
@@ -41,7 +34,6 @@ class MagiskPlugin : Plugin<Project> {
|
||||
override fun apply(project: Project) = project.applyPlugin()
|
||||
|
||||
private fun Project.applyPlugin() {
|
||||
initRandom(rootProject.file("dict.txt"))
|
||||
props.clear()
|
||||
|
||||
// Get gradle properties relevant to Magisk
|
||||
|
||||
@@ -262,8 +262,7 @@ fun Project.setupAppCommon() {
|
||||
// Always add a transformation to set comments on the APK
|
||||
this.transformations.add {
|
||||
it.eocdComment = ("version=${Config.version}\n" +
|
||||
"versionCode=${Config.versionCode}\n" +
|
||||
"stubVersion=${Config.stubVersion}\n").toByteArray()
|
||||
"versionCode=${Config.versionCode}\n").toByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,309 +0,0 @@
|
||||
import com.android.build.api.artifact.SingleArtifact
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.file.DirectoryProperty
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.tasks.CacheableTask
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.InputFile
|
||||
import org.gradle.api.tasks.OutputDirectory
|
||||
import org.gradle.api.tasks.OutputFile
|
||||
import org.gradle.api.tasks.PathSensitive
|
||||
import org.gradle.api.tasks.PathSensitivity
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.kotlin.dsl.assign
|
||||
import org.gradle.kotlin.dsl.register
|
||||
import org.gradle.kotlin.dsl.withType
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.PrintStream
|
||||
import java.security.SecureRandom
|
||||
import java.util.Random
|
||||
import java.util.zip.Deflater
|
||||
import java.util.zip.DeflaterOutputStream
|
||||
import java.util.zip.ZipFile
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.CipherOutputStream
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
import kotlin.random.asKotlinRandom
|
||||
|
||||
private val kRANDOM get() = RANDOM.asKotlinRandom()
|
||||
|
||||
private val c1 = mutableListOf<String>()
|
||||
private val c2 = mutableListOf<String>()
|
||||
private val c3 = mutableListOf<String>()
|
||||
|
||||
fun initRandom(dict: File) {
|
||||
RANDOM = if (RAND_SEED != 0) Random(RAND_SEED.toLong()) else SecureRandom()
|
||||
c1.clear()
|
||||
c2.clear()
|
||||
c3.clear()
|
||||
for (a in chain('a'..'z', 'A'..'Z')) {
|
||||
if (a != 'a' && a != 'A') {
|
||||
c1.add("$a")
|
||||
}
|
||||
for (b in chain('a'..'z', 'A'..'Z', '0'..'9')) {
|
||||
c2.add("$a$b")
|
||||
for (c in chain('a'..'z', 'A'..'Z', '0'..'9')) {
|
||||
c3.add("$a$b$c")
|
||||
}
|
||||
}
|
||||
}
|
||||
c1.shuffle(RANDOM)
|
||||
c2.shuffle(RANDOM)
|
||||
c3.shuffle(RANDOM)
|
||||
PrintStream(dict).use {
|
||||
for (c in chain(c1, c2, c3)) {
|
||||
it.println(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> chain(vararg iters: Iterable<T>) = sequence {
|
||||
iters.forEach { it.forEach { v -> yield(v) } }
|
||||
}
|
||||
|
||||
private fun PrintStream.byteField(name: String, bytes: ByteArray) {
|
||||
println("public static byte[] $name() {")
|
||||
print("byte[] buf = {")
|
||||
print(bytes.joinToString(",") { it.toString() })
|
||||
println("};")
|
||||
println("return buf;")
|
||||
println("}")
|
||||
}
|
||||
|
||||
@CacheableTask
|
||||
private abstract class ManifestUpdater: DefaultTask() {
|
||||
@get:Input
|
||||
abstract val applicationId: Property<String>
|
||||
|
||||
@get:Input
|
||||
abstract val factoryClass: Property<String>
|
||||
|
||||
@get:Input
|
||||
abstract val appClass: Property<String>
|
||||
|
||||
@get:InputFile
|
||||
@get:PathSensitive(PathSensitivity.RELATIVE)
|
||||
abstract val mergedManifest: RegularFileProperty
|
||||
|
||||
@get:OutputFile
|
||||
abstract val outputManifest: RegularFileProperty
|
||||
|
||||
@TaskAction
|
||||
fun taskAction() {
|
||||
fun String.ind(level: Int) = replaceIndentByMargin(" ".repeat(level))
|
||||
|
||||
val cmpList = mutableListOf<String>()
|
||||
|
||||
cmpList.add("""
|
||||
|<provider
|
||||
| android:name="x.COMPONENT_PLACEHOLDER_0"
|
||||
| android:authorities="${'$'}{applicationId}.provider"
|
||||
| android:directBootAware="true"
|
||||
| android:exported="false"
|
||||
| android:grantUriPermissions="true" />""".ind(2)
|
||||
)
|
||||
|
||||
cmpList.add("""
|
||||
|<receiver
|
||||
| android:name="x.COMPONENT_PLACEHOLDER_1"
|
||||
| android:exported="false">
|
||||
| <intent-filter>
|
||||
| <action android:name="android.intent.action.LOCALE_CHANGED" />
|
||||
| <action android:name="android.intent.action.UID_REMOVED" />
|
||||
| <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
| </intent-filter>
|
||||
| <intent-filter>
|
||||
| <action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />
|
||||
|
|
||||
| <data android:scheme="package" />
|
||||
| </intent-filter>
|
||||
|</receiver>""".ind(2)
|
||||
)
|
||||
|
||||
cmpList.add("""
|
||||
|<activity
|
||||
| android:name="x.COMPONENT_PLACEHOLDER_2"
|
||||
| android:exported="true">
|
||||
| <intent-filter>
|
||||
| <action android:name="android.intent.action.MAIN" />
|
||||
| <category android:name="android.intent.category.LAUNCHER" />
|
||||
| </intent-filter>
|
||||
|</activity>""".ind(2)
|
||||
)
|
||||
|
||||
cmpList.add("""
|
||||
|<service
|
||||
| android:name="x.COMPONENT_PLACEHOLDER_3"
|
||||
| android:exported="false"
|
||||
| android:foregroundServiceType="dataSync" />""".ind(2)
|
||||
)
|
||||
|
||||
cmpList.add("""
|
||||
|<service
|
||||
| android:name="x.COMPONENT_PLACEHOLDER_4"
|
||||
| android:exported="false"
|
||||
| android:permission="android.permission.BIND_JOB_SERVICE" />""".ind(2)
|
||||
)
|
||||
|
||||
// Shuffle the order of the components
|
||||
cmpList.shuffle(RANDOM)
|
||||
val components = cmpList.joinToString("\n\n")
|
||||
.replace("\${applicationId}", applicationId.get())
|
||||
val manifest = mergedManifest.asFile.get().readText().replace(Regex(".*\\<application"), """
|
||||
|<application
|
||||
| android:appComponentFactory="${factoryClass.get()}"
|
||||
| android:name="${appClass.get()}"""".ind(1)
|
||||
).replace(Regex(".*\\<\\/application"), "$components\n </application")
|
||||
outputManifest.get().asFile.writeText(manifest)
|
||||
}
|
||||
}
|
||||
|
||||
private fun genStubClasses(outDir: File): Pair<String, String> {
|
||||
val classNameGenerator = sequence {
|
||||
fun notJavaKeyword(name: String) = when (name) {
|
||||
"do", "if", "for", "int", "new", "try" -> false
|
||||
else -> true
|
||||
}
|
||||
|
||||
fun List<String>.process() = asSequence()
|
||||
.filter(::notJavaKeyword)
|
||||
// Distinct by lower case to support case insensitive file systems
|
||||
.distinctBy { it.lowercase() }
|
||||
|
||||
val names = mutableListOf<String>()
|
||||
names.addAll(c1)
|
||||
names.addAll(c2.process().take(30))
|
||||
names.addAll(c3.process().take(30))
|
||||
names.shuffle(RANDOM)
|
||||
|
||||
while (true) {
|
||||
val cls = StringBuilder()
|
||||
cls.append(names.random(kRANDOM))
|
||||
cls.append('.')
|
||||
cls.append(names.random(kRANDOM))
|
||||
// Old Android does not support capitalized package names
|
||||
// Check Android 7.0.0 PackageParser#buildClassName
|
||||
yield(cls.toString().replaceFirstChar { it.lowercase() })
|
||||
}
|
||||
}.distinct().iterator()
|
||||
|
||||
fun genClass(type: String, outDir: File): String {
|
||||
val clzName = classNameGenerator.next()
|
||||
val (pkg, name) = clzName.split('.')
|
||||
val pkgDir = File(outDir, pkg)
|
||||
pkgDir.mkdirs()
|
||||
PrintStream(File(pkgDir, "$name.java")).use {
|
||||
it.println("package $pkg;")
|
||||
it.println("public class $name extends com.topjohnwu.magisk.$type {}")
|
||||
}
|
||||
return clzName
|
||||
}
|
||||
|
||||
val factory = genClass("DelegateComponentFactory", outDir)
|
||||
val app = genClass("StubApplication", outDir)
|
||||
return Pair(factory, app)
|
||||
}
|
||||
|
||||
private fun genEncryptedResources(res: ByteArray, outDir: File) {
|
||||
val mainPkgDir = File(outDir, "com/topjohnwu/magisk")
|
||||
mainPkgDir.mkdirs()
|
||||
|
||||
// Generate iv and key
|
||||
val iv = ByteArray(16)
|
||||
val key = ByteArray(32)
|
||||
RANDOM.nextBytes(iv)
|
||||
RANDOM.nextBytes(key)
|
||||
|
||||
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
|
||||
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv))
|
||||
val bos = ByteArrayOutputStream()
|
||||
|
||||
ByteArrayInputStream(res).use {
|
||||
CipherOutputStream(bos, cipher).use { os ->
|
||||
it.transferTo(os)
|
||||
}
|
||||
}
|
||||
|
||||
PrintStream(File(mainPkgDir, "Bytes.java")).use {
|
||||
it.println("package com.topjohnwu.magisk;")
|
||||
it.println("public final class Bytes {")
|
||||
|
||||
it.byteField("key", key)
|
||||
it.byteField("iv", iv)
|
||||
it.byteField("res", bos.toByteArray())
|
||||
|
||||
it.println("}")
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class TaskWithDir : DefaultTask() {
|
||||
@get:OutputDirectory
|
||||
abstract val outputFolder: DirectoryProperty
|
||||
}
|
||||
|
||||
fun Project.setupStubApk() {
|
||||
setupAppCommon()
|
||||
|
||||
androidAppComponents {
|
||||
onVariants { variant ->
|
||||
val variantName = variant.name
|
||||
val variantCapped = variantName.replaceFirstChar { it.uppercase() }
|
||||
val variantLowered = variantName.lowercase()
|
||||
|
||||
val componentJavaOutDir = layout.buildDirectory
|
||||
.dir("generated/${variantLowered}/components").get().asFile
|
||||
componentJavaOutDir.deleteRecursively()
|
||||
|
||||
val (factory, app) = genStubClasses(componentJavaOutDir)
|
||||
|
||||
val manifestUpdater =
|
||||
project.tasks.register("${variantName}ManifestProducer", ManifestUpdater::class.java) {
|
||||
applicationId = variant.applicationId
|
||||
factoryClass.set(factory)
|
||||
appClass.set(app)
|
||||
}
|
||||
variant.artifacts.use(manifestUpdater)
|
||||
.wiredWithFiles(
|
||||
ManifestUpdater::mergedManifest,
|
||||
ManifestUpdater::outputManifest)
|
||||
.toTransform(SingleArtifact.MERGED_MANIFEST)
|
||||
|
||||
val resTask = tasks.getByPath(":stub-res:package$variantCapped")
|
||||
val genResourcesTask = tasks.register("generate${variantCapped}BundledResources", TaskWithDir::class) {
|
||||
dependsOn(resTask)
|
||||
outputFolder.set(layout.buildDirectory.dir("generated/${variantLowered}/resources"))
|
||||
|
||||
doLast {
|
||||
val apk = resTask.outputs.files.asFileTree
|
||||
.filter { it.name.endsWith(".apk") }.files.first()
|
||||
|
||||
val bos = ByteArrayOutputStream()
|
||||
ZipFile(apk).use { src ->
|
||||
DeflaterOutputStream(bos, Deflater(Deflater.BEST_COMPRESSION)).use {
|
||||
src.getInputStream(src.getEntry("resources.arsc")).transferTo(it)
|
||||
}
|
||||
}
|
||||
genEncryptedResources(bos.toByteArray(), outputFolder.get().asFile)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(TransformApkTask::class) {
|
||||
transformations.add {
|
||||
// Always delete resources.arsc from the APK
|
||||
// to ensure that external resources can be loaded
|
||||
it.get("resources.arsc")?.delete()
|
||||
}
|
||||
}
|
||||
|
||||
variant.sources.java?.let {
|
||||
it.addStaticSourceDirectory(componentJavaOutDir.path)
|
||||
it.addGeneratedSourceDirectory(genResourcesTask, TaskWithDir::outputFolder)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user