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

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

编辑:

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


当前回答

使用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'来启动模拟器。

对于一键式(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

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

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

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

回头谢谢我……

如果你有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模拟器启动!