diff --git a/src/android/templates/build.gradle b/src/android/templates/build.gradle index 33867903c2..fbd0a48fe6 100644 --- a/src/android/templates/build.gradle +++ b/src/android/templates/build.gradle @@ -35,7 +35,7 @@ android { * Changing them manually might break the compilation! *******************************************************/ - compileSdkVersion androidCompileSdkVersion.toInteger() + compileSdkVersion androidCompileSdkVersion buildToolsVersion androidBuildToolsVersion ndkVersion androidNdkVersion diff --git a/src/tools/androiddeployqt/main.cpp b/src/tools/androiddeployqt/main.cpp index b47d16477d..4bb43f4236 100644 --- a/src/tools/androiddeployqt/main.cpp +++ b/src/tools/androiddeployqt/main.cpp @@ -2693,22 +2693,36 @@ void checkAndWarnGradleLongPaths(const QString &outputDirectory) } #endif -bool gradleSetsLegacyPackagingProperty(const QString &path) +struct GradleFlags { + bool setsLegacyPackaging = false; + bool usesIntegerCompileSdkVersion = false; +}; + +GradleFlags gradleBuildFlags(const QString &path) { + GradleFlags flags; + QFile file(path); if (!file.open(QIODevice::ReadOnly)) - return false; + return flags; + + auto isComment = [](const QByteArray &line) { + const auto trimmed = line.trimmed(); + return trimmed.startsWith("//") || trimmed.startsWith('*') || trimmed.startsWith("/*"); + }; const auto lines = file.readAll().split('\n'); for (const auto &line : lines) { + if (isComment(line)) + continue; if (line.contains("useLegacyPackaging")) { - const auto trimmed = line.trimmed(); - if (!trimmed.startsWith("//") && !trimmed.startsWith('*') && !trimmed.startsWith("/*")) - return true; + flags.setsLegacyPackaging = true; + } else if (line.contains("compileSdkVersion androidCompileSdkVersion.toInteger()")) { + flags.usesIntegerCompileSdkVersion = true; } } - return false; + return flags; } bool buildAndroidProject(const Options &options) @@ -2723,7 +2737,8 @@ bool buildAndroidProject(const Options &options) GradleProperties gradleProperties = readGradleProperties(gradlePropertiesPath); const QString gradleBuildFilePath = options.outputDirectory + "build.gradle"_L1; - if (!gradleSetsLegacyPackagingProperty(gradleBuildFilePath)) + GradleFlags gradleFlags = gradleBuildFlags(gradleBuildFilePath); + if (!gradleFlags.setsLegacyPackaging) gradleProperties["android.bundle.enableUncompressedNativeLibs"] = "false"; gradleProperties["buildDir"] = "build"; @@ -2738,7 +2753,26 @@ bool buildAndroidProject(const Options &options) (options.qtInstallDirectory + u'/' + options.qtDataDirectory + "/src/android/java"_L1) .toUtf8(); - gradleProperties["androidCompileSdkVersion"] = options.androidPlatform.split(u'-').last().toLocal8Bit(); + + QByteArray sdkPlatformVersion; + // Provide the integer version only if build.gradle explicitly converts to Integer, + // to avoid regression to existing projects that build for sdk platform of form android-xx. + if (gradleFlags.usesIntegerCompileSdkVersion) { + const QByteArray tmp = options.androidPlatform.split(u'-').last().toLocal8Bit(); + bool ok; + tmp.toInt(&ok); + if (ok) { + sdkPlatformVersion = tmp; + } else { + fprintf(stderr, "Warning: Gradle expects SDK platform version to be an integer, " + "but the set version is not convertible to an integer."); + } + } + + if (sdkPlatformVersion.isEmpty()) + sdkPlatformVersion = options.androidPlatform.toLocal8Bit(); + + gradleProperties["androidCompileSdkVersion"] = sdkPlatformVersion; gradleProperties["qtMinSdkVersion"] = options.minSdkVersion; gradleProperties["qtTargetSdkVersion"] = options.targetSdkVersion; gradleProperties["androidNdkVersion"] = options.ndkVersion.toUtf8();