[android] build, run and logcat

This commit is contained in:
2019-08-23 14:37:36 +03:00
parent 8205a4cb0c
commit c248744c70
3 changed files with 71 additions and 36 deletions

View File

@@ -1,6 +1,9 @@
const fs = require('fs');
const path = require('path');
const exec = require('./exec');
const run = require('../run/index');
const Command = require('../run/command');
const through = require('through2');
const Sdk = require('./sdk');
const Env = require('./env');
@@ -10,12 +13,12 @@ class Android extends Sdk {
constructor(version) {
super(Android.ID, version || Android.VERSION);
this.platform = 19;
this.ndk_version = 'r15c';
}
get prepared() {
try {
return fs.existsSync(`${this.path}/platform-tools/adb`);
return fs.existsSync(`${this.path}/tools/bin/sdkmanager`);
} catch (e) {
return false;
}
@@ -49,9 +52,10 @@ class Android extends Sdk {
}
prepare_ndk() {
const url = 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip';
// ToDo: download and extract to `this.ndk_home`
return Promise.resolve();
// ToDo: support windows
const url = `https://dl.google.com/android/repository/android-ndk-${this.ndk_version}-linux-x86_64.zip`;
this.log.d('download: *%s*', url);
return Sdk.Downloader.download(url, this.ndk_home);
}
sdkmanager(packages) {
@@ -69,14 +73,18 @@ class Android extends Sdk {
Env.set('NDK_HOME', this.ndk_home);
}
get adbBin() {
return path.join(this.path, 'platform-tools/adb');
}
adb(args) {
const adbBin = path.join(this.path, 'platform-tools/adb');
return exec('.', [adbBin].concat(args).join(' ')).then(data => {
return exec('.', [this.adbBin].concat(args).join(' ')).then(data => {
for (let line of data.stderr.split('\n')) {
if (line.indexOf('Error') > -1) {
throw line;
}
}
return data;
});
}
@@ -128,9 +136,25 @@ class Android extends Sdk {
start() {
const self = this;
return through.obj((file, enc, callback) => {
return through.obj(function(file, enc, callback) {
const name = `${file.package}/${file.activity}`;
self.adb(['shell', 'am', 'start', '-n', name]).then(() => callback());
self.adb(['shell', 'am', 'start', '-n', name]).then(() => {
setTimeout(() => {
self.adb(['shell', `"ps | grep ${file.package}"`]).then(result => {
file.pid = result.stdout.split(/\s+/)[1];
this.push(file);
callback();
});
}, 1000);
});
});
}
logcat() {
const self = this;
return through.obj(function(file, enc, callback) {
const cmd = `${self.adbBin} logcat --pid ${file.pid}`;
this.push(new Command(cmd).exec());
});
}
}