electron 打包adb二进制文件,访问不可用的问题
发布于 3 个月前 作者 lcepy 850 次浏览 来自 问答

版本信息如下:

adb :Android Debug Bridge version 1.0.39 Revision 3db08f2c6889-android "electron": “1.7.10”, “electron-builder”: “^19.48.3”, “electron-webpack”: “1.11.0”, “electron-webpack-ts”: “^1.2.0”, node.js:v6.9.4 Mac OS:10.13.1

Expected behavior

可以启动adb service通过adb devices 获取到设备id

Actual behavior


A JavaScript error occurred in the main process

Uncaught Exception:

Error : spawn ENOTDIR

adb放置在resources/mac/adb,代码写在src/index.js中,整体的目录结构如下:

resources // adb二进制文件
src //源代码
.babelrc
package.json

代码:

import os from 'os';
import path  from 'path';
import childProcess from 'child_process';
const spawn = childProcess.spawn;
const PLATFORM = {
  MAC: 'mac',
  WINDOWS: 'windows',
  UNKNOWN: 'unknown'
}
function fetchPlatform(){
  switch(os.platform()){
    case 'darwin':
        return PLATFORM.MAC;
      break;
    case 'win32':
        return PLATFORM.WINDOWS;
      break;
    default:
        return PLATFORM.UNKNOWN;
      break;
  }
}
const platform = fetchPlatform();
const rootDir = path.resolve(__dirname,'../');
const RESOURCES = path.resolve(rootDir,'resources')
const _adbPath = path.resolve(RESOURCES,platform);
function fetchAdbPath(){
  switch(platform){
    case PLATFORM.MAC:
        return path.resolve(_adbPath,'adb');
      break;
    case PLATFORM.WINDOWS:
        return path.resolve(_adbPath, 'adb.exe');
      break;
    default:
        return false;
      break;
  }
}
export function start_server(callback){
  let adbExecPath = fetchAdbPath();
  if (adbExecPath){
    let adb = spawn(adbExecPath,['start-server']);
    adb.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
    });
    adb.stderr.on('data', function (data) {
      console.log('stderr: ' + data);
    });
    adb.on('exit', function (code) {
      typeof callback === 'function' && callback(code);
    });
    adb.stdin.end();
  } else {
    throw new Error('Not Found Android Debug Bridge');
  }
}

How to reproduce

将上述代码做为一个npm包,假设叫adb-test-a,然后在electron工程里,npm install adb-test-a,调用start_server,必抛错误:Error : spawn ENOTDIR

自己发现的问题是:每一个操作系统的版本的路径不同,spawn访问了错误的路径。然后自己也找了一些方法,发现都不起做用,比如asar:false这样的配置关闭asar,依然问题如旧。

1 回复

基本已经确认方案:https://github.com/electron/electron/issues/9459 ,^_^,最后还是靠自己了0.0

回到顶部