如何使用 .apk 文件运行(不仅仅是安装)Android 应用程序?

发布于 2024-11-09 16:29:50 字数 335 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

匿名的好友 2024-11-16 16:29:50

您无法一次性安装并运行 - 但您当然可以使用 adb 来启动已安装的应用程序。使用 adb shell am start 来触发意图 - 不过,您需要为您的应用程序使用正确的意图。几个示例:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings 

将启动“设置”,并将

adb shell am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity 

启动浏览器。
如果您想将浏览器指向特定页面,请执行此操作。

adb shell am start -a android.intent.action.VIEW -n com.android.browser/.BrowserActivity http://www.google.co.uk

如果您不知道 APK 中活动的名称,请执行此操作,

aapt d xmltree <path to apk> AndroidManifest.xml

输出内容将包括如下部分:

   E: activity (line=32)
    A: android:theme(0x01010000)=@0x7f080000
    A: android:label(0x01010001)=@0x7f070000
    A: android:name(0x01010003)="com.anonymous.MainWindow"
    A: android:launchMode(0x0101001d)=(type 0x10)0x3
    A: android:screenOrientation(0x0101001e)=(type 0x10)0x1
    A: android:configChanges(0x0101001f)=(type 0x11)0x80
    E: intent-filter (line=33)
      E: action (line=34)
        A: android:name(0x01010003)="android.intent.action.MAIN"
        XE: (line=34)

That Tells you the name of the main活动(主窗口),您现在可以运行

adb shell am start -a android.intent.action.MAIN -n com.anonymous/.MainWindow

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:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings 

will launch Settings, and

adb shell am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity 

will launch the Browser.
If you want to point the Browser at a particular page, do this

adb shell am start -a android.intent.action.VIEW -n com.android.browser/.BrowserActivity http://www.google.co.uk

If you don't know the name of the activities in the APK, then do this

aapt d xmltree <path to apk> AndroidManifest.xml

the output content will includes a section like this:

   E: activity (line=32)
    A: android:theme(0x01010000)=@0x7f080000
    A: android:label(0x01010001)=@0x7f070000
    A: android:name(0x01010003)="com.anonymous.MainWindow"
    A: android:launchMode(0x0101001d)=(type 0x10)0x3
    A: android:screenOrientation(0x0101001e)=(type 0x10)0x1
    A: android:configChanges(0x0101001f)=(type 0x11)0x80
    E: intent-filter (line=33)
      E: action (line=34)
        A: android:name(0x01010003)="android.intent.action.MAIN"
        XE: (line=34)

That tells you the name of the main activity (MainWindow), and you can now run

adb shell am start -a android.intent.action.MAIN -n com.anonymous/.MainWindow
梅窗月明清似水 2024-11-16 16:29:50

如果您正在寻找“adb run myapp.apk”的等效项,

您可以使用此 答案

(仅限 Linux 和 Mac - 也许在 Windows 上使用 cygwin)

Linux/Mac 用户还可以创建一个脚本来运行 apk,如下所示:

创建一个名为的文件“adb-run.sh”包含以下 3 行:

pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
act=$(aapt dump badging $1|awk -F" " '/launchable-activity/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell am start -n $pkg/$act

然后“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:

pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
act=$(aapt dump badging $1|awk -F" " '/launchable-activity/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell am start -n $pkg/$act

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

十级心震 2024-11-16 16:29:50

这是shell脚本中的解决方案:

apk="$apk_path"

1.安装apk

adb install "$apk"
sleep 1

2.获取包名

pkg_info=`aapt dump badging "$apk" | head -1 | awk -F " " '{print $2}'`
eval $pkg_info > /dev/null

3.启动应用程序

pkg_name=$name
adb shell monkey -p "${pkg_name}" -c android.intent.category.LAUNCHER 1

This is a solution in shell script:

apk="$apk_path"

1. Install apk

adb install "$apk"
sleep 1

2. Get package name

pkg_info=`aapt dump badging "$apk" | head -1 | awk -F " " '{print $2}'`
eval $pkg_info > /dev/null

3. Start app

pkg_name=$name
adb shell monkey -p "${pkg_name}" -c android.intent.category.LAUNCHER 1
归属感 2024-11-16 16:29:50

当您从 GUI 启动应用程序时,adb logcat 可能会向您显示相应的操作/类别/组件:

$ adb logcat
[...]
I/ActivityManager( 1607): 从 pid 1792 开始 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.browser/.BrowserActivity}
[...]

When you start the app from the GUI, adb logcat might show you the corresponding action/category/component:

$ adb logcat
[...]
I/ActivityManager( 1607): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.browser/.BrowserActivity} from pid 1792
[...]

Spring初心 2024-11-16 16:29:50

首先安装您的应用程序:

adb install -r path\ProjectName.apk

-r 的好处是即使尚未安装它也可以工作。

要启动 MainActivity,您可以像这样启动它:

adb shell am start -n com.other.ProjectName/.MainActivity

First to install your app:

adb install -r path\ProjectName.apk

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

半衾梦 2024-11-16 16:29:50

我将其放入我的 makefile 中,就在 adb install ... 之后的下一行,

adb shell monkey -p `cat .identifier` -c android.intent.category.LAUNCHER 1

要使其正常工作,必须有一个 .identifier 文件,其中包含应用程序的包标识符,例如 com.company .ourfirstapp

无需寻找活动名称。

I put this in my makefile, right the next line after adb install ...

adb shell monkey -p `cat .identifier` -c android.intent.category.LAUNCHER 1

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.

開玄 2024-11-16 16:29:50

我创建了终端别名来使用单个命令安装和运行 apk。

// I use ZSH, here is what I added to my .zshrc file (config file)
// at ~/.zshrc
// If you use bash shell, append it to ~/.bashrc

# Have the adb accessible, by including it in the PATH
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:path/to/android_sdk/platform-tools/"

# Setup your Android SDK path in ANDROID_HOME variable
export ANDROID_HOME=~/sdks/android_sdk

# Setup aapt tool so it accessible using a single command
alias aapt="$ANDROID_HOME/build-tools/27.0.3/aapt"

# Install APK to device
# Use as: apkinstall app-debug.apk
alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X install -r $1"
# As an alternative to apkinstall, you can also do just ./gradlew installDebug

# Alias for building and installing the apk to connected device
# Run at the root of your project
# $ buildAndInstallApk
alias buildAndInstallApk='./gradlew assembleDebug && apkinstall ./app/build/outputs/apk/debug/app-debug.apk'

# Launch your debug apk on your connected device
# Execute at the root of your android project
# Usage: launchDebugApk
alias launchDebugApk="adb shell monkey -p `aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name' | cut -d \' -f 2` 1"

# ------------- Single command to build+install+launch apk------------#
# Execute at the root of your android project
# Use as: buildInstallLaunchDebugApk
alias buildInstallLaunchDebugApk="buildAndInstallApk && launchDebugApk"

注意:这里我正在构建、安装和启动调试 apk,它通常位于路径:./app/build/outputs/apk/debug/app-debug.apk,当命令从项目的根目录执行

如果您想安装并运行任何其他 apk,只需将调试 apk 的路径替换为您自己的 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.

// I use ZSH, here is what I added to my .zshrc file (config file)
// at ~/.zshrc
// If you use bash shell, append it to ~/.bashrc

# Have the adb accessible, by including it in the PATH
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:path/to/android_sdk/platform-tools/"

# Setup your Android SDK path in ANDROID_HOME variable
export ANDROID_HOME=~/sdks/android_sdk

# Setup aapt tool so it accessible using a single command
alias aapt="$ANDROID_HOME/build-tools/27.0.3/aapt"

# Install APK to device
# Use as: apkinstall app-debug.apk
alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X install -r $1"
# As an alternative to apkinstall, you can also do just ./gradlew installDebug

# Alias for building and installing the apk to connected device
# Run at the root of your project
# $ buildAndInstallApk
alias buildAndInstallApk='./gradlew assembleDebug && apkinstall ./app/build/outputs/apk/debug/app-debug.apk'

# Launch your debug apk on your connected device
# Execute at the root of your android project
# Usage: launchDebugApk
alias launchDebugApk="adb shell monkey -p `aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name' | cut -d \' -f 2` 1"

# ------------- Single command to build+install+launch apk------------#
# Execute at the root of your android project
# Use as: buildInstallLaunchDebugApk
alias buildInstallLaunchDebugApk="buildAndInstallApk && launchDebugApk"

Note: Here I am building, installing and launching the debug apk which is usually in the path: ./app/build/outputs/apk/debug/app-debug.apk, when this command is executed from the root of the project

If you would like to install and run any other apk, simply replace the path for debug apk with path of your own apk

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 the aapt tool to extract the package name from the apk.

Next, is the command: adb shell monkey -p com.package.name 1, which basically uses the monkey tool to open up the default launcher activity of the installed app on the connected device. The part of com.package.name is replaced by our previous command which takes care of getting the package name from the apk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文