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

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

编辑:

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


我假设你已经构建了你的项目,只需要启动它,但你没有创建任何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.

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

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

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

在这里,你可以通过命令行查看执行该命令的选项:

emulator -avd avd_name [-option [value]] ... [-qemu args]

例如,我是这样使用它的:

emulator -avd ICS_ARM -wipe-data -no-boot-anim -cpu-delay 0

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

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

如果您严格地试图从命令行运行模拟器,请在OSX中尝试此操作。

"/Applications/Android Studio.app/sdk/tools/emulator" -avd <NAMEOFDEVICE> -netspeed full -netdelay none

您可以通过向.bash_profile添加别名并将其发送到后台作业来简化它。

alias android='/Applications/Android\ Studio.app/sdk/tools/emulator <NAMEOFDEVICE> -netspeed full -netdelay none &'

让Bash知道这些变化。

source ~/.bash_profile

上可用的avd名称列表

android - avd。 SDK_PATH/工具/ emuls / netdelay no -netspeed full -avd “AVD_NAME”

(Linux) 在你的终端复制粘贴和改变三个蓝色的字段

/home/YouUser/Folder/adt-bundle-linux-x86_64-20140702/sdk/tools/emulator64-x86 -avd Android5.1.1

YouUser = user of session in linux
Folder = path of folfer
Android5.1.1 = You version of android in the emulator,for example (Android4.4.2)

你可以在你的文件夹home中查看模拟器 cd /home/Youuser/.android/avd/

列出所有模拟器:

emulator -list-avds

运行一个带-avd标志的模拟器:

emulator -avd name-of-your-emulator

模拟器可执行文件在下面:

${ANDROID_SDK}/tools/emulator

或者,对于更新的sdk,尝试:

cd ${ANDROID_HOME}/emulator
emulator -avd name-of-your-emulator

打开CMD

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

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

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

-avd阿迪vd模拟器

现在假设你已经安装了Android Studio(2.2)在我的情况下,只有一个模拟器,你可能会使用这一行

export ANDROID_SDK_ROOT=~/Library/Android/sdk/ && emulator '@'`emulator -list-avds`

如果你经常这样做,让它变得更容易:

$ echo 'export ANDROID_SDK_ROOT=~/Library/Android/sdk/' >> ~/.profile

向~.aliases添加别名

alias androidup="emulator '@'`emulator -list-avds`" 

召回到源~/。概要文件~ /。在测试之前使用别名

下次只需要$ androidup

我认为通过终端到达的最佳方式是:

cd ~/Library/Android/sdk/emulator

或者对于Windows:

cd %userProfile%\AppData\Local\Android\sdk\emulator

直接运行某个AVD:

./emulator -avd {AVD_NAME}

列出你的avd使用:

./emulator -list-avds

在windows中,我使用这个PowerShell脚本来启动它。

$em = $env:USERPROFILE+"\AppData\Local\Android\sdk\tools\emulator.exe"; 
Start-Process $em " -avd Nexus_5X_API_24" -WindowStyle Hidden;

不建议使用android create avd命令。现在建议使用avdmanager来代替从命令行启动模拟器。

首先,如果模拟器不存在,创建一个新的模拟器:

avdmanager create avd --name "MyEmulator" -k "system-images;android-
26;google_apis;x86"

这里假设您已经安装了一个与API 26匹配的X86系统映像,并安装了谷歌API。

然后,您可以使用emulator @MyEmulator启动模拟器。

使用dns帮助从命令行启动模拟器

模拟器程序位置:/Users/{{UserName}}/Library/Android/sdk/tools

检查现有avd:- 模拟器-list-avds 启动模拟器,设置dns /Users/{{UserName}}/Library/Android/sdk/tools/emulator -avd Pixel_API_26 -dns-server 8.8.8.8

在Mac(我认为还有Linux)上,创建AVD后,你可以创建一个别名: alias run-android='~/Library/Android/sdk/tools/emulator -avd ${YOUR_AVD_NAME} &'

注意:别名的执行不会锁定你的终端,如果你想这样做,只需删除最后一个'&'。

运行模拟器它自己会给你一个错误,因为他期望,在你当前的位置,你有:/emulator/qemu/${YOUR_PATFORM}/qemu-system-x86_64'来启动模拟器。

我使用Zenity编写了这个简单的shell脚本,允许您选择想要运行的avd。如果你没有定义ANDROID_HOME,你可以用模拟器的完整路径替换它。 使用select而不是Zenity也很容易做到这一点,但我选择了Zenity,因为我是从xfce-application菜单(虽然是.desktop-file)中运行它的。

#!/bin/sh

opt=$(zenity --title="Choose AVD" --text="Choose which emulator to start" --list \
                   --column="Devices" --separator="\n" `$ANDROID_HOME/emulator/emulator -list-avds`);

$ANDROID_HOME/emulator/emulator -avd $opt

如果你有Git Bash,你可以双击打开模拟器,进行一些初始设置(仅在Windows 10中测试):

Open Git Bash. Run cd ~/AppData/Local/Android/sdk/emulator Run ./emulator -list-avds and keep note of the emulator's name. MY_EMULATOR for example. Open Notepad and add these two lines (insert your emulator's name instead of MY_EMULATOR): cd ~/AppData/Local/Android/sdk/emulator; ./emulator -avd MY_EMULATOR Save the file somewhere convinient as myfile.sh. You can change "myfile" as suits you best, but the extension .sh is needed. Open Git Bash at the directory where you stored myfile.sh and run chmod +x myfile.sh. Remember to put your file's name instead of myfile.

就是这样!从现在开始,每次你需要启动模拟器,只要找到你的myfile.sh,双击它,等待你的Android模拟器启动!

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

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

打开CMD 获取所有模拟器列表(C:\Users\DJango\AppData\Local\Android\sdk\ Emulator > Emulator -list-avds) 选择任何模拟器 (C:\Users\DJango\AppData\Local\Android\sdk\emulator>emulator -avd Pixel_2_API_28)

我迟到了,在这里,但想要分享,所以它可能会帮助别人和我以后需要的时候:), 下面是使用bash脚本从命令行打开模拟器的方法。 我使用MX Linux,但在所有操作系统上的过程是相同的

1-首先检查已安装的仿真器

emulator -list-avds

结果如下所示

emulator -list-avds
Nexus_4_API_28
Pixel_2_API_28

2-打开任何纯文本或代码编辑器,创建一个新文件,并如下所示

#!/bin/sh
emulator -avd Nexus_4_API_28

Nexus_4_API_28是模拟器,我想打开你写你从第一步得到的

以.sh扩展名保存此文件

3-然后,更改文件的权限,使其可执行:

chmod u+x emu.sh

4-现在打开模拟器,用下面的命令执行这个bash脚本文件

./emu.sh

转到模拟器所在的目录

美元{ANDROID_SDK} -工具-模拟器

从CLI检查模拟器列表

模拟器-list-avds

从列表中提取AVD

-avd模拟器{avd_name}

对于一键式(BATCH文件)启动,这是我所做的:

从Android Studio -> Tools -> AVD Manager ->点击箭头向下,选择查看详细信息(对我来说是Pixel_2_API_28,所以下面相应地更改) 创建一个start.bat文件,并将以下内容放入其中:

c: cd C:\Program Files (x86)\Android\ android-sdk\emulator\ 模拟器-avd Pixel_2_API_28

解释:

第一行:对我来说是必要的,因为我从d启动它: 第二行:对我来说,没有使用工具\模拟器 第三行: 确保你用你的名字更改了AVD的名字 Pixel_2_API_28)

使用:

从命令行启动start.bat

在cmd中轻松地键入此命令。 替换Users\您的用户名\ 如果你没有这个文件重新安装android studio。

Mac使用zsh的说明:

打开终端窗口(CTRL+ALT+T) 运行命令 纳米~ / . zshrc 编辑您的个人资料 在打开的文件中添加如下行:

出口ANDROID_SDK_HOME = " ~ /图书馆/ Android Sdk”

别名模拟器=“ANDROID_SDK_HOME美元-模拟器,模拟器”

保存文件(CTRL+O, CTRL+X) 通过运行Source ~/命令获取概要文件的来源。或者注销再重新登录 运行以下命令进行测试:

模拟器-help或模拟器-list-avds 在终端中显示模拟器 运行Android模拟器:

模拟器-avd <name>

注意:将.zshrc替换为.bashrc对于bash也应该是一样的

一个启动程序脚本,列出现有的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应用程序:在菜单上选择“文件”,按住“选项”键,选择“另存为”,然后选择“文件格式:应用程序”。

列出所有已安装的设备:emulator -list-avds

这将列出安装在Android Studio中的所有设备。

运行模拟器:emulator -avd device_name

例如,如果你在运行选项1中的命令后得到了Pixel_3a_API_30_x86,那么你运行模拟器的命令将是:emulator -avd Pixel_3a_API_30_x86

Windows 10对我的帮助:

1 cd C:\Users\xxx\AppData\Local\Android\Sdk\emulator

列出所有可用的:emulator -list-avds

3启动NON-GUI模式:模拟器-avd Pixel_3a_API_30_x86 -no-window

对于自动化(CI),我们采取了如下步骤:

Frist, find SDK's tools location, and store in variable for later use. tools=$ANDROID_HOME/cmdline-tools/latest/bin arch="x86_64" package="system-images;android-23;google_apis;$arch" Ensure Android-image's downloaded. $tools/sdkmanager "$package" Accept the licenses. echo yes | $tools/sdkmanager --licenses Create AVD. echo no | $tools/avdmanager create avd --force --name MyAVD --abi google_apis/$arch --package "$package" Says "no" to "Do you wish to create a custom hardware profile? ..." Launch emulator parallel. $ANDROID_HOME/emulator/emulator -netdelay none -netspeed full -no-snapshot-load -no-snapshot -avd MyAVD > /dev/null & Build APK. ./gradlew assembleDebug At last, Android-tests (Instrumented tests) automatically detect and run on Emulator. ./gradlew connectedAndroidTest Note that because we launch Emulator parallel, you need to wait until Emulator is ready, and that before Step #7. Maybe count build-time with script, and if build takes less than what Emulator-launch takes, call sleep.

Windows:

在Windows搜索栏中搜索环境,选择“编辑系统环境变量” 在“系统属性”窗口中,单击“环境变量”并指定PATH环境变量的值:

C:\Users\{USER}\AppData\Local\Android\Sdk\emulator.

现在打开CMD或Powershell,输入: 模拟器-list-avds 这将显示所有avd的列表。 要运行特定的AVD,在CMD/Powershell中输入:emulator -avd {EmulatorName}

如果您添加了正确的android模拟器路径,AVD将打开。

如果你已经有模拟器,并想使用使用命令,只需使用这一行代码

cd  C:\Users\yourUserName\AppData\Local\Android\Sdk\emulator | ./emulator -avd Pixel_5_API_32

回头谢谢我……