forked from qt-creator/qt-creator
Android: Say hello to gradle!
Switching from Ant to Gradle brings lots of advantages: - it is way faster when rebuilding (25-50% faster than ant). - it enables first class Android Studio integration. - adding Android Extras libs (e.g. Google Play services, OBB, etc.) to your project is now painless. [ChangeLog][Android] Added Gradle support to build the APK. Change-Id: Iee492954f8ffb2c22e6ab14a8a25faf644de9a51 Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
@@ -595,4 +595,112 @@ AndroidQtSupport *AndroidManager::androidQtSupport(ProjectExplorer::Target *targ
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool AndroidManager::useGradle(ProjectExplorer::Target *target)
|
||||
{
|
||||
if (!target)
|
||||
return false;
|
||||
AndroidBuildApkStep *buildApkStep
|
||||
= AndroidGlobal::buildStep<AndroidBuildApkStep>(target->activeBuildConfiguration());
|
||||
return buildApkStep && buildApkStep->useGradle();
|
||||
}
|
||||
|
||||
typedef QMap<QByteArray, QByteArray> GradleProperties;
|
||||
|
||||
static GradleProperties readGradleProperties(const QString &path)
|
||||
{
|
||||
GradleProperties properties;
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return properties;
|
||||
|
||||
foreach (const QByteArray &line, file.readAll().split('\n')) {
|
||||
if (line.trimmed().startsWith('#'))
|
||||
continue;
|
||||
|
||||
QList<QByteArray> prop(line.split('='));
|
||||
if (prop.size() > 1)
|
||||
properties[prop.at(0).trimmed()] = prop.at(1).trimmed();
|
||||
}
|
||||
file.close();
|
||||
return properties;
|
||||
}
|
||||
|
||||
static bool mergeGradleProperties(const QString &path, GradleProperties properties)
|
||||
{
|
||||
QFile::remove(path + QLatin1Char('~'));
|
||||
QFile::rename(path, path + QLatin1Char('~'));
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text))
|
||||
return false;
|
||||
|
||||
QFile oldFile(path + QLatin1Char('~'));
|
||||
if (oldFile.open(QIODevice::ReadOnly)) {
|
||||
while (!oldFile.atEnd()) {
|
||||
QByteArray line(oldFile.readLine());
|
||||
QList<QByteArray> prop(line.split('='));
|
||||
if (prop.size() > 1) {
|
||||
GradleProperties::iterator it = properties.find(prop.at(0).trimmed());
|
||||
if (it != properties.end()) {
|
||||
file.write(it.key() + '=' + it.value() + '\n');
|
||||
properties.erase(it);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
file.write(line);
|
||||
}
|
||||
oldFile.close();
|
||||
} else {
|
||||
file.write("## This file is automatically generated by QtCreator.\n"
|
||||
"#\n"
|
||||
"# This file must *NOT* be checked into Version Control Systems,\n"
|
||||
"# as it contains information specific to your local configuration.\n\n");
|
||||
|
||||
}
|
||||
|
||||
for (GradleProperties::const_iterator it = properties.begin(); it != properties.end(); ++it)
|
||||
file.write(it.key() + '=' + it.value() + '\n');
|
||||
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool AndroidManager::updateGradleProperties(ProjectExplorer::Target *target)
|
||||
{
|
||||
QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(target->kit());
|
||||
if (!version)
|
||||
return false;
|
||||
|
||||
AndroidBuildApkStep *buildApkStep
|
||||
= AndroidGlobal::buildStep<AndroidBuildApkStep>(target->activeBuildConfiguration());
|
||||
|
||||
if (!buildApkStep || !buildApkStep->androidPackageSourceDir().appendPath(QLatin1String("gradlew")).toFileInfo().exists())
|
||||
return false;
|
||||
|
||||
GradleProperties localProperties;
|
||||
localProperties["sdk.dir"] = AndroidConfigurations::currentConfig().sdkLocation().toString().toLocal8Bit();
|
||||
if (!mergeGradleProperties(buildApkStep->androidPackageSourceDir().appendPath(QLatin1String("local.properties")).toString(), localProperties))
|
||||
return false;
|
||||
|
||||
QString gradlePropertiesPath = buildApkStep->androidPackageSourceDir().appendPath(QLatin1String("gradle.properties")).toString();
|
||||
GradleProperties gradleProperties = readGradleProperties(gradlePropertiesPath);
|
||||
gradleProperties["qt5AndroidDir"] = version->qmakeProperty("QT_INSTALL_PREFIX")
|
||||
.append(QLatin1String("/src/android/java")).toLocal8Bit();
|
||||
gradleProperties["buildDir"] = ".build";
|
||||
gradleProperties["androidCompileSdkVersion"] = buildTargetSDK(target).split(QLatin1Char('-')).last().toLocal8Bit();
|
||||
if (gradleProperties["androidBuildToolsVersion"].isEmpty()) {
|
||||
QString maxVersion;
|
||||
QDir buildToolsDir(AndroidConfigurations::currentConfig().sdkLocation().appendPath(QLatin1String("build-tools")).toString());
|
||||
foreach (const QFileInfo &file, buildToolsDir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)) {
|
||||
QString ver(file.fileName());
|
||||
if (maxVersion < ver)
|
||||
maxVersion = ver;
|
||||
}
|
||||
if (maxVersion.isEmpty())
|
||||
return false;
|
||||
gradleProperties["androidBuildToolsVersion"] = maxVersion.toLocal8Bit();
|
||||
}
|
||||
return mergeGradleProperties(gradlePropertiesPath, gradleProperties);
|
||||
}
|
||||
|
||||
} // namespace Android
|
||||
|
||||
Reference in New Issue
Block a user