2016-01-15 14:57:40 +01:00
|
|
|
/****************************************************************************
|
2012-04-18 20:30:57 +03:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 BogDan Vatra <bog_dan_ro@yahoo.com>
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2012-04-18 20:30:57 +03:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2012-04-18 20:30:57 +03:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-04-18 20:30:57 +03:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2012-04-18 20:30:57 +03:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2012-04-18 20:30:57 +03:00
|
|
|
|
|
|
|
|
#include "androidrunner.h"
|
|
|
|
|
|
2018-05-04 16:01:13 +02:00
|
|
|
#include "androidconstants.h"
|
2013-09-17 18:24:57 +02:00
|
|
|
#include "androiddeployqtstep.h"
|
2012-04-18 20:30:57 +03:00
|
|
|
#include "androidconfigurations.h"
|
|
|
|
|
#include "androidrunconfiguration.h"
|
2012-04-24 15:49:09 +02:00
|
|
|
#include "androidmanager.h"
|
2017-04-03 11:11:17 +02:00
|
|
|
#include "androidavdmanager.h"
|
2018-03-17 09:31:56 +02:00
|
|
|
#include "androidrunnerworker.h"
|
2012-04-24 15:49:09 +02:00
|
|
|
|
2017-06-14 14:59:23 +02:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2016-06-08 16:57:32 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/projectexplorersettings.h>
|
2018-05-04 16:01:13 +02:00
|
|
|
#include <projectexplorer/runconfigurationaspects.h>
|
2012-04-24 15:49:09 +02:00
|
|
|
#include <projectexplorer/target.h>
|
2017-09-29 12:41:53 +02:00
|
|
|
#include <utils/url.h>
|
2012-04-18 20:30:57 +03:00
|
|
|
|
2016-06-08 16:57:32 +02:00
|
|
|
using namespace ProjectExplorer;
|
2017-05-19 14:40:49 +02:00
|
|
|
using namespace Utils;
|
2016-06-08 16:57:32 +02:00
|
|
|
|
2014-09-18 18:29:35 +02:00
|
|
|
/*
|
|
|
|
|
This uses explicit handshakes between the application and the
|
2014-11-18 15:14:40 +01:00
|
|
|
gdbserver start and the host side by using the gdbserver socket.
|
|
|
|
|
|
|
|
|
|
For the handshake there are two mechanisms. Only the first method works
|
|
|
|
|
on Android 5.x devices and is chosen as default option. The second
|
|
|
|
|
method can be enabled by setting the QTC_ANDROID_USE_FILE_HANDSHAKE
|
|
|
|
|
environment variable before starting Qt Creator.
|
|
|
|
|
|
|
|
|
|
1.) This method uses a TCP server on the Android device which starts
|
|
|
|
|
listening for incoming connections. The socket is forwarded by adb
|
|
|
|
|
and creator connects to it. This is the only method that works
|
|
|
|
|
on Android 5.x devices.
|
|
|
|
|
|
|
|
|
|
2.) This method uses two files ("ping" file in the application dir,
|
|
|
|
|
"pong" file in /data/local/tmp/qt).
|
2014-09-18 18:29:35 +02:00
|
|
|
|
|
|
|
|
The sequence is as follows:
|
|
|
|
|
|
|
|
|
|
host: adb forward debugsocket :5039
|
|
|
|
|
|
|
|
|
|
host: adb shell rm pong file
|
|
|
|
|
host: adb shell am start
|
|
|
|
|
host: loop until ping file appears
|
|
|
|
|
|
|
|
|
|
app start up: launch gdbserver --multi +debug-socket
|
|
|
|
|
app start up: loop until debug socket appear
|
|
|
|
|
|
|
|
|
|
gdbserver: normal start up including opening debug-socket,
|
|
|
|
|
not yet attached to any process
|
|
|
|
|
|
2014-11-18 15:14:40 +01:00
|
|
|
app start up: 1.) set up ping connection or 2.) touch ping file
|
|
|
|
|
app start up: 1.) accept() or 2.) loop until pong file appears
|
2014-09-18 18:29:35 +02:00
|
|
|
|
|
|
|
|
host: start gdb
|
|
|
|
|
host: gdb: set up binary, breakpoints, path etc
|
|
|
|
|
host: gdb: target extended-remote :5039
|
|
|
|
|
|
|
|
|
|
gdbserver: accepts connection from gdb
|
|
|
|
|
|
|
|
|
|
host: gdb: attach <application-pid>
|
|
|
|
|
|
|
|
|
|
gdbserver: attaches to the application
|
|
|
|
|
and stops it
|
|
|
|
|
|
|
|
|
|
app start up: stopped now (it is still waiting for
|
|
|
|
|
the pong anyway)
|
|
|
|
|
|
|
|
|
|
host: gdb: continue
|
|
|
|
|
|
|
|
|
|
gdbserver: resumes application
|
|
|
|
|
|
|
|
|
|
app start up: resumed (still waiting for the pong)
|
|
|
|
|
|
2014-11-18 15:14:40 +01:00
|
|
|
host: 1) write "ok" to ping pong connection or 2.) write pong file
|
2014-09-18 18:29:35 +02:00
|
|
|
|
|
|
|
|
app start up: java code continues now, the process
|
|
|
|
|
is already fully under control
|
|
|
|
|
of gdbserver. Breakpoints are set etc,
|
|
|
|
|
we are before main.
|
|
|
|
|
app start up: native code launches
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-18 20:30:57 +03:00
|
|
|
namespace Android {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-04-17 10:09:35 +02:00
|
|
|
AndroidRunner::AndroidRunner(RunControl *runControl,
|
|
|
|
|
const QString &intentName,
|
|
|
|
|
const QString &extraAppParams,
|
|
|
|
|
const Utils::Environment &extraEnvVars)
|
2017-05-19 14:40:49 +02:00
|
|
|
: RunWorker(runControl), m_target(runControl->runConfiguration()->target())
|
2016-08-05 15:40:33 +02:00
|
|
|
{
|
2017-05-19 14:40:49 +02:00
|
|
|
setDisplayName("AndroidRunner");
|
2016-08-05 15:40:33 +02:00
|
|
|
static const int metaTypes[] = {
|
|
|
|
|
qRegisterMetaType<QVector<QStringList> >("QVector<QStringList>"),
|
|
|
|
|
qRegisterMetaType<Utils::Port>("Utils::Port")
|
|
|
|
|
};
|
|
|
|
|
Q_UNUSED(metaTypes);
|
|
|
|
|
|
|
|
|
|
m_checkAVDTimer.setInterval(2000);
|
|
|
|
|
connect(&m_checkAVDTimer, &QTimer::timeout, this, &AndroidRunner::checkAVD);
|
|
|
|
|
|
2018-01-30 18:28:23 +01:00
|
|
|
m_androidRunnable.intentName = intentName.isEmpty() ? AndroidManager::intentName(m_target)
|
|
|
|
|
: intentName;
|
2016-08-05 15:40:33 +02:00
|
|
|
m_androidRunnable.packageName = m_androidRunnable.intentName.left(
|
|
|
|
|
m_androidRunnable.intentName.indexOf(QLatin1Char('/')));
|
2018-01-30 18:28:23 +01:00
|
|
|
|
2018-05-04 16:01:13 +02:00
|
|
|
RunConfiguration *rc = runControl->runConfiguration();
|
|
|
|
|
if (auto aspect = rc->extraAspect(Constants::ANDROID_AMSTARTARGS_ASPECT))
|
|
|
|
|
m_androidRunnable.amStartExtraArgs = static_cast<BaseStringAspect *>(aspect)->value().split(' ');
|
|
|
|
|
|
|
|
|
|
if (auto aspect = rc->extraAspect(Constants::ANDROID_PRESTARTSHELLCMDLIST_ASPECT)) {
|
|
|
|
|
for (QString shellCmd : static_cast<BaseStringListAspect *>(aspect)->value())
|
2018-01-30 18:31:40 +01:00
|
|
|
m_androidRunnable.beforeStartAdbCommands.append(QString("shell %1").arg(shellCmd));
|
2018-05-04 16:01:13 +02:00
|
|
|
}
|
2018-01-30 18:31:40 +01:00
|
|
|
|
2018-05-04 16:01:13 +02:00
|
|
|
if (auto aspect = rc->extraAspect(Constants::ANDROID_POSTSTARTSHELLCMDLIST_ASPECT)) {
|
|
|
|
|
for (QString shellCmd : static_cast<BaseStringListAspect *>(aspect)->value())
|
2018-01-30 18:31:40 +01:00
|
|
|
m_androidRunnable.afterFinishAdbCommands.append(QString("shell %1").arg(shellCmd));
|
|
|
|
|
}
|
2017-05-15 14:48:39 +02:00
|
|
|
|
2018-05-07 17:13:52 +02:00
|
|
|
const int apiLevel = AndroidManager::deviceApiLevel(m_target);
|
2018-05-09 12:20:54 +02:00
|
|
|
m_worker.reset(new AndroidRunnerWorker(runControl, m_androidRunnable));
|
|
|
|
|
m_worker->setIsPreNougat(apiLevel <= 23);
|
2018-05-07 18:06:30 +02:00
|
|
|
m_worker->setExtraAppParams(extraAppParams);
|
|
|
|
|
m_worker->setExtraEnvVars(extraEnvVars);
|
|
|
|
|
|
2016-08-05 15:40:33 +02:00
|
|
|
m_worker->moveToThread(&m_thread);
|
|
|
|
|
|
2018-05-09 12:20:54 +02:00
|
|
|
connect(this, &AndroidRunner::asyncStart, m_worker.data(), &AndroidRunnerWorker::asyncStart);
|
|
|
|
|
connect(this, &AndroidRunner::asyncStop, m_worker.data(), &AndroidRunnerWorker::asyncStop);
|
2018-05-07 17:13:52 +02:00
|
|
|
connect(this, &AndroidRunner::androidDeviceInfoChanged,
|
2018-05-09 12:20:54 +02:00
|
|
|
m_worker.data(), &AndroidRunnerWorker::setAndroidDeviceInfo);
|
|
|
|
|
connect(m_worker.data(), &AndroidRunnerWorker::remoteProcessStarted,
|
2017-05-19 14:40:49 +02:00
|
|
|
this, &AndroidRunner::handleRemoteProcessStarted);
|
2018-05-09 12:20:54 +02:00
|
|
|
connect(m_worker.data(), &AndroidRunnerWorker::remoteProcessFinished,
|
2017-05-19 14:40:49 +02:00
|
|
|
this, &AndroidRunner::handleRemoteProcessFinished);
|
2018-05-09 12:20:54 +02:00
|
|
|
connect(m_worker.data(), &AndroidRunnerWorker::remoteOutput,
|
2016-08-05 15:40:33 +02:00
|
|
|
this, &AndroidRunner::remoteOutput);
|
2018-05-09 12:20:54 +02:00
|
|
|
connect(m_worker.data(), &AndroidRunnerWorker::remoteErrorOutput,
|
2016-08-05 15:40:33 +02:00
|
|
|
this, &AndroidRunner::remoteErrorOutput);
|
|
|
|
|
|
2017-06-14 14:59:23 +02:00
|
|
|
connect(&m_outputParser, &QmlDebug::QmlOutputParser::waitingForConnectionOnPort,
|
|
|
|
|
this, &AndroidRunner::qmlServerPortReady);
|
|
|
|
|
|
2016-08-05 15:40:33 +02:00
|
|
|
m_thread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AndroidRunner::~AndroidRunner()
|
|
|
|
|
{
|
|
|
|
|
m_thread.quit();
|
|
|
|
|
m_thread.wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidRunner::start()
|
|
|
|
|
{
|
|
|
|
|
if (!ProjectExplorerPlugin::projectExplorerSettings().deployBeforeRun) {
|
|
|
|
|
// User choose to run the app without deployment. Start the AVD if not running.
|
|
|
|
|
launchAVD();
|
|
|
|
|
if (!m_launchedAVDName.isEmpty()) {
|
|
|
|
|
m_checkAVDTimer.start();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 14:57:09 +02:00
|
|
|
emit asyncStart();
|
2016-08-05 15:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidRunner::stop()
|
|
|
|
|
{
|
|
|
|
|
if (m_checkAVDTimer.isActive()) {
|
|
|
|
|
m_checkAVDTimer.stop();
|
2017-05-19 14:40:49 +02:00
|
|
|
appendMessage("\n\n" + tr("\"%1\" terminated.").arg(m_androidRunnable.packageName),
|
|
|
|
|
Utils::DebugFormat);
|
2016-08-05 15:40:33 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 14:57:09 +02:00
|
|
|
emit asyncStop();
|
2016-08-05 15:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-14 14:59:23 +02:00
|
|
|
void AndroidRunner::qmlServerPortReady(Port port)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Note that the passed is nonsense, as the port is on the
|
|
|
|
|
// device side. It only happens to work since we redirect
|
|
|
|
|
// host port n to target port n via adb.
|
|
|
|
|
QUrl serverUrl;
|
|
|
|
|
serverUrl.setPort(port.number());
|
2017-10-13 10:02:41 +02:00
|
|
|
serverUrl.setScheme(urlTcpScheme());
|
2017-06-14 14:59:23 +02:00
|
|
|
emit qmlServerReady(serverUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 14:40:49 +02:00
|
|
|
void AndroidRunner::remoteOutput(const QString &output)
|
|
|
|
|
{
|
2017-06-14 14:59:23 +02:00
|
|
|
Core::MessageManager::write("LOGCAT: " + output, Core::MessageManager::Silent);
|
2017-05-19 14:40:49 +02:00
|
|
|
appendMessage(output, Utils::StdOutFormatSameLine);
|
2017-06-14 14:59:23 +02:00
|
|
|
m_outputParser.processOutput(output);
|
2017-05-19 14:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidRunner::remoteErrorOutput(const QString &output)
|
2012-04-18 20:30:57 +03:00
|
|
|
{
|
2017-06-14 14:59:23 +02:00
|
|
|
Core::MessageManager::write("LOGCAT: " + output, Core::MessageManager::Silent);
|
2017-05-19 14:40:49 +02:00
|
|
|
appendMessage(output, Utils::StdErrFormatSameLine);
|
2017-06-14 14:59:23 +02:00
|
|
|
m_outputParser.processOutput(output);
|
2017-05-19 14:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-18 12:23:42 +02:00
|
|
|
void AndroidRunner::handleRemoteProcessStarted(Utils::Port gdbServerPort,
|
|
|
|
|
const QUrl &qmlServer, int pid)
|
2017-05-19 14:40:49 +02:00
|
|
|
{
|
|
|
|
|
m_pid = ProcessHandle(pid);
|
|
|
|
|
m_gdbServerPort = gdbServerPort;
|
2017-08-18 12:23:42 +02:00
|
|
|
m_qmlServer = qmlServer;
|
2017-05-19 14:40:49 +02:00
|
|
|
reportStarted();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidRunner::handleRemoteProcessFinished(const QString &errString)
|
|
|
|
|
{
|
|
|
|
|
appendMessage(errString, Utils::DebugFormat);
|
2017-06-14 14:59:23 +02:00
|
|
|
if (runControl()->isRunning())
|
|
|
|
|
runControl()->initiateStop();
|
2017-05-19 14:40:49 +02:00
|
|
|
reportStopped();
|
2016-01-29 14:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-05 15:40:33 +02:00
|
|
|
void AndroidRunner::launchAVD()
|
|
|
|
|
{
|
2017-11-25 13:57:44 +03:00
|
|
|
if (!m_target || !m_target->project())
|
2017-05-19 14:40:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int deviceAPILevel = AndroidManager::minimumSDK(m_target);
|
|
|
|
|
QString targetArch = AndroidManager::targetArch(m_target);
|
2016-08-05 15:40:33 +02:00
|
|
|
|
|
|
|
|
// Get AVD info.
|
|
|
|
|
AndroidDeviceInfo info = AndroidConfigurations::showDeviceDialog(
|
2017-09-04 15:21:16 -07:00
|
|
|
m_target->project(), deviceAPILevel, targetArch);
|
2017-05-19 14:40:49 +02:00
|
|
|
AndroidManager::setDeviceSerialNumber(m_target, info.serialNumber);
|
2018-05-07 17:13:52 +02:00
|
|
|
emit androidDeviceInfoChanged(info);
|
2016-08-05 15:40:33 +02:00
|
|
|
if (info.isValid()) {
|
2017-04-03 11:11:17 +02:00
|
|
|
AndroidAvdManager avdManager;
|
|
|
|
|
if (avdManager.findAvd(info.avdname).isEmpty()) {
|
|
|
|
|
bool launched = avdManager.startAvdAsync(info.avdname);
|
2016-08-05 15:40:33 +02:00
|
|
|
m_launchedAVDName = launched ? info.avdname:"";
|
|
|
|
|
} else {
|
|
|
|
|
m_launchedAVDName.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidRunner::checkAVD()
|
|
|
|
|
{
|
|
|
|
|
const AndroidConfig &config = AndroidConfigurations::currentConfig();
|
2017-04-03 11:11:17 +02:00
|
|
|
AndroidAvdManager avdManager(config);
|
|
|
|
|
QString serialNumber = avdManager.findAvd(m_launchedAVDName);
|
2016-08-05 15:40:33 +02:00
|
|
|
if (!serialNumber.isEmpty())
|
|
|
|
|
return; // try again on next timer hit
|
|
|
|
|
|
2017-04-03 11:11:17 +02:00
|
|
|
if (avdManager.isAvdBooted(serialNumber)) {
|
2016-08-05 15:40:33 +02:00
|
|
|
m_checkAVDTimer.stop();
|
2017-05-19 14:40:49 +02:00
|
|
|
AndroidManager::setDeviceSerialNumber(m_target, serialNumber);
|
2017-06-08 14:57:09 +02:00
|
|
|
emit asyncStart();
|
2016-08-05 15:40:33 +02:00
|
|
|
} else if (!config.isConnected(serialNumber)) {
|
|
|
|
|
// device was disconnected
|
|
|
|
|
m_checkAVDTimer.stop();
|
|
|
|
|
}
|
2012-04-18 20:30:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2013-10-16 11:02:37 +02:00
|
|
|
} // namespace Android
|