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 <assam.boudjelthia@qt.io>
This commit is contained in:
Mike Achtelik 2021-04-14 14:26:42 +02:00
parent 7c233d4034
commit 08d8c59629

View File

@ -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), .arg(shellQuote(zipAlignTool),
options.verbose ? QLatin1String(" -v") : QLatin1String(), options.verbose ? QLatin1String(" -v") : QLatin1String(),
packagePath(options, UnsignedAPK), packagePath(options, UnsignedAPK));
packagePath(options, SignedAPK));
FILE *zipAlignCommand = openProcess(zipAlignTool); if (zipalignRunner(verifyZipAlignCommandLine)) {
if (zipAlignCommand == 0) { if (options.verbose)
fprintf(stderr, "Couldn't run zipalign.\n"); fprintf(stdout, "APK already aligned, copying it for signing.\n");
return false;
}
char buffer[512]; if (QFile::exists(packagePath(options, SignedAPK)))
while (fgets(buffer, sizeof(buffer), zipAlignCommand) != 0) QFile::remove(packagePath(options, SignedAPK));
fprintf(stdout, "%s", buffer);
int errorCode = pclose(zipAlignCommand); if (!QFile::copy(packagePath(options, UnsignedAPK), packagePath(options, SignedAPK))) {
if (errorCode != 0) { fprintf(stderr, "Could not copy unsigned APK.\n");
fprintf(stderr, "zipalign command failed.\n"); return false;
if (!options.verbose) }
fprintf(stderr, " -- Run with --verbose for more information.\n"); } else {
return false; 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") QString apkSignerCommandLine = QLatin1String("%1 sign --ks %2")
@ -2841,7 +2865,7 @@ bool signPackage(const Options &options)
while (fgets(buffer, sizeof(buffer), apkSignerCommand) != 0) while (fgets(buffer, sizeof(buffer), apkSignerCommand) != 0)
fprintf(stdout, "%s", buffer); fprintf(stdout, "%s", buffer);
errorCode = pclose(apkSignerCommand); int errorCode = pclose(apkSignerCommand);
if (errorCode != 0) { if (errorCode != 0) {
fprintf(stderr, "apksigner command failed.\n"); fprintf(stderr, "apksigner command failed.\n");
if (!options.verbose) if (!options.verbose)