315 lines
12 KiB
Kotlin
Raw Normal View History

2021-09-08 00:45:15 +08:00
import com.android.build.gradle.BaseExtension
2021-09-09 20:19:49 -07:00
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
2022-06-18 02:08:44 +08:00
import com.android.builder.internal.packaging.IncrementalPackager
import com.android.builder.model.SigningConfig
import com.android.tools.build.apkzlib.sign.SigningExtension
import com.android.tools.build.apkzlib.sign.SigningOptions
import com.android.tools.build.apkzlib.zfile.ZFiles
import com.android.tools.build.apkzlib.zip.ZFileOptions
2021-09-09 20:19:49 -07:00
import org.apache.tools.ant.filters.FixCrLfFilter
2021-09-08 00:45:15 +08:00
import org.gradle.api.Action
import org.gradle.api.JavaVersion
import org.gradle.api.Project
2022-06-03 01:13:29 -07:00
import org.gradle.api.plugins.ExtensionAware
2022-08-26 03:43:31 -07:00
import org.gradle.api.tasks.Copy
2022-01-09 02:22:34 +08:00
import org.gradle.api.tasks.Delete
2021-09-09 20:19:49 -07:00
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.Sync
2022-10-31 22:31:15 +08:00
import org.gradle.kotlin.dsl.*
2022-05-27 00:44:20 -07:00
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
2022-06-18 02:08:44 +08:00
import java.security.KeyStore
import java.security.cert.X509Certificate
2021-09-09 20:19:49 -07:00
import java.util.*
2022-06-20 02:51:58 +08:00
import java.util.jar.JarFile
import java.util.zip.*
2021-12-14 21:30:15 +08:00
private fun Project.androidBase(configure: Action<BaseExtension>) =
extensions.configure("android", configure)
2021-09-09 20:19:49 -07:00
2021-12-14 21:30:15 +08:00
private fun Project.android(configure: Action<BaseAppModuleExtension>) =
2021-09-09 20:19:49 -07:00
extensions.configure("android", configure)
2021-09-08 00:45:15 +08:00
2022-06-03 01:13:29 -07:00
private fun BaseExtension.kotlinOptions(configure: Action<KotlinJvmOptions>) =
(this as ExtensionAware).extensions.findByName("kotlinOptions")?.let {
2022-05-27 00:44:20 -07:00
configure.execute(it as KotlinJvmOptions)
}
2021-12-14 21:30:15 +08:00
private val Project.android: BaseAppModuleExtension
2022-10-31 22:31:15 +08:00
get() = extensions["android"] as BaseAppModuleExtension
2021-09-08 00:45:15 +08:00
fun Project.setupCommon() {
2021-12-14 21:30:15 +08:00
androidBase {
compileSdkVersion(33)
2022-12-26 02:02:21 -08:00
buildToolsVersion = "33.0.1"
2022-01-27 01:46:00 -08:00
ndkPath = "$sdkDirectory/ndk/magisk"
2021-09-08 00:45:15 +08:00
defaultConfig {
minSdk = 21
targetSdk = 33
2021-09-08 00:45:15 +08:00
}
compileOptions {
2023-02-20 14:55:27 +08:00
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
2021-09-08 00:45:15 +08:00
}
2022-06-03 01:13:29 -07:00
kotlinOptions {
2023-02-20 14:55:27 +08:00
jvmTarget = "17"
2022-06-03 01:13:29 -07:00
}
2022-05-27 00:44:20 -07:00
}
2021-09-08 00:45:15 +08:00
}
2022-06-18 02:08:44 +08:00
private fun SigningConfig.getPrivateKey(): KeyStore.PrivateKeyEntry {
val keyStore = KeyStore.getInstance(storeType ?: KeyStore.getDefaultType())
storeFile!!.inputStream().use {
keyStore.load(it, storePassword!!.toCharArray())
}
val keyPwdArray = keyPassword!!.toCharArray()
val entry = keyStore.getEntry(keyAlias!!, KeyStore.PasswordProtection(keyPwdArray))
return entry as KeyStore.PrivateKeyEntry
}
private fun addComment(apkPath: File, signConfig: SigningConfig, minSdk: Int, eocdComment: String) {
val privateKey = signConfig.getPrivateKey()
val signingOptions = SigningOptions.builder()
.setMinSdkVersion(minSdk)
.setV1SigningEnabled(true)
.setV2SigningEnabled(true)
.setKey(privateKey.privateKey)
.setCertificates(privateKey.certificate as X509Certificate)
.setValidation(SigningOptions.Validation.ASSUME_INVALID)
.build()
val options = ZFileOptions().apply {
noTimestamps = true
autoSortFiles = true
}
ZFiles.apk(apkPath, options).use {
SigningExtension(signingOptions).register(it)
it.eocdComment = eocdComment.toByteArray()
it.get(IncrementalPackager.APP_METADATA_ENTRY_PATH)?.delete()
2022-06-20 02:51:58 +08:00
it.get(JarFile.MANIFEST_NAME)?.delete()
2022-06-18 02:08:44 +08:00
}
}
2021-09-09 20:19:49 -07:00
private fun Project.setupAppCommon() {
2021-09-08 00:45:15 +08:00
setupCommon()
2021-09-09 20:19:49 -07:00
2021-09-08 00:45:15 +08:00
android {
signingConfigs {
create("config") {
Config["keyStore"]?.also {
storeFile = rootProject.file(it)
storePassword = Config["keyStorePass"]
keyAlias = Config["keyAlias"]
keyPassword = Config["keyPass"]
}
}
}
buildTypes {
2022-10-31 22:31:15 +08:00
signingConfigs["config"].also {
debug {
2021-09-08 00:45:15 +08:00
signingConfig = if (it.storeFile?.exists() == true) it
2022-10-31 22:31:15 +08:00
else signingConfigs["debug"]
2021-09-08 00:45:15 +08:00
}
2022-10-31 22:31:15 +08:00
release {
2021-09-08 00:45:15 +08:00
signingConfig = if (it.storeFile?.exists() == true) it
2022-10-31 22:31:15 +08:00
else signingConfigs["debug"]
2021-09-08 00:45:15 +08:00
}
}
}
2021-12-14 21:30:15 +08:00
lint {
2021-09-08 00:45:15 +08:00
disable += "MissingTranslation"
}
2021-12-14 21:30:15 +08:00
dependenciesInfo {
includeInApk = false
}
2021-09-08 00:45:15 +08:00
}
2022-06-18 02:08:44 +08:00
android.applicationVariants.all {
2023-02-20 14:55:27 +08:00
val projectName = project.name.lowercase()
val variantCapped = name.replaceFirstChar { it.uppercase() }
2022-06-18 02:08:44 +08:00
tasks.getByPath(":$projectName:package$variantCapped").doLast {
2022-06-20 02:51:58 +08:00
val apk = outputs.files.asFileTree.filter { it.name.endsWith(".apk") }.singleFile
2022-06-19 00:43:27 -07:00
val comment = "version=${Config.version}\nversionCode=${Config.versionCode}"
2022-06-18 02:08:44 +08:00
addComment(apk, signingConfig, android.defaultConfig.minSdk!!, comment)
}
}
2021-09-08 00:45:15 +08:00
}
2021-09-09 20:19:49 -07:00
fun Project.setupApp() {
setupAppCommon()
2022-10-31 22:31:15 +08:00
val syncLibs by tasks.registering(Sync::class) {
2021-09-09 20:19:49 -07:00
into("src/main/jniLibs")
into("armeabi-v7a") {
from(rootProject.file("native/out/armeabi-v7a")) {
2022-03-17 03:15:39 -07:00
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
2021-09-09 20:19:49 -07:00
rename { if (it == "magisk") "libmagisk32.so" else "lib$it.so" }
}
}
into("x86") {
from(rootProject.file("native/out/x86")) {
2022-03-17 03:15:39 -07:00
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
2021-09-09 20:19:49 -07:00
rename { if (it == "magisk") "libmagisk32.so" else "lib$it.so" }
}
}
into("arm64-v8a") {
from(rootProject.file("native/out/arm64-v8a")) {
2022-03-17 03:15:39 -07:00
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
2021-09-09 20:19:49 -07:00
rename { if (it == "magisk") "libmagisk64.so" else "lib$it.so" }
}
}
into("x86_64") {
from(rootProject.file("native/out/x86_64")) {
2022-03-17 03:15:39 -07:00
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
2021-09-09 20:19:49 -07:00
rename { if (it == "magisk") "libmagisk64.so" else "lib$it.so" }
}
}
onlyIf {
2022-03-17 03:15:39 -07:00
if (inputs.sourceFiles.files.size != 20)
2021-09-09 20:19:49 -07:00
throw StopExecutionException("Please build binaries first! (./build.py binary)")
true
}
}
2022-10-31 22:31:15 +08:00
val syncResources by tasks.registering(Sync::class) {
2021-09-09 20:19:49 -07:00
dependsOn(syncLibs)
into("src/main/resources/META-INF/com/google/android")
from(rootProject.file("scripts/update_binary.sh")) {
rename { "update-binary" }
}
from(rootProject.file("scripts/flash_script.sh")) {
rename { "updater-script" }
}
}
android.applicationVariants.all {
2023-02-20 14:55:27 +08:00
val variantCapped = name.replaceFirstChar { it.uppercase() }
2022-12-26 15:23:06 -08:00
val stubTask = tasks.getByPath(":stub:package$variantCapped")
val stubApk = stubTask.outputs.files.asFileTree.filter {
it.name.endsWith(".apk")
}
2022-08-26 03:43:31 -07:00
2022-10-31 22:31:15 +08:00
val syncAssets = tasks.register("sync${variantCapped}Assets", Sync::class) {
2022-12-26 15:23:06 -08:00
dependsOn(syncResources, stubTask)
2022-10-31 22:31:15 +08:00
inputs.property("version", Config.version)
inputs.property("versionCode", Config.versionCode)
2022-08-26 03:43:31 -07:00
into("src/main/assets")
2022-10-31 22:31:15 +08:00
from(rootProject.file("scripts")) {
include("util_functions.sh", "boot_patch.sh", "addon.d.sh")
include("uninstaller.sh", "module_installer.sh")
}
from(rootProject.file("tools/bootctl"))
into("chromeos") {
from(rootProject.file("tools/futility"))
from(rootProject.file("tools/keys")) {
include("kernel_data_key.vbprivk", "kernel.keyblock")
}
}
2022-12-26 15:23:06 -08:00
from(stubApk) {
2022-08-26 03:43:31 -07:00
rename { "stub.apk" }
}
2022-10-31 22:31:15 +08:00
filesMatching("**/util_functions.sh") {
filter {
it.replace(
"#MAGISK_VERSION_STUB",
"MAGISK_VER='${Config.version}'\nMAGISK_VER_CODE=${Config.versionCode}"
)
}
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
2022-08-26 03:43:31 -07:00
}
}
2022-10-31 22:31:15 +08:00
preBuildProvider.get().dependsOn(syncAssets)
2021-12-14 21:30:15 +08:00
2021-09-09 20:19:49 -07:00
val keysDir = rootProject.file("tools/keys")
val outSrcDir = File(buildDir, "generated/source/keydata/$name")
val outSrc = File(outSrcDir, "com/topjohnwu/magisk/signing/KeyData.java")
2022-08-26 03:43:31 -07:00
val genSrcTask = tasks.register("generate${variantCapped}KeyData") {
2021-09-09 20:19:49 -07:00
inputs.dir(keysDir)
outputs.file(outSrc)
doLast {
genKeyData(keysDir, outSrc)
}
}
registerJavaGeneratingTask(genSrcTask, outSrcDir)
}
}
fun Project.setupStub() {
setupAppCommon()
android.applicationVariants.all {
2023-02-20 14:55:27 +08:00
val variantCapped = name.replaceFirstChar { it.uppercase() }
val variantLowered = name.lowercase()
2021-12-14 21:30:15 +08:00
val manifest = file("src/${variantLowered}/AndroidManifest.xml")
val outSrcDir = File(buildDir, "generated/source/obfuscate/${variantLowered}")
2021-09-09 20:19:49 -07:00
val templateDir = file("template")
2021-12-14 21:30:15 +08:00
val aapt = File(android.sdkDirectory, "build-tools/${android.buildToolsVersion}/aapt2")
val apk = File(buildDir, "intermediates/processed_res/" +
"${variantLowered}/out/resources-${variantLowered}.ap_")
val apkTmp = File("${apk}.tmp")
2021-09-09 20:19:49 -07:00
2021-12-14 21:30:15 +08:00
val genManifestTask = tasks.register("generate${variantCapped}ObfuscatedManifest") {
2021-09-09 20:19:49 -07:00
doLast {
val xml = genStubManifest(templateDir, outSrcDir)
2021-12-14 21:30:15 +08:00
manifest.parentFile.mkdirs()
2021-09-09 20:19:49 -07:00
PrintStream(manifest).use {
it.print(xml)
}
2021-12-14 21:30:15 +08:00
}
}
2021-12-29 17:09:59 +08:00
tasks.getByPath(":stub:process${variantCapped}MainManifest").dependsOn(genManifestTask)
2021-09-09 20:19:49 -07:00
2021-12-14 21:30:15 +08:00
val genSrcTask = tasks.register("generate${variantCapped}ObfuscatedSources") {
dependsOn(":stub:process${variantCapped}Resources")
2021-12-29 17:09:59 +08:00
inputs.file(apk)
outputs.file(apk)
2021-12-14 21:30:15 +08:00
doLast {
exec {
commandLine(aapt, "optimize", "-o", apkTmp, "--collapse-resource-names", apk)
}
2021-09-09 20:19:49 -07:00
val bos = ByteArrayOutputStream()
ZipFile(apkTmp).use { src ->
ZipOutputStream(apk.outputStream()).use {
it.setLevel(Deflater.BEST_COMPRESSION)
it.putNextEntry(ZipEntry("AndroidManifest.xml"))
src.getInputStream(src.getEntry("AndroidManifest.xml")).transferTo(it)
it.closeEntry()
2021-09-09 20:19:49 -07:00
}
DeflaterOutputStream(bos, Deflater(Deflater.BEST_COMPRESSION)).use {
src.getInputStream(src.getEntry("resources.arsc")).transferTo(it)
2021-09-09 20:19:49 -07:00
}
}
2021-12-14 21:30:15 +08:00
apkTmp.delete()
genEncryptedResources(ByteArrayInputStream(bos.toByteArray()), outSrcDir)
2021-09-09 20:19:49 -07:00
}
}
registerJavaGeneratingTask(genSrcTask, outSrcDir)
}
2021-12-14 21:30:15 +08:00
// Override optimizeReleaseResources task
tasks.whenTaskAdded {
if (name == "optimizeReleaseResources") {
2023-02-20 14:55:27 +08:00
dependsOn("generateReleaseObfuscatedSources")
val apk = File(buildDir, "intermediates/processed_res/" +
"release/out/resources-release.ap_")
val optRes = File(buildDir, "intermediates/optimized_processed_res/" +
"release/resources-release-optimize.ap_")
2021-12-14 21:30:15 +08:00
doLast { apk.copyTo(optRes, true) }
}
}
2022-01-09 02:22:34 +08:00
tasks.named<Delete>("clean") {
delete.addAll(listOf("src/debug/AndroidManifest.xml", "src/release/AndroidManifest.xml"))
}
2021-09-09 20:19:49 -07:00
}