From 08d8c5962930b8bf64d696e07ef65fb0c4871474 Mon Sep 17 00:00:00 2001 From: Mike Achtelik Date: Wed, 14 Apr 2021 14:26:42 +0200 Subject: [PATCH] androiddeployqt: Check if apk is already aligned Newer versions of the android gradle plugin already align the apk internally. Therefore it is not necessary to indiscriminately align every apk. So let's first check, if it is already aligned and only align it if necessary. This prevents possible alignment errors, which might occur when aligning it again. If it is already aligned, we can just copy and continue signing the apk. Fixes: QTBUG-88989 Change-Id: If29004e372e7927c88a900dc56f490bf9bce9ec7 Reviewed-by: Assam Boudjelthia --- src/tools/androiddeployqt/main.cpp | 60 +++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/src/tools/androiddeployqt/main.cpp b/src/tools/androiddeployqt/main.cpp index 2d660ff8445..4a476cd0656 100644 --- a/src/tools/androiddeployqt/main.cpp +++ b/src/tools/androiddeployqt/main.cpp @@ -2788,28 +2788,52 @@ bool signPackage(const Options &options) } } - zipAlignTool = QLatin1String("%1%2 -f 4 %3 %4") + auto zipalignRunner = [](const QString &zipAlignCommandLine) { + FILE *zipAlignCommand = openProcess(zipAlignCommandLine); + if (zipAlignCommand == 0) { + fprintf(stderr, "Couldn't run zipalign.\n"); + return false; + } + + char buffer[512]; + while (fgets(buffer, sizeof(buffer), zipAlignCommand) != 0) + fprintf(stdout, "%s", buffer); + + return pclose(zipAlignCommand) == 0; + }; + + const QString verifyZipAlignCommandLine = QLatin1String("%1%2 -c 4 %3") .arg(shellQuote(zipAlignTool), options.verbose ? QLatin1String(" -v") : QLatin1String(), - packagePath(options, UnsignedAPK), - packagePath(options, SignedAPK)); + packagePath(options, UnsignedAPK)); - FILE *zipAlignCommand = openProcess(zipAlignTool); - if (zipAlignCommand == 0) { - fprintf(stderr, "Couldn't run zipalign.\n"); - return false; - } + if (zipalignRunner(verifyZipAlignCommandLine)) { + if (options.verbose) + fprintf(stdout, "APK already aligned, copying it for signing.\n"); - char buffer[512]; - while (fgets(buffer, sizeof(buffer), zipAlignCommand) != 0) - fprintf(stdout, "%s", buffer); + if (QFile::exists(packagePath(options, SignedAPK))) + QFile::remove(packagePath(options, SignedAPK)); - int errorCode = pclose(zipAlignCommand); - if (errorCode != 0) { - fprintf(stderr, "zipalign command failed.\n"); - if (!options.verbose) - fprintf(stderr, " -- Run with --verbose for more information.\n"); - return false; + if (!QFile::copy(packagePath(options, UnsignedAPK), packagePath(options, SignedAPK))) { + fprintf(stderr, "Could not copy unsigned APK.\n"); + return false; + } + } else { + if (options.verbose) + fprintf(stdout, "APK not aligned, aligning it for signing.\n"); + + const QString zipAlignCommandLine = QLatin1String("%1%2 -f 4 %3 %4") + .arg(shellQuote(zipAlignTool), + options.verbose ? QLatin1String(" -v") : QLatin1String(), + packagePath(options, UnsignedAPK), + packagePath(options, SignedAPK)); + + if (!zipalignRunner(zipAlignCommandLine)) { + fprintf(stderr, "zipalign command failed.\n"); + if (!options.verbose) + fprintf(stderr, " -- Run with --verbose for more information.\n"); + return false; + } } QString apkSignerCommandLine = QLatin1String("%1 sign --ks %2") @@ -2841,7 +2865,7 @@ bool signPackage(const Options &options) while (fgets(buffer, sizeof(buffer), apkSignerCommand) != 0) fprintf(stdout, "%s", buffer); - errorCode = pclose(apkSignerCommand); + int errorCode = pclose(apkSignerCommand); if (errorCode != 0) { fprintf(stderr, "apksigner command failed.\n"); if (!options.verbose)