如何通过命令行关闭Android模拟器

31

我无法通过命令提示符优雅地停止模拟器。

我的操作系统是Linux Ubuntu v10.04(64位),Android版本是v2.3(API 9 - Gingerbread)。

我使用快照启动了模拟器。现在我的问题是要优雅地关闭正在运行的模拟器实例。我尝试使用 kill -9 (正在运行的模拟器的进程ID)来关闭模拟器,但这会使其快照损坏,下次无法正常启动。请帮助我避免强制关闭模拟器。

您有什么解决方法吗?


为什么需要通过命令行关闭它? - Mike dg
3个回答

66
请不要滥用kill -9,这是一个非常糟糕的习惯。
正确的命令是:
 $ adb emu kill

或者我应该更好地说它曾经是正确的命令,直到最近一些adb的变化。似乎有人忘记加上身份验证。

在最新的(截至2016年6月)最新版本中,adb的版本是

$ adb version
Android Debug Bridge version 1.0.36
Revision 0a04cdc4a62f-android

当你尝试时

$ adb emu kill

什么都没发生,这就是原因。

...
connect(3, {sa_family=AF_INET, sin_port=htons(5554), 
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
write(3, "kill\nquit\n", 10)            = 10
read(3, "\377\373\1", 8192)             = 3
read(3, "\377\373\3\377\373\0\377\375\0", 8192) = 9
read(3, "Android Console: Authentication required\r\nAndroid Console: type 'auth <auth_token>' to authenticate\r\nAndroid Console: you can find your <auth_token> in \r\n'/home/diego/.emulator_console_auth_token'\r\nOK\r\n", 8192) = 202
read(3, "k\33[K", 8192)                 = 4
read(3, "\33[Dki\33[K", 8192)           = 8
read(3, "\33[D\33[Dkil\33[K\33[D\33[D\33[Dkill\33[K", 8192) = 28
read(3, "\r\nKO: unknown command, try 'help'\r\n", 8192) = 35
read(3, "q\33[K\33[Dqu\33[K", 8192)     = 12
read(3, "\33[D\33[Dqui\33[K\33[D\33[D\33[Dquit\33[K", 8192) = 28
read(3, "\r\n", 8192)                   = 2
read(3, "", 8192)                       = 0
close(3)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

那么我们需要另一个解决方案。

如果之前的命令不起作用(如一些用户在Windows上所报告的),你可以尝试使用以下命令(下一个命令中,5554是模拟器使用的端口)。

将token文件(~/.emulator_console_auth_token)的内容复制到剪贴板,以便您可以在telnet会话期间粘贴它:

$ telnet localhost 5554

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in 
'/home/user/.emulator_console_auth_token'
OK
auth <YOUR_TOKEN_HERE>
Android Console: type 'help' for a list of commands
OK
Android console command help:

    help|h|?         print a list of commands
    crash            crash the emulator instance
    kill             kill the emulator instance
    quit|exit        quit control session
    redir            manage port redirections
    power            power related commands
    event            simulate hardware events
    avd              control virtual device execution
    finger           manage emulator fingerprint
    geo              Geo-location commands
    sms              SMS related commands
    cdma             CDMA related commands
    gsm              GSM related commands
    rotate           rotate the screen by 90 degrees

try 'help <command>' for command-specific help
OK

然后,你只需要在命令提示符中输入kill

kill
OK: killing emulator, bye bye
Connection closed by foreign host.

然而,等等,应该有更好的方法。实际上确实如此!

这个gist提供了一个自动化解决方案,使用expect而不是每次都要剪切和粘贴身份验证令牌。

希望您觉得它有用。


5
这在Linux上运行良好,但在Windows 7上无法运行。这个问题已经被提出了一个错误报告:http://code.google.com/p/android/issues/detail?id=21021 - inazaruk
我必须先验证我的telnet会话(以使用“kill”命令),使用命令“auth <AUTH_TOKEN>”。认证令牌位于我的主目录中的文件“.emulator_console_auth_token”中(请参阅您所在位置的telnet登录说明)。 - Koen

4

我在ubuntu上遇到了一些问题,模拟器会不断地打开新的进程。我无法关闭模拟器,它也没有响应。

我使用了htop

在htop中的步骤:

  1. 按下F4进行筛选。
  2. 筛选“avd”。
  3. 按下F5以查看树形结构。
  4. 找到并点击父进程。
  5. 按下F9打开杀死菜单。
  6. 选择信号9并输入。

不错!对我有用,现在我也学会了htop命令 :) - Kartoos

0
在Ubuntu 16-04上,使用ADB版本1.0.32,在docker容器中运行Android 4.4 (API 19)的模拟器。暴露的端口为30004用于控制台和30005用于ADB。
我可以通过执行adb connect 0.0.0.0:30005来连接到它。
然而,要关闭模拟器,我必须使用adb -s emulator-30004 emu kill,使用0.0.0.0:30005会给我带来错误:未检测到模拟器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接