android镜像
As an Android developer, I find myself mirroring my physical device onto my monitor quite often. Not only does it allow me to keep my hands on my mouse and keyboard without switching back-and-forth to my phone, but it also allows me to screen-share my physical Android device to coworkers when necessary. Outside of work, I’ve also been doing so more frequently as I can browse the internet while doing mindless farming in FGO or Azur Lane.
作为一名Android开发人员,我发现自己经常将物理设备镜像到显示器上。 它不仅可以让我将手放在鼠标和键盘上,而不必在手机上来回切换,而且还可以让我在必要时与同事进行屏幕共享。 在工作之外,我也经常这样做,因为我可以在FGO或Azur Lane进行盲目耕种时浏览互联网。
For mirroring, I use scrcpy, which allows for displaying and controlling Android devices connected via USB and does not require the device to be rooted. It’s similar to a popular program called Vysor, but in my opinion, it is superior since it is more flexible and is free without ads.
对于镜像,我使用scrcpy ,它可以显示和控制通过USB连接的Android设备,而无需植根该设备。 它类似于一个流行的程序Vysor,但在我看来,它具有优越性,因为它更灵活并且没有广告是免费的。
The one issue I’ve had lately is that I have been using scrcpy a lot. I’ve been looking for a way to automatically launch scrcpy for each device when I plug it in. I didn’t find anything immediately, so I decided to build something myself. Why spend 3 seconds doing something multiple times when I can spend 6 hours automating it? That being said, now that I’ve done the work I hope someone else can find it useful.
我最近遇到的一个问题是我经常使用scrcpy。 我一直在寻找一种在插入设备时自动为每个设备启动scrcpy的方法。我没有立即找到任何东西,因此我决定自己构建一些东西。 当我花6个小时将其自动化时,为什么要花3秒钟做多次事情? 话虽如此,现在我已经完成了工作,我希望其他人可以发现它有用。
auto-scrcpy.py (auto-scrcpy.py)
What I came up with was a python script that automatically starts scrcpy when a new Android device is plugged in. This script requires that python, adb and scrcpy are installed. I would definitely recommend making sure that you have regular scrcpy working normally before following this article.
我想到的是一个python脚本,当插入新的Android设备时,该脚本会自动启动scrcpy。此脚本要求安装python,adb和scrcpy。 我绝对建议您在阅读本文之前,确保您的常规scrcpy工作正常。
Originally, I had used a plain shell script but it didn’t give me the flexibility that I had wanted. For example, creating arrays and dictionaries in shell is hard but very easy in Python.
最初,我使用的是普通的Shell脚本,但没有给我想要的灵活性。 例如,在shell中创建数组和字典很困难,但在Python中非常容易。
The full script that I finally ended up with is as follows:
我最终得到的完整脚本如下:
This python script basically runs the command “adb track-devices” in order to be notified when the list of devices changes. When a change is detected, we gather the list of connected devices and then start scrcpy for each of them (if we have not already started scrcpy for that device). One other important aspect of this script is the CUSTOM_DEVICE_COMMANDS dictionary:
这个python脚本基本上运行命令“ adb track-devices”,以便在设备列表更改时得到通知。 检测到更改时,我们将收集已连接设备的列表,然后为每个设备启动scrcpy(如果尚未为该设备启动scrcpy)。 此脚本的另一个重要方面是CUSTOM_DEVICE_COMMANDS字典:
CUSTOM_DEVICE_COMMANDS = { "device1-serial" : "scrcpy --turn-screen-off --stay-awake", "device2-serial" : "scrcpy --window-x 10 --window-y 720"}
This dictionary lists specific scrcpy commands that should be run when given a certain device serial number. When the device with the given serial number is connected, it runs the custom scrcpy command rather than the default one, which gives the ability to do things such as always starting the device in the same screen location. This dictionary should be updated to suit your specific needs.
该词典列出了特定的设备序列号时应运行的特定scrcpy命令。 连接具有给定序列号的设备时,它将运行自定义scrcpy命令,而不是默认命令,从而可以执行诸如始终在同一屏幕位置启动设备的操作。 该字典应进行更新以适合您的特定需求。
To run auto-scrcpy, download the above script, make any requisite changes, and then just run it normally from terminal or the command line. On my Mac, I can simply run it using python in the following way.
要运行auto-scrcpy,请下载上述脚本,进行必要的更改,然后从终端或命令行正常运行它。 在Mac上,我可以通过以下方式使用python运行它。
python auto-scrcpy.py
If we make the file executable, we can also just do the following since we provide the shebang at the beginning of the file to specify that it should be run using the Python interpreter.
如果我们将该文件设为可执行文件,则也可以执行以下操作,因为我们在文件的开头提供了shebang ,以指定应使用Python解释器运行该文件。
./auto-scrcpy.py
After you’ve run the script, plugging in a device should automatically run scrcpy.
运行脚本后,插入设备应自动运行scrcpy。
让我们稍微懒惰(仅Mac) (Let’s be slightly lazier (Mac Only))
The auto-scrcpy.py script works fine but sometimes even starting up terminal can be kind of a hassle, right? Who wants to launch terminal on every machine restart just to run the same script every time?
auto-scrcpy.py脚本可以正常工作,但有时即使启动终端也可能有点麻烦,对吗? 谁想在每台机器重启时启动终端,以便每次都运行相同的脚本?
After a fair amount of reading, I found that I could use launchctl to install launch agents that would run my script on startup. Great! Doing this by hand was a bit of a hassle and I ran into tons of issues, even when following guides like this. I ended up using LaunchControl to help figure things out, but will do my best to explain!
经过大量的阅读之后,我发现我可以使用launchctl安装启动代理,该启动代理将在启动时运行我的脚本。 大! 做手工,这是一个有点麻烦,我跑进吨的问题,下面像导游,即使这个 。 我最终使用LaunchControl来帮助解决问题,但是将尽我所能解释!
Overall, the gist is that we need to create a plist file that will tell launchctl the details about our program that we want to run at launch. For me, the contents of the plist were as follows
总的来说,要点是我们需要创建一个plist文件,该文件将告诉launchctl我们要在启动时运行的程序的详细信息。 对我来说,plist的内容如下
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>EnvironmentVariables</key>
- <dict>
- <key>PATH</key>
- <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/jdvp/Library/Android/sdk/tools:/Users/jdvp/Library/Android/sdk/tools/bin/:/Users/jdvp/Library/Android/sdk/platform-tools</string>
- </dict>
- <key>Label</key>
- <string>auto-scrcpy</string>
- <key>Program</key>
- <string>/Users/jdvp/auto-scrcpy.py</string>
- <key>RunAtLoad</key>
- <true/>
- <key>StandardErrorPath</key>
- <string>/Users/jdvp/auto-scrcpy.log</string>
- <key>StandardOutPath</key>
- <string>/Users/jdvp/auto-scrcpy.log</string>
- </dict>
- </plist>
Starting from line 5, we have the EnvironmentVariables section. This section will be used by the LaunchAgent to set environment variables when starting your program. For me, the only thing that I needed to add was the PATH variable in order to make sure that both the adb and scrcpy commands can be found. You will likely need to edit Line 8 to match your actual PATH on your machine.
从第5行开始,我们有EnvironmentVariables部分。 启动程序时,LaunchAgent将使用此部分来设置环境变量。 对我来说,我唯一需要添加的就是PATH变量,以确保可以找到adb和scrcpy命令。 您可能需要编辑第8行以匹配计算机上的实际PATH。
Line 11 is the name of the program. This does not have to match the name of the python file that you have created.
第11行是程序的名称。 这不必与您创建的python文件的名称匹配。
Line 13 is the location of the program. This should point to the python file you have saved. This file should be executable. One issue I ran into here that I didn’t want to spend too much time solving is that when I had the script inside /Documents, Python was unable to run the file for some reason. I personally just moved the script into the base user directory instead.
第13行是程序的位置。 这应该指向您保存的python文件。 该文件应该是可执行的。 我在这里遇到的一个我不想花太多时间解决的问题是,当我在/ Documents中包含脚本时,Python由于某种原因无法运行该文件。 我个人只是将脚本移到了基本用户目录中。
Line 14 and 15 specifies that the job should launch as soon as we login.
第14和15行指定该作业应在我们登录后立即启动。
Line 16 through 19 specify the output path for logging. I am logging both stderr and stdout to the same file in my base user directory but feel free to change this to whatever suits you best.
第16至19行指定了日志记录的输出路径。 我将stderr和stdout都记录到我的基本用户目录中的同一文件中,但是可以随时将其更改为最适合您的内容。
This plist file should then be saved into the following directory
然后应将此plist文件保存到以下目录中
/Library/LaunchAgents
Note: This is not user/Library.
注意:这不是用户/库。
After you have placed the plist file, you can use launchctl to load the plist, which will trigger our script to start running and ensure that it runs on launch next time:
放置plist文件后,可以使用launchctl加载plist,这将触发我们的脚本开始运行,并确保它在下次启动时运行:
launchctl load /Library/LaunchAgents/auto-scrcpy.plist
then to verify it worked, either plug in a device to see if scrcpy automatically starts or run the following command (change auto-scrcpy to whatever you had updated the Label variable to in the auto-scrcpy.plist file):
然后要验证其是否有效,请插入设备以查看scrcpy是否自动启动,或运行以下命令(将auto-scrcpy更改为将auto-scrcpy.plist文件中的Label变量更新为的内容):
launchctl list | grep auto-scrcpy
which should should output something like:
应该输出如下内容:
4322 0 auto-scrcpy
The first column is the process id, the second column the status, and finally the process name. If the status is 0, the program should be running normally!
第一列是进程ID,第二列是状态,最后是进程名称。 如果状态为0,则程序应正常运行!
If you do have issues at this point, you will likely need to unload and load the program multiple times using launchctl and monitoring your log files. For me, the LaunchControl program I mentioned earlier helped immensely but there may be free alternatives I am unaware of.
如果此时确实存在问题,则可能需要使用launchctl并监视日志文件多次卸载和加载程序。 对我来说,我前面提到的LaunchControl程序提供了极大的帮助,但可能有我不知道的免费替代方案。
At this point, you should be able to plug in an Android device and automatically see scrcpy pop up! Remember, if you need to edit the auto-scrcpy.py file, you’ll need to re-load the plist to make sure changes take effect (unless you don’t mind waiting until the next login).
此时,您应该可以插入Android设备并自动看到scrcpy弹出了! 请记住,如果您需要编辑auto-scrcpy.py文件,则需要重新加载plist以确保更改生效(除非您不介意等待下一次登录)。
其他注意事项 (Other considerations)
After doing this project, I realized that the scrcpy README directly mentions another way of doing this using autoadb. I hadn’t seen this previously so I had continued on my merry way unaware. One benefit I see to using my approach rather than autoadb is that you don’t have to build the autoadb binary directly, which is a process many may be unfamiliar with. Further, using auto-scrcpy.py allows for each device to have a custom set of instructions specific to it.
完成该项目后,我意识到scrcpy README直接提到了使用autoadb进行此操作的另一种方法。 我以前没看过这个,所以我继续以自己快乐的方式不知所措。 我看到使用我的方法而不是autoadb的一个好处是您不必直接构建autoadb二进制文件,这是许多人可能不熟悉的过程。 此外,使用auto-scrcpy.py允许每个设备具有一组特定于其的自定义指令。
That being said, autoadb may be a better choice if you don’t need the flexibility of specific commands for each device and just want scrcpy to start. Further, if you want to do other adb commands for each device, it may be a better option.
话虽如此,如果您不需要为每个设备使用特定命令的灵活性而只想启动scrcpy,则autoadb可能是一个更好的选择。 此外,如果要为每个设备执行其他adb命令,则可能是一个更好的选择。
谢谢 (Thanks)
Thanks for sticking with me this far! I hope you were able to learn something or find this useful! If you have any questions or comments, please leave them below. I haven’t been able to test directly on Linux or Windows so if you do and it works without any changes, let me know!
感谢您到目前为止与我保持联系! 希望您能学到一些东西或觉得有用! 如果您有任何疑问或意见,请留在下面。 我还不能直接在Linux或Windows上进行测试,因此如果您可以进行测试并且可以在不做任何更改的情况下进行测试,请告诉我!
To keep up with what I’m working on, drop by my personal site!
为了跟上我的工作进度,请访问我的个人站点 !
翻译自: https://medium.com/@jdvp/auto-mirroring-android-devices-with-scrcpy-bdec44068887
android镜像