1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 BogDan Vatra <bog_dan_ro@yahoo.com>
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "androiddeploystep.h"
#include "androidconstants.h"
#include "androiddeploystepwidget.h"
#include "androidglobal.h"
#include "androidpackagecreationstep.h"
#include "androidrunconfiguration.h"
#include "androidtarget.h"
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
#include <qt4projectmanager/qt4project.h>
#include <qt4projectmanager/qt4target.h>
#include <qt4projectmanager/qt4nodes.h>
#include <qt4projectmanager/qt4buildconfiguration.h>
#include <QDir>
#define ASSERT_STATE(state) ASSERT_STATE_GENERIC(State, state, m_state)
using namespace ProjectExplorer;
using namespace Qt4ProjectManager;
namespace Android {
namespace Internal {
const QLatin1String AndroidDeployStep::Id("Qt4ProjectManager.AndroidDeployStep");
AndroidDeployStep::AndroidDeployStep(ProjectExplorer::BuildStepList *parent)
: BuildStep(parent, Id)
{
ctor();
}
AndroidDeployStep::AndroidDeployStep(ProjectExplorer::BuildStepList *parent,
AndroidDeployStep *other)
: BuildStep(parent, other)
{
ctor();
}
AndroidDeployStep::~AndroidDeployStep() { }
void AndroidDeployStep::ctor()
{
//: AndroidDeployStep default display name
setDefaultDisplayName(tr("Deploy to Android device"));
m_deployAction = NoDeploy;
m_useLocalQtLibs = false;
}
bool AndroidDeployStep::init()
{
AndroidTarget *androidTarget = qobject_cast<AndroidTarget *>(target());
if (!androidTarget) {
raiseError(tr("Cannot deploy: current target is not android."));
return false;
}
const Qt4BuildConfiguration *const bc = androidTarget->activeQt4BuildConfiguration();
m_packageName = androidTarget->packageName();
const QString targetSDK = androidTarget->targetSDK();
writeOutput(tr("Please wait, searching for a suitable device for target:%1.").arg(targetSDK));
m_deviceAPILevel = targetSDK.mid(targetSDK.indexOf(QLatin1Char('-')) + 1).toInt();
m_deviceSerialNumber = AndroidConfigurations::instance().getDeployDeviceSerialNumber(&m_deviceAPILevel);
if (!m_deviceSerialNumber.length()) {
m_deviceSerialNumber.clear();
raiseError(tr("Cannot deploy: no devices or emulators found for your package."));
return false;
}
if (!bc->qtVersion())
return false;
m_qtVersionSourcePath = bc->qtVersion()->sourcePath().toString();
m_qtVersionQMakeBuildConfig = bc->qtVersion()->defaultBuildConfig();
m_androidDirPath = androidTarget->androidDirPath();
m_apkPathDebug = androidTarget->apkPath(AndroidTarget::DebugBuild);
m_apkPathRelease = androidTarget->apkPath(AndroidTarget::ReleaseBuildSigned);
m_buildDirectory = androidTarget->qt4Project()->rootQt4ProjectNode()->buildDir();
m_runQASIPackagePath = m_QASIPackagePath;
m_runDeployAction = m_deployAction;
return true;
}
void AndroidDeployStep::run(QFutureInterface<bool> &fi)
{
fi.reportResult(deployPackage());
}
BuildStepConfigWidget *AndroidDeployStep::createConfigWidget()
{
return new AndroidDeployStepWidget(this);
}
AndroidDeployStep::AndroidDeployAction AndroidDeployStep::deployAction()
{
return m_deployAction;
}
bool AndroidDeployStep::useLocalQtLibs()
{
return m_useLocalQtLibs;
}
void AndroidDeployStep::setDeployAction(AndroidDeployStep::AndroidDeployAction deploy)
{
m_deployAction = deploy;
}
void AndroidDeployStep::setDeployQASIPackagePath(const QString &package)
{
m_QASIPackagePath = package;
m_deployAction = InstallQASI;
}
void AndroidDeployStep::setUseLocalQtLibs(bool useLocal)
{
m_useLocalQtLibs = useLocal;
}
bool AndroidDeployStep::runCommand(QProcess *buildProc,
const QString &program, const QStringList &arguments)
{
writeOutput(tr("Package deploy: Running command '%1 %2'.").arg(program).arg(arguments.join(QLatin1String(" "))), BuildStep::MessageOutput);
buildProc->start(program, arguments);
if (!buildProc->waitForStarted()) {
writeOutput(tr("Packaging error: Could not start command '%1 %2'. Reason: %3")
.arg(program).arg(arguments.join(QLatin1String(" "))).arg(buildProc->errorString()), BuildStep::ErrorMessageOutput);
return false;
}
buildProc->waitForFinished(-1);
if (buildProc->error() != QProcess::UnknownError
|| buildProc->exitCode() != 0) {
QString mainMessage = tr("Packaging Error: Command '%1 %2' failed.")
.arg(program).arg(arguments.join(QLatin1String(" ")));
if (buildProc->error() != QProcess::UnknownError)
mainMessage += tr(" Reason: %1").arg(buildProc->errorString());
else
mainMessage += tr("Exit code: %1").arg(buildProc->exitCode());
writeOutput(mainMessage, BuildStep::ErrorMessageOutput);
return false;
}
return true;
}
void AndroidDeployStep::handleBuildOutput()
{
QProcess *const buildProc = qobject_cast<QProcess *>(sender());
if (!buildProc)
return;
emit addOutput(QString::fromLocal8Bit(buildProc->readAllStandardOutput())
, BuildStep::NormalOutput);
}
void AndroidDeployStep::handleBuildError()
{
QProcess *const buildProc = qobject_cast<QProcess *>(sender());
if (!buildProc)
return;
emit addOutput(QString::fromLocal8Bit(buildProc->readAllStandardError())
, BuildStep::ErrorOutput);
}
QString AndroidDeployStep::deviceSerialNumber()
{
return m_deviceSerialNumber;
}
int AndroidDeployStep::deviceAPILevel()
{
return m_deviceAPILevel;
}
QString AndroidDeployStep::localLibsRulesFilePath()
{
AndroidTarget *androidTarget = qobject_cast<AndroidTarget *>(target());
if (!androidTarget)
return QString();
return androidTarget->localLibsRulesFilePath();
}
void AndroidDeployStep::copyLibs(const QString &srcPath, const QString &destPath, QStringList &copiedLibs, const QStringList &filter)
{
QDir dir;
dir.mkpath(destPath);
QDirIterator libsIt(srcPath, filter, QDir::NoFilter, QDirIterator::Subdirectories);
int pos = srcPath.size();
while (libsIt.hasNext()) {
libsIt.next();
const QString destFile(destPath + libsIt.filePath().mid(pos));
if (libsIt.fileInfo().isDir()) {
dir.mkpath(destFile);
} else {
QFile::copy(libsIt.filePath(), destFile);
copiedLibs.append(destFile);
}
}
}
bool AndroidDeployStep::deployPackage()
{
QProcess *const deployProc = new QProcess;
connect(deployProc, SIGNAL(readyReadStandardOutput()), this,
SLOT(handleBuildOutput()));
connect(deployProc, SIGNAL(readyReadStandardError()), this,
SLOT(handleBuildError()));
if (m_runDeployAction == DeployLocal) {
writeOutput(tr("Clean old qt libs"));
runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber
<< QLatin1String("shell") << QLatin1String("rm") << QLatin1String("-r") << QLatin1String("/data/local/qt"));
writeOutput(tr("Deploy qt libs ... this may take some time, please wait"));
const QString tempPath = QDir::tempPath() + QLatin1String("/android_qt_libs_") + m_packageName;
AndroidPackageCreationStep::removeDirectory(tempPath);
QStringList stripFiles;
copyLibs(m_qtVersionSourcePath + QLatin1String("/lib"),
tempPath + QLatin1String("/lib"), stripFiles, QStringList() << QLatin1String("*.so"));
copyLibs(m_qtVersionSourcePath + QLatin1String("/plugins"),
tempPath + QLatin1String("/plugins"), stripFiles);
copyLibs(m_qtVersionSourcePath + QLatin1String("/imports"),
tempPath + QLatin1String("/imports"), stripFiles);
copyLibs(m_qtVersionSourcePath + QLatin1String("/jar"),
tempPath + QLatin1String("/jar"), stripFiles);
AndroidPackageCreationStep::stripAndroidLibs(stripFiles, target()->activeRunConfiguration()->abi().architecture());
runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber
<< QLatin1String("push") << tempPath << QLatin1String("/data/local/qt"));
AndroidPackageCreationStep::removeDirectory(tempPath);
emit (resetDelopyAction());
}
if (m_runDeployAction == InstallQASI) {
if (!runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber
<< QLatin1String("install") << QLatin1String("-r ") << m_runQASIPackagePath)) {
raiseError(tr("Qt Android smart installer instalation failed"));
disconnect(deployProc, 0, this, 0);
deployProc->deleteLater();
return false;
}
emit resetDelopyAction();
}
deployProc->setWorkingDirectory(m_androidDirPath);
writeOutput(tr("Installing package onto %1.").arg(m_deviceSerialNumber));
runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber << QLatin1String("uninstall") << m_packageName);
QString package = m_apkPathDebug;
if (!(m_qtVersionQMakeBuildConfig & QtSupport::BaseQtVersion::DebugBuild)
&& QFile::exists(m_apkPathRelease))
package = m_apkPathRelease;
if (!runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber << QLatin1String("install") << package)) {
raiseError(tr("Package instalation failed"));
disconnect(deployProc, 0, this, 0);
deployProc->deleteLater();
return false;
}
writeOutput(tr("Pulling files necessary for debugging"));
runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber
<< QLatin1String("pull") << QLatin1String("/system/bin/app_process")
<< QString::fromLatin1("%1/app_process").arg(m_buildDirectory));
runCommand(deployProc, AndroidConfigurations::instance().adbToolPath(),
QStringList() << QLatin1String("-s") << m_deviceSerialNumber << QLatin1String("pull")
<< QLatin1String("/system/lib/libc.so")
<< QString::fromLatin1("%1/libc.so").arg(m_buildDirectory));
disconnect(deployProc, 0, this, 0);
deployProc->deleteLater();
return true;
}
void AndroidDeployStep::raiseError(const QString &errorString)
{
emit addTask(Task(Task::Error, errorString, Utils::FileName::fromString(QString()), -1,
ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
}
void AndroidDeployStep::writeOutput(const QString &text, OutputFormat format)
{
emit addOutput(text, format);
}
} // namespace Internal
} // namespace Qt4ProjectManager
|