如何使用 .apk 文件运行(不仅仅是安装)Android 应用程序?
cmd.exe
上是否有任何命令允许我使用该应用程序的 .apk
文件启动特定 Android 应用程序的主要活动。请注意,我知道这个命令只安装一个 android 应用程序:
adb install myapp.apk
这个命令只会将 myapp
安装到模拟器上,我必须从模拟器手动运行这个应用程序(通过单击其图标) 。
我想要做的是使用一个命令,该命令不仅安装应用程序,还启动其主要活动(请注意,我只有它的 .apk
文件,所以我不知道包名称或任何活动名称都是)。
Is there any command on cmd.exe
that would allow me to start the main activity of a particular android application using the .apk
file of that application. Please note that I know this command which only installs an android application:
adb install myapp.apk
This command will only install myapp
onto the emulator and I have to manually run this application from the emulator (by performing single click on its icon).
What I want to do is use a command which not only installs the application but also starts its main activity (please note that I have only its .apk
file so I don't know what the package name or any activity name is).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您无法一次性安装并运行 - 但您当然可以使用 adb 来启动已安装的应用程序。使用 adb shell am start 来触发意图 - 不过,您需要为您的应用程序使用正确的意图。几个示例:
将启动“设置”,并将
启动浏览器。
如果您想将浏览器指向特定页面,请执行此操作。
如果您不知道 APK 中活动的名称,请执行此操作,
输出内容将包括如下部分:
That Tells you the name of the main活动(主窗口),您现在可以运行
You can't install and run in one go - but you can certainly use adb to start your already installed application. Use adb shell am start to fire an intent - you will need to use the correct intent for your application though. A couple of examples:
will launch Settings, and
will launch the Browser.
If you want to point the Browser at a particular page, do this
If you don't know the name of the activities in the APK, then do this
the output content will includes a section like this:
That tells you the name of the main activity (MainWindow), and you can now run
如果您正在寻找“adb run myapp.apk”的等效项,
您可以使用此 答案
(仅限 Linux 和 Mac - 也许在 Windows 上使用 cygwin)
Linux/Mac 用户还可以创建一个脚本来运行 apk,如下所示:
创建一个名为的文件“adb-run.sh”包含以下 3 行:
然后“chmod +x adb-run.sh”使其可执行。
现在您可以简单地:
adb-run.sh myapp.apk
这样做的好处是您不需要知道包名称或可启动的活动名称。同样,您可以创建“adb-uninstall.sh myapp.apk”
注意:这要求您的路径中有aapt。您可以在 SDK 中的新构建工具文件夹下找到它
if you're looking for the equivalent of "adb run myapp.apk"
you can use the script shown in this answer
(linux and mac only - maybe with cygwin on windows)
linux/mac users can also create a script to run an apk with something like the following:
create a file named "adb-run.sh" with these 3 lines:
then "chmod +x adb-run.sh" to make it executable.
now you can simply:
adb-run.sh myapp.apk
The benefit here is that you don't need to know the package name or launchable activity name. Similarly, you can create "adb-uninstall.sh myapp.apk"
Note: This requires that you have aapt in your path. You can find it under the new build tools folder in the SDK
这是shell脚本中的解决方案:
1.安装apk
2.获取包名
3.启动应用程序
This is a solution in shell script:
1. Install apk
2. Get package name
3. Start app
当您从 GUI 启动应用程序时,
adb logcat
可能会向您显示相应的操作/类别/组件:When you start the app from the GUI,
adb logcat
might show you the corresponding action/category/component:首先安装您的应用程序:
-r 的好处是即使尚未安装它也可以工作。
要启动 MainActivity,您可以像这样启动它:
adb shell am start -n com.other.ProjectName/.MainActivity
First to install your app:
The great thing about the -r is it works even if it wasn’t already installed.
To launch MainActivity, so you can launch it like:
adb shell am start -n com.other.ProjectName/.MainActivity
我将其放入我的 makefile 中,就在
adb install ...
之后的下一行,要使其正常工作,必须有一个 .identifier 文件,其中包含应用程序的包标识符,例如
com.company .ourfirstapp
无需寻找活动名称。
I put this in my makefile, right the next line after
adb install ...
For this to work there must be a .identifier file with the app's bundle identifier in it, like
com.company.ourfirstapp
No need to hunt activity name.
我创建了终端别名来使用单个命令安装和运行 apk。
这是 要点相同。
我创建这个是因为我在使用 Android Studio 构建时遇到问题,构建时间约为 28 分钟,因此我切换到终端构建,该构建时间约为 3 分钟。您可以在此处阅读有关此内容的更多信息< /a>
解释:
我认为需要解释的一个别名是
launchDebugApk
别名。下面是它的分解方式:
aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | 部分grep -e 'package: name
基本上使用aapt
工具从 apk 中提取包名称。接下来是命令:
adb shell Monkey -p com.package.name 1
,它基本上使用monkey
工具来打开已安装应用程序的默认启动器活动连接的设备。com.package.name
部分被我们之前的命令替换,该命令负责从 apk 中获取包名称。I created terminal aliases to install and run an apk using a single command.
Here is the gist for the same.
I created this because I was having trouble working with Android Studio build reaching around 28 minutes, so I switched over to terminal builds which were around 3 minutes. You can read more about this here
Explanation:
The one alias that I think needs explanation is the
launchDebugApk
alias.Here is how it is broken down:
The part
aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name
basically uses theaapt
tool to extract the package name from the apk.Next, is the command:
adb shell monkey -p com.package.name 1
, which basically uses themonkey
tool to open up the default launcher activity of the installed app on the connected device. The part ofcom.package.name
is replaced by our previous command which takes care of getting the package name from the apk.