我在Mac上,在终端上进行Android开发。我已经成功地创建了HelloWorld项目,现在我试图从Android模拟器中的命令行运行它。哪个命令运行HelloWorld项目的模拟器?

我已经有Android工具和平台工具在我的路径。

编辑:

如何告诉模拟器从命令行运行HelloWorld项目?我已经用ant构建了这个项目。


当前回答

运行第一个可用的模拟器:

~/Library/Android/sdk/tools/emulator -avd `~/Library/Android/sdk/tools/emulator -list-avds | awk '{print $1}'`

其他回答

一个启动程序脚本,列出现有的avd,让你选择一个你想要的。需要python3(至少v3.4)和一个有效的环境变量ANDROID_HOME或ANDROID_SDK指向Android SDK目录。

#!/usr/bin/env python3

import os
import subprocess
from pathlib import Path

android_emulator_dir: Path = Path(os.environ['ANDROID_HOME'] or os.environ['ANDROID_SDK']) / 'emulator'

if android_emulator_dir.exists():
    emulator_dir = android_emulator_dir.absolute()
    print(f'SDK emulator dir: {emulator_dir}', end='\n\n')

    proc = subprocess.Popen(['./emulator', '-list-avds'], stdout=subprocess.PIPE, cwd=emulator_dir, text=True)
    avds = {idx: avd_name.strip() for idx, avd_name in enumerate(proc.stdout, start=1)}

    print('\n'.join([f'{idx}: {avd_name}' for idx, avd_name in avds.items()]))

    avd_idx = input("\nType AVD index and press Enter... ")
    avd_name = avds.get(int(avd_idx))

    if avd_name:
        subprocess.Popen(['./emulator', '-avd', avd_name, '-no-boot-anim'], cwd=emulator_dir)
    else:
        print('Invalid AVD index')
else:
    print(f'Either $ANDROID_HOME or $ANDROID_SDK must be defined!')

AppleScript版本(仅限MacOS)

osascript -e '
set avds to paragraphs of (do shell script "~/Library/Android/sdk/emulator/emulator -list-avds")
set avd to (choose from list avds with prompt "Please select an AVD to start" default items "None" OK button name {"Start"} cancel button name {"Cancel"})
do shell script "~/Library/Android/sdk/emulator/emulator -avd " & avd & " -no-boot-anim > /dev/null 2>&1 &"
'

上面的脚本也可以在脚本编辑器中运行。app或Automator。App(单引号之间的位)。也可以使用脚本编辑器将其保存为原生macos应用程序:在菜单上选择“文件”,按住“选项”键,选择“另存为”,然后选择“文件格式:应用程序”。

我假设你已经构建了你的项目,只需要启动它,但你没有创建任何avd,必须使用命令行进行所有操作。你必须做以下事情。

Create a new virtual device (AVD) for the platform you need. If you have to use command line for creating your AVD, you can call android create avd -n <name> -t <targetID> where targetID is the API level you need. If you can use GUI, just type in android avd and it will launch the manager, where you can do the same. You can read more about AVD management through GUI and through command line. Run the AVD either by using command emulator -avd <name> or through previously launched GUI. Wait until the emulator fully loads, it takes some time. You can read about additional options here. Now you have to install the application to your AVD. Usually during development you just use the same Ant script you used to build the project, just select install target. However, you can install the application manually using command adb install <path-to-your-APK>. Now switch to emulator and launch your application like on any normal device, through the launcher. Or, as an alternative, you can use the following command: adb shell am start -a android.intent.action.MAIN -n <package>/<activity class>. For example: adb shell am start -a android.intent.action.MAIN -n org.sample.helloworld/org.sample.helloworld.HelloWorld. As a commenter suggested, you can also replace org.sample.helloworld.HelloWorld in the line above with just .HelloWorld, and it will work too.

打开CMD

打开命令提示符 在我的例子中输入模拟器的路径

C: \ adt - bundle - windows - x86_64 - 20140702 \ \ sdk工具

在我的例子中写“emulator -avd emulatorname”

-avd阿迪vd模拟器

在这里补充一下,每当你得到“错误:设备离线”意味着与模拟器和adb桥接器的连接由于模拟器启动所花费的时间而中断。

而不是重新启动模拟器在这一点上,尝试以下两个命令停止&启动adb桥再次。

亚行kill-server 亚洲开发银行启动服务器

打开命令提示符,进入<android-Home>\sdk\tools>emulator -avd <AVD_NAME>

这里“emulator”是用来打开Android虚拟设备的命令。