Android: Fix process id parsing for API 22

Task-number: QTCREATORBUG-22098
Change-Id: I8aeed9179538689083aacf9487ae0dca8feaed97
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Vikas Pachdha
2019-03-13 14:38:18 +01:00
parent fa0a2d97e9
commit 6ff0f94768

View File

@@ -95,14 +95,14 @@ static bool isTimedOut(const chrono::high_resolution_clock::time_point &start,
return timedOut; return timedOut;
} }
static qint64 extractPID(const QString &output, const QString &packageName) static qint64 extractPID(const QByteArray &output, const QString &packageName)
{ {
qint64 pid = -1; qint64 pid = -1;
foreach (auto tuple, output.split('\n')) { foreach (auto tuple, output.split('\n')) {
tuple = tuple.simplified(); tuple = tuple.simplified();
if (!tuple.isEmpty()) { if (!tuple.isEmpty()) {
auto parts = tuple.split(':'); auto parts = tuple.split(':');
QString commandName = parts.first(); QString commandName = QString::fromLocal8Bit(parts.first());
if (parts.length() == 2 && commandName == packageName) { if (parts.length() == 2 && commandName == packageName) {
pid = parts.last().toLongLong(); pid = parts.last().toLongLong();
break; break;
@@ -121,18 +121,17 @@ static void findProcessPID(QFutureInterface<qint64> &fi, QStringList selector,
qint64 processPID = -1; qint64 processPID = -1;
chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now(); chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
selector.append("shell");
selector.append(preNougat ? pidScriptPreNougat : pidScript.arg(packageName));
do { do {
QThread::msleep(200); QThread::msleep(200);
SdkToolResult result = AndroidManager::runAdbCommand(selector); QString adbPath = AndroidConfigurations::currentConfig().adbToolPath().toString();
selector.append("shell");
selector.append(preNougat ? pidScriptPreNougat : pidScript.arg(packageName));
const auto out = Utils::SynchronousProcess().runBlocking(adbPath, selector).allRawOutput();
if (preNougat) { if (preNougat) {
processPID = extractPID(result.stdOut(), packageName); processPID = extractPID(out, packageName);
} else { } else {
if (!result.stdOut().isEmpty()) if (!out.isEmpty())
processPID = result.stdOut().trimmed().toLongLong(); processPID = out.trimmed().toLongLong();
} }
} while (processPID == -1 && !isTimedOut(start) && !fi.isCanceled()); } while (processPID == -1 && !isTimedOut(start) && !fi.isCanceled());
@@ -321,11 +320,8 @@ void AndroidRunnerWorker::forceStop()
runAdb({"shell", "am", "force-stop", m_packageName}); runAdb({"shell", "am", "force-stop", m_packageName});
// try killing it via kill -9 // try killing it via kill -9
QString out; if (m_processPID != -1)
runAdb({"shell", pidScriptPreNougat}, &out); adbKill(m_processPID);
qint64 pid = extractPID(out.simplified(), m_packageName);
if (pid != -1)
adbKill(pid);
} }
void AndroidRunnerWorker::logcatReadStandardError() void AndroidRunnerWorker::logcatReadStandardError()