Use AGP to compile resources

This commit is contained in:
南宫雪珊
2021-12-14 21:30:15 +08:00
committed by GitHub
parent baa19f0ccf
commit df191cd2b5
49 changed files with 89 additions and 130 deletions
+6 -18
View File
@@ -1,11 +1,9 @@
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.PrintStream
import java.security.SecureRandom
import java.util.*
import java.util.zip.GZIPOutputStream
import javax.crypto.Cipher
import javax.crypto.CipherOutputStream
import javax.crypto.spec.IvParameterSpec
@@ -54,7 +52,7 @@ private fun <T> chain(vararg iters: Iterable<T>) = sequence {
private fun PrintStream.byteField(name: String, bytes: ByteArray) {
println("public static byte[] $name() {")
print("byte[] buf = {")
print(bytes.joinToString(",") { "(byte)(${it.toInt() and 0xff})" })
print(bytes.joinToString(",") { it.toString() })
println("};")
println("return buf;")
println("}")
@@ -192,10 +190,8 @@ fun genStubManifest(srcDir: File, outDir: File): String {
names.addAll(c3.subList(0, 10))
names.shuffle(RANDOM)
// Decapitalize as older Android versions do not allow capitalized package names
// Distinct by lower case to support case insensitive file systems
val pkgNames = names.map { it.decapitalize(Locale.ROOT) }
.distinctBy { it.toLowerCase(Locale.ROOT) }
val pkgNames = names.distinctBy { it.toLowerCase(Locale.ROOT) }
var idx = 0
fun genCmpName() = "${pkgNames[idx++]}.${names.random(kRANDOM)}"
@@ -247,15 +243,8 @@ fun genStubManifest(srcDir: File, outDir: File): String {
return genXml
}
fun genEncryptedResources(res: File, outDir: File) {
fun genEncryptedResources(res: InputStream, outDir: File) {
val mainPkgDir = File(outDir, "com/topjohnwu/magisk")
// Rename R.java
val r = File(mainPkgDir, "R.java").let {
val txt = it.readText()
it.delete()
txt
}
File(mainPkgDir, "R2.java").writeText(r.replace("class R", "class R2"))
// Generate iv and key
val iv = ByteArray(16)
@@ -267,9 +256,8 @@ fun genEncryptedResources(res: File, outDir: File) {
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv))
val bos = ByteArrayOutputStream()
FileInputStream(res).use {
// First compress, then encrypt
GZIPOutputStream(CipherOutputStream(bos, cipher)).use { os ->
res.use {
CipherOutputStream(bos, cipher).use { os ->
it.transferTo(os)
}
}