据我所知,(仍然)不可能在现成的 Android 设备上进行完全无人值守的应用程序更新,因此我研究了 root 设备的可能性。经过一天的搜索、阅读和实验,我们找到了一种可行的方法,它为我们提供了所需的所有远程控制功能以及更多功能。所涉及的步骤和设置的粗略概述可能会帮助其他有类似需求的人:
- root 设备(具体情况因设备而异,因此您必须搜索如何为您的特定设备执行此操作)
- 配置(或重新配置,取决于生根过程的效果)设备设置(例如,wifi)以使其发挥最低限度的功能
- 进入Google设置>安全并取消选中“改进有害应用程序检测”和“扫描设备是否存在安全威胁”选项(否则,远程安装应用程序时手机将需要屏幕确认)
- 确保开发者选项已启用;我读到该位置可能因 Android 版本或设备制造商而异,但一个常见的位置是“设置”>“电话”>(滚动到底部)内部版本号 - 在“内部版本号”项目上点击 7 次以启用“开发人员选项”
- 在“设置”>“开发者选项”中启用 USB 调试(即使我们永远不会与手机建立 USB 连接,而是通过 TCP 进行所有调试;无论是通过 USB 还是 TCP,此选项的行为似乎更像“启用 adb 调试”)
- 安装 ssh 服务器或 sshd 应用程序(有很多;我选择了SSH服务器 https://play.google.com/store/apps/details?id=com.icecoldapps.sshserver现在并配置它(为该服务器添加 1 个服务器和 1 个用户)
- 从计算机或其他设备上的终端窗口,使用您在 SSH 服务器中设置的服务器所使用的任何端口通过 ssh 连接到手机:
$ ssh 192.168.1.217 -p 34567 # e.g., a local network IP address
- 通过发出以下命令为 adb over TCP 配置设备(这样做,无需先通过 USB 连接):
$ su
$ setprop service.adb.tcp.port 5555
$ stop adbd
$ start adbd
$ exit
Note that the first time you issue the su command, the device
will prompt for an on-screen tap to permit the SSH Server
app to run with elevated privileges -- this is a one-time
configuration that you must perform before you deploy to the field!
Note also that you specify which port adb will use -- you could
use something other than the default 5555.
- 或者,将 adb 授权密钥的文本文件放置到设备上(/data/misc/adb/adb_keys - 需要 su),以预授权您希望能够连接和管理远程设备的一台或多台计算机 - - 请参阅下面的进一步讨论
- 从 Android Studio 的终端选项卡(或从安装了 adb 的任何终端),使用 adb 连接到设备,最好使用设备的公共 IP 地址(更多信息见下文):
$ adb connect 93.184.216.34 # a public IP address
# or, if you aren't set up yet for public access, a local address
$ adb connect 192.168.1.217 # a local network IP address
Note that you will get another on-screen prompt, this time
to permit this computer to connect unless one of the
following is true:
a) you have previously connected to this device at this IP
address from this computer and you check the option to
permanently allow this connection, or
b) you created an adb_keys file containing the key for the
computer you are currently connecting from
If you get the prompt, you must confirm it on-screen with
a finger tap; if you select the option to permanently allow
this connection, the /data/misc/adb/adb_keys file will be
created and/or updated with the key for this connection
由于目标是配置手机,使其不需要任何屏幕上的用户交互,因此您需要确保使用 adb_keys 文件将其部署到现场,该文件包含远程连接时它将看到的密钥。最简单的方法是从您将用于远程管理的计算机上手动连接一次设备,并使用电话在部署时拥有的 IP 地址,因为通过本地网络时的密钥与通过本地网络时的密钥不同它是通过互联网进行的(我希望有比我更有知识的人来澄清所有这些 - 我只是报告我今天所看到/学到的所有这些)。
如果您预计从多台计算机管理手机,则应提前确定所有此类计算机的密钥,并将它们放入安装过程中安装的文件中 /data/misc/adb/adb_keys。这将“预授权”密钥与文件中的密钥匹配的所有计算机的 adb 连接。
设置就这样了。要使用此设置来实际管理应用程序更新非常简单,只需要 adb。以下是可用于管理远程设备的 adb 命令的最小列表 -- 输入 adb --help 或在网络上搜索有关可以使用 adb 执行的许多操作的更多信息(请注意,大多数但不是全部)这些命令是“adb shell”命令,适用于活动管理器(“adb shell am”)或包管理器(“adb shell pm”):
# terminate your currently-running app
$ adb shell am force-stop com.example.appname
# uninstall your app completely
$ adb shell pm uninstall com.example.appname
# or, to keep the app's data and cache use the -k option
$ adb shell pm uninstall -k com.example.appname
# install your app from your local computer to the remote device
$ adb install /local/path/to/the/app.apk
# start your app by invoking it's main/startup activity
$ adb shell am start -n com.example.appname.Main
我只对一部手机做过此操作;我将在未来几天内为更多手机重复此操作,并根据需要修改此答案。虽然每部手机的设置都有些复杂,并且需要花费几分钟的时间,但一旦配置完成,我们数百部手机的更新似乎可以作为 adb 命令的简单脚本完全自动化。