From a250e2b56cb39efd16c5ee15be919e9b518094b3 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sat, 18 Jun 2022 02:08:44 +0800 Subject: [PATCH] Set version comment in apk --- buildSrc/src/main/java/Codegen.kt | 2 -- buildSrc/src/main/java/Setup.kt | 53 +++++++++++++++++++++++++++++- tools/keys/testkey.pk8 | Bin 1217 -> 0 bytes tools/keys/testkey.x509.pem | Bin 1675 -> 0 bytes 4 files changed, 52 insertions(+), 3 deletions(-) delete mode 100644 tools/keys/testkey.pk8 delete mode 100644 tools/keys/testkey.x509.pem diff --git a/buildSrc/src/main/java/Codegen.kt b/buildSrc/src/main/java/Codegen.kt index 7e9eae7bc..b8ac6171a 100644 --- a/buildSrc/src/main/java/Codegen.kt +++ b/buildSrc/src/main/java/Codegen.kt @@ -64,8 +64,6 @@ fun genKeyData(keysDir: File, outSrc: File) { it.println("package com.topjohnwu.magisk.signing;") it.println("public final class KeyData {") - it.byteField("testCert", File(keysDir, "testkey.x509.pem").readBytes()) - it.byteField("testKey", File(keysDir, "testkey.pk8").readBytes()) it.byteField("verityCert", File(keysDir, "verity.x509.pem").readBytes()) it.byteField("verityKey", File(keysDir, "verity.pk8").readBytes()) diff --git a/buildSrc/src/main/java/Setup.kt b/buildSrc/src/main/java/Setup.kt index a4ade9b3e..f1b11f23c 100644 --- a/buildSrc/src/main/java/Setup.kt +++ b/buildSrc/src/main/java/Setup.kt @@ -1,6 +1,11 @@ - import com.android.build.gradle.BaseExtension import com.android.build.gradle.internal.dsl.BaseAppModuleExtension +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 import org.apache.tools.ant.filters.FixCrLfFilter import org.gradle.api.Action import org.gradle.api.JavaVersion @@ -16,6 +21,8 @@ import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File import java.io.PrintStream +import java.security.KeyStore +import java.security.cert.X509Certificate import java.util.* import java.util.zip.* @@ -55,6 +62,37 @@ fun Project.setupCommon() { } } +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() + } +} + private fun Project.setupAppCommon() { setupCommon() @@ -91,6 +129,19 @@ private fun Project.setupAppCommon() { includeInApk = false } } + + android.applicationVariants.all { + val projectName = project.name.toLowerCase(Locale.ROOT) + val variantCapped = name.capitalize(Locale.ROOT) + val variant = name.toLowerCase(Locale.ROOT) + tasks.getByPath(":$projectName:package$variantCapped").doLast { + val apkDir = if (properties["android.injected.invoked.from.ide"] == "true") + "intermediates" else "outputs" + val apk = File(buildDir, "${apkDir}/apk/${variant}/$projectName-${variant}.apk") + val comment = "${Config.versionCode}" + addComment(apk, signingConfig, android.defaultConfig.minSdk!!, comment) + } + } } fun Project.setupApp() { diff --git a/tools/keys/testkey.pk8 b/tools/keys/testkey.pk8 deleted file mode 100644 index 586c1bd5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