我在Mac上,在终端上进行Android开发。我已经成功地创建了HelloWorld项目,现在我试图从Android模拟器中的命令行运行它。哪个命令运行HelloWorld项目的模拟器?
我已经有Android工具和平台工具在我的路径。
编辑:
如何告诉模拟器从命令行运行HelloWorld项目?我已经用ant构建了这个项目。
我在Mac上,在终端上进行Android开发。我已经成功地创建了HelloWorld项目,现在我试图从Android模拟器中的命令行运行它。哪个命令运行HelloWorld项目的模拟器?
我已经有Android工具和平台工具在我的路径。
编辑:
如何告诉模拟器从命令行运行HelloWorld项目?我已经用ant构建了这个项目。
当前回答
对于自动化(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.
其他回答
转到模拟器所在的目录
美元{ANDROID_SDK} -工具-模拟器
从CLI检查模拟器列表
模拟器-list-avds
从列表中提取AVD
-avd模拟器{avd_name}
在Mac(我认为还有Linux)上,创建AVD后,你可以创建一个别名: alias run-android='~/Library/Android/sdk/tools/emulator -avd ${YOUR_AVD_NAME} &'
注意:别名的执行不会锁定你的终端,如果你想这样做,只需删除最后一个'&'。
运行模拟器它自己会给你一个错误,因为他期望,在你当前的位置,你有:/emulator/qemu/${YOUR_PATFORM}/qemu-system-x86_64'来启动模拟器。
我迟到了,在这里,但想要分享,所以它可能会帮助别人和我以后需要的时候:), 下面是使用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
我假设你已经构建了你的项目,只需要启动它,但你没有创建任何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.
不建议使用android create avd命令。现在建议使用avdmanager来代替从命令行启动模拟器。
首先,如果模拟器不存在,创建一个新的模拟器:
avdmanager create avd --name "MyEmulator" -k "system-images;android-
26;google_apis;x86"
这里假设您已经安装了一个与API 26匹配的X86系统映像,并安装了谷歌API。
然后,您可以使用emulator @MyEmulator启动模拟器。