From d2d5298ca6777e08fae72a59f2d244d56949a503 Mon Sep 17 00:00:00 2001 From: kissonchan Date: Fri, 7 Feb 2020 11:34:23 +0800 Subject: [PATCH] provider 'use7z' extension for developer to decide whether repack base apk by 7z format when you use qigsawUploadSplit${VariantName} task to upload split apk files. --- app/build.gradle | 6 ++ .../gradle/QigsawAppBasePlugin.groovy | 3 +- .../extension/QigsawSplitExtension.groovy | 6 ++ .../QigsawSplitExtensionHelper.groovy | 10 ++- .../gradle/internal/tool/FileUtils.groovy | 11 ++- .../gradle/task/QigsawAssembleTask.groovy | 2 +- .../task/QigsawUploadSplitApkTask.groovy | 79 ++++++++++--------- 7 files changed, 75 insertions(+), 42 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d142ac3c..29212464 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -91,6 +91,12 @@ qigsawSplit { */ releaseSplitApk = false + /** + * Whether repack base apk with 7z format when you use qigsawUploadSplit${VariantName} task to upload split apk files. + * default value is {@code false} + */ + use7z = true + } if (sample) { diff --git a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/QigsawAppBasePlugin.groovy b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/QigsawAppBasePlugin.groovy index 3f4102ce..d6c89ac5 100644 --- a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/QigsawAppBasePlugin.groovy +++ b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/QigsawAppBasePlugin.groovy @@ -192,8 +192,9 @@ class QigsawAppBasePlugin extends QigsawPlugin { qigsawInstallTask.installApkFile = apkFile qigsawInstallTask.setGroup(QIGSAW) + boolean isSigningNeed = variant.buildType.signingConfig != null && variant.buildType.signingConfig.isSigningReady() QigsawUploadSplitApkTask uploadSplitApkTask = project.tasks.create("qigsawUploadSplit${variantName}", QigsawUploadSplitApkTask) - uploadSplitApkTask.initArgs(oldApk == null ? null : new File(oldApk), splitUploadOutputDir, packageOutputDir, variantName) + uploadSplitApkTask.initArgs(oldApk == null ? null : new File(oldApk), splitUploadOutputDir, packageOutputDir, variantName, isSigningNeed) uploadSplitApkTask.setGroup(QIGSAW) //set task dependency if (hasQigsawTask) { diff --git a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtension.groovy b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtension.groovy index c8315ebe..64c6d326 100644 --- a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtension.groovy +++ b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtension.groovy @@ -50,6 +50,12 @@ class QigsawSplitExtension { */ boolean releaseSplitApk = false + /** + * Whether repack base apk with 7z format when you use qigsawUploadSplit${VariantName} task to upload split apk files. + * default value is {@code false} + */ + boolean use7z = false + /** * Restrict splits working process, if you do not assign split name, this split will work on * all processes, otherwise only work processes declared in its manifest. diff --git a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtensionHelper.groovy b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtensionHelper.groovy index b7817baa..32bf7f1a 100644 --- a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtensionHelper.groovy +++ b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/extension/QigsawSplitExtensionHelper.groovy @@ -48,7 +48,7 @@ class QigsawSplitExtensionHelper { return null } - static boolean getReleaseSplitApk(Project project) { + static boolean isReleaseSplitApk(Project project) { try { return project.extensions.qigsawSplit.releaseSplitApk } catch (Throwable ignored) { @@ -75,4 +75,12 @@ class QigsawSplitExtensionHelper { } return null } + + static boolean isUse7z(Project project) { + try { + return project.extensions.qigsawSplit.use7z + } catch (Throwable ignored) { + return false + } + } } diff --git a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/internal/tool/FileUtils.groovy b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/internal/tool/FileUtils.groovy index bd7f491a..b5e0e7ec 100644 --- a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/internal/tool/FileUtils.groovy +++ b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/internal/tool/FileUtils.groovy @@ -98,10 +98,17 @@ class FileUtils { } } - static void copyFile(File source, File dest) + static void copyFile(File source, File dest, boolean log) throws IOException { copyFile(new FileInputStream(source), new FileOutputStream(dest)) - QigsawLogger.w("Succeed to copy ${source.absolutePath} to ${dest.absolutePath}") + if (log) { + QigsawLogger.w("Succeed to copy ${source.absolutePath} to ${dest.absolutePath}") + } + } + + static void copyFile(File source, File dest) + throws IOException { + copyFile(source, dest, true) } static void closeQuietly(Closeable obj) { diff --git a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawAssembleTask.groovy b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawAssembleTask.groovy index 6ff89184..6d458b09 100644 --- a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawAssembleTask.groovy +++ b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawAssembleTask.groovy @@ -110,7 +110,7 @@ class QigsawAssembleTask extends DefaultTask { File splitDependenciesOutputDir QigsawAssembleTask() { - this.releaseSplitApk = QigsawSplitExtensionHelper.getReleaseSplitApk(project) + this.releaseSplitApk = QigsawSplitExtensionHelper.isReleaseSplitApk(project) this.restrictWorkProcessesForSplits = QigsawSplitExtensionHelper.getRestrictWorkProcessesForSplits(project) } diff --git a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawUploadSplitApkTask.groovy b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawUploadSplitApkTask.groovy index 77df3d2d..e2dd628f 100644 --- a/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawUploadSplitApkTask.groovy +++ b/buildSrc/src/main/groovy/com/iqiyi/qigsaw/buildtool/gradle/task/QigsawUploadSplitApkTask.groovy @@ -25,6 +25,7 @@ package com.iqiyi.qigsaw.buildtool.gradle.task import com.android.SdkConstants +import com.iqiyi.qigsaw.buildtool.gradle.extension.QigsawSplitExtensionHelper import com.iqiyi.qigsaw.buildtool.gradle.internal.entity.SplitDetails import com.iqiyi.qigsaw.buildtool.gradle.internal.entity.SplitInfo import com.iqiyi.qigsaw.buildtool.gradle.internal.tool.CommandUtils @@ -47,13 +48,13 @@ import java.util.zip.ZipEntry class QigsawUploadSplitApkTask extends DefaultTask { - static final String OLD_APK_EXTRACTION = "old_apk_extraction" - - static final String NEW_SPLIT_INFO = "new_split_info" + static final String NEW_APKS = "new_apks" static final String STORED_FILES = "store_files" - static final String NEW_APKS = "new_apks" + static final String NEW_SPLIT_INFO = "new_split_info" + + static final String OLD_APK_EXTRACTION = "old_apk_extraction" @InputFile @Optional @@ -68,11 +69,19 @@ class QigsawUploadSplitApkTask extends DefaultTask { @OutputDirectory File packageOutputDir - void initArgs(File oldApk, File outputDir, File packageOutputDir, String variantName) { + @Input + boolean use7z + + @Input + boolean isSigningNeed + + void initArgs(File oldApk, File outputDir, File packageOutputDir, String variantName, boolean isSigningNeed) { this.oldApk = oldApk this.variantName = variantName this.outputDir = outputDir this.packageOutputDir = packageOutputDir + this.use7z = QigsawSplitExtensionHelper.isUse7z(project) + this.isSigningNeed = isSigningNeed } @TaskAction @@ -145,21 +154,13 @@ class QigsawUploadSplitApkTask extends DefaultTask { FileUtils.copyFile(newSplitJsonFile, oldSplitJsonFile) File newApksDir = new File(outputDir, NEW_APKS) newApksDir.mkdirs() - Collection resFiles = new ArrayList<>(0) - Collections.addAll(resFiles, new File(oldApkExtractionPath).listFiles()) - File unsignedApk = new File(newApksDir, oldApk.name.replace(".apk", "_unsigned.apk")) - ZipUtils.zipFiles(resFiles, new File(oldApkExtractionPath), unsignedApk, compressData) - SplitApkSigner apkSigner = new SplitApkSigner(project, variantName) - File signedApk = new File(newApksDir, oldApk.name.replace(".apk", "_signed.apk")) - try { - apkSigner.signAPKIfNeed(unsignedApk, signedApk) - } catch (Throwable ignored) { - QigsawLogger.e("Can't find signingConfigs in app build.gradle") - } - //generate 7zip format apk files - File unsigned7zaApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_unsigned.apk")) - boolean createOk = run7zCmd("7za", "a", "-tzip", unsigned7zaApk.absolutePath, oldApkExtractionPath + File.separator + "*", "-mx9") - if (createOk) { + File signedApk + File unsignedApk + if (use7z) { + //generate 7zip format apk files + unsignedApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_unsigned_${variantName.uncapitalize()}.apk")) + signedApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_signed_${variantName.uncapitalize()}.apk")) + run7zCmd("7za", "a", "-tzip", unsignedApk.absolutePath, oldApkExtractionPath + File.separator + "*", "-mx9") List storedFiles = new ArrayList<>() for (String name : compressData.keySet()) { File file = new File(oldApkExtractionPath, name) @@ -172,6 +173,7 @@ class QigsawUploadSplitApkTask extends DefaultTask { } } if (storedFiles.size() > 0) { + QigsawLogger.w("Rewrite the stored files into the 7zip, file count ${storedFiles.size()}") File storeFilesDir = new File(outputDir, STORED_FILES) storeFilesDir.mkdirs() for (String name : storedFiles) { @@ -180,41 +182,44 @@ class QigsawUploadSplitApkTask extends DefaultTask { if (parent != null && (!parent.exists())) { parent.mkdirs() } - FileUtils.copyFile(new File(oldApkExtractionPath, name), storeFile) - } - boolean updateOk = run7zCmd("7za", "a", "-tzip", unsigned7zaApk.absolutePath, storeFilesDir.absolutePath + File.separator + "*", "-mx0") - if (updateOk) { - File signed7zaApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_signed.apk")) - try { - apkSigner.signAPKIfNeed(unsigned7zaApk, signed7zaApk) - } catch (Throwable ignored) { - QigsawLogger.e("Can't find signingConfigs in app build.gradle") - } + FileUtils.copyFile(new File(oldApkExtractionPath, name), storeFile, false) } + run7zCmd("7za", "a", "-tzip", unsignedApk.absolutePath, storeFilesDir.absolutePath + File.separator + "*", "-mx0") + } + } else { + signedApk = new File(newApksDir, oldApk.name.replace(".apk", "_signed_${variantName.uncapitalize()}.apk")) + unsignedApk = new File(newApksDir, oldApk.name.replace(".apk", "_unsigned_${variantName.uncapitalize()}.apk")) + Collection resFiles = new ArrayList<>(0) + Collections.addAll(resFiles, new File(oldApkExtractionPath).listFiles()) + ZipUtils.zipFiles(resFiles, new File(oldApkExtractionPath), unsignedApk, compressData) + } + if (isSigningNeed) { + QigsawLogger.w("'SignConfig' has been configured, start to sign apk ${unsignedApk.absolutePath}") + if (unsignedApk.exists()) { + SplitApkSigner apkSigner = new SplitApkSigner(project, variantName) + apkSigner.signAPKIfNeed(unsignedApk, signedApk) + unsignedApk.delete() } } //copy target products to package output dir. if (packageOutputDir.exists()) { packageOutputDir.deleteDir() } - File qigsawApksOutputDir = new File(packageOutputDir, "qigsaw_apks") - qigsawApksOutputDir.mkdirs() + packageOutputDir.mkdirs() FileUtils.copyFile(newSplitJsonFile, new File(packageOutputDir, newSplitJsonFile.name)) if (newApksDir.listFiles() != null) { newApksDir.listFiles().each { - FileUtils.copyFile(it, new File(qigsawApksOutputDir, it.name)) + FileUtils.copyFile(it, new File(packageOutputDir, it.name)) } } } - static boolean run7zCmd(String... cmd) { + static void run7zCmd(String... cmd) { try { String cmdResult = CommandUtils.runCmd(cmd) QigsawLogger.w("Run command successfully, result: " + cmdResult) - return true } catch (Throwable e) { - QigsawLogger.e("'7za' command is not found, have you install 7zip?", e) + throw new GradleException("'7za' command is not found, have you install 7zip?", e) } - return false } }