以编程方式安装 Android apk 作为测试框架的一部分

发布于 2024-12-20 06:31:17 字数 668 浏览 4 评论 0原文

我正在尝试以编程方式安装 apk,但运气不太好。我正在设置一个针对物理设备的自动化测试框架,并且我想让测试设备在运行测试之前从构建服务器检索最新的 apk。虽然我知道没有通用的方法可以在未经用户同意的情况下实际安装 apk,但我很好奇在开发人员同时拥有 apk 和设备的情况下是否可以使用某种方法。

我过去尝试过的方法(apk 已下载到 pathName/apkFilename):

String command = "adb install " + pathName + apkFilename;
Runtime.getRuntime().exec(command);

并且:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(pathName + apkFilename)), "application/vnd.android.package-archive");
getActivity().startActivity(intent);

虽然我无法使第一种方法发挥作用,但第二次尝试会创建一个系统对话框,要求用户确认安装(几乎已经完成,但还没有完全完成)。由于它是一个系统对话框,不幸的是,我无法使用 Robotium 进行确认。

I'm attempting to install an apk programmatically, but I'm not having much luck. I'm setting up an automated test framework targeting physical devices, and I want to have the test devices retrieve the latest apk from the build server before running tests. While I am aware that there is no general way to actually install an apk without the user's consent, I'm curious if there might be some approach available in the case where a developer owns both the apk and device.

Approaches I've tried in the past (the apk has been downloaded to pathName/apkFilename):

String command = "adb install " + pathName + apkFilename;
Runtime.getRuntime().exec(command);

And:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(pathName + apkFilename)), "application/vnd.android.package-archive");
getActivity().startActivity(intent);

While I haven't been able to get the first approach to work, the second attempt creates a system dialog asking the user to confirm the installation (so almost there, but not quite). Since it is a System dialog, I, unfortunately, cannot use Robotium to confirm.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深陷 2024-12-27 06:31:17

很多人都在尝试解决类似的问题。我相信在没有确认的情况下可能无法安装 APK,至少不容易:

我已经接受了一段时间,在 Android 上静默安装应用程序是不可能的

您无法静默安装应用程序,Android 出于显而易见的原因不支持该应用程序。应用程序安装需要用户干预才能继续。

解决方法?

您需要该应用拥有 android.permission.INSTALL_PACKAGES 权限。

如果您拥有某些特权,这些线程上有一些关于如何执行此操作的提示,尽管可能很难让您的应用程序以这种方式运行。您可能必须安装到特殊目录,和/或可能必须以特殊用户身份运行(这可能很难做到)。

以提升的权限运行应用程序的一种可能方法:如何我可以通过 Android SDK 获得 root 权限吗?

在此线程中,他们提到您可能必须“root”您的手机才能启用该权限:

我不会如果这使保修失效,我会感到惊讶。您在帖子的评论中提到您没有“对设备的控制权”,因此这也可能会终止此选项。

一些提及一些应用程序使用的漏洞利用线程,但我不认为他们受到支持。如果他们仍然工作,他们可能会在某个时候停止工作。

A lot of people are trying to solve similar problems. I believe it may not be possible to install an APK without confirmation, at least not easily:

I've accepted for a while now that it's impossible to silently install an application on Android

You cannot silently install app, its not supported by Android for obvious reasons. Application installation requires user intervention to continue.

Workarounds?

You need the app to have the android.permission.INSTALL_PACKAGES permission.

There are some hints on those threads about how to do this if you have certain priveleges, though it might be hard to get your app to run that way. You might have to install to a special directory, and/or you might have to run as a special user (which might be hard to do).

One possible way to run the app with elevated permissions: How can I get root permissions through the Android SDK?

On this thread, they mention you might have to "root" your phone to enable that permission:

I wouldn't be surprised if this voids the warranty though. You mentioned in the comments on your post that you don't have "control over the device", so that might kill this option too.

There is some mention on this thread of exploits that some apps use, but I don't think they're supported. If they still work, they might stop working at some point.

往日情怀 2024-12-27 06:31:17

我正在尝试做同样的事情,以便将更新推送到我们控制的设备。在我们的例子中,它们已经获得 root 权限,并且应用程序已被授予超级用户,因此我认为仅将 .apk 复制到现有文件的顶部可能会起作用,但这看起来非常 hacky。

似乎更好的方法(如果有效的话)是使用 pm Package Manager 应用程序:

# /system/bin/pm
usage: pm [list|path|install|uninstall]
       pm list packages [-f]
       pm list permission-groups
       pm list permissions [-g] [-f] [-d] [-u] [GROUP]
       pm list instrumentation [-f] [TARGET-PACKAGE]
       pm list features
       pm path PACKAGE
       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH
       pm uninstall [-k] PACKAGE
       pm enable PACKAGE_OR_COMPONENT
       pm disable PACKAGE_OR_COMPONENT
       pm setInstallLocation [0/auto] [1/internal] [2/external]

The list packages command prints all packages.  Options:
  -f: see their associated file.

The list permission-groups command prints all known
permission groups.

The list permissions command prints all known
permissions, optionally only those in GROUP.  Options:
  -g: organize by group.
  -f: print all information.
  -s: short summary.
  -d: only list dangerous permissions.
  -u: list only the permissions users will see.

The list instrumentation command prints all instrumentations,
or only those that target a specified package.  Options:
  -f: see their associated file.

The list features command prints all features of the system.

The path command prints the path to the .apk of a package.

The install command installs a package to the system.  Options:
      -l: install the package with FORWARD_LOCK.
  -r: reinstall an exisiting app, keeping its data.
  -t: allow test .apks to be installed.
  -i: specify the installer package name.
  -s: install package on sdcard.
  -f: install package on internal flash.

The uninstall command removes a package from the system. Options:
  -k: keep the data and cache directories around.
after the package removal.

The enable and disable commands change the enabled state of
a given package or component (written as "package/class").

The getInstallLocation command gets the current install location
  0 [auto]: Let system decide the best location
  1 [internal]: Install on internal device storage
  2 [external]: Install on external media

The setInstallLocation command changes the default install location
  0 [auto]: Let system decide the best location
  1 [internal]: Install on internal device storage
  2 [external]: Install on external media

I'm trying to do the same thing, in order to push updates to devices we control. In our case they're already rooted, and the application has been granted superuser, so I think just copying the .apk over the top of the existing file would probably work, but this seems very hacky.

What seems like a better approach (if it works) is to use the pm Package Manager application:

# /system/bin/pm
usage: pm [list|path|install|uninstall]
       pm list packages [-f]
       pm list permission-groups
       pm list permissions [-g] [-f] [-d] [-u] [GROUP]
       pm list instrumentation [-f] [TARGET-PACKAGE]
       pm list features
       pm path PACKAGE
       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH
       pm uninstall [-k] PACKAGE
       pm enable PACKAGE_OR_COMPONENT
       pm disable PACKAGE_OR_COMPONENT
       pm setInstallLocation [0/auto] [1/internal] [2/external]

The list packages command prints all packages.  Options:
  -f: see their associated file.

The list permission-groups command prints all known
permission groups.

The list permissions command prints all known
permissions, optionally only those in GROUP.  Options:
  -g: organize by group.
  -f: print all information.
  -s: short summary.
  -d: only list dangerous permissions.
  -u: list only the permissions users will see.

The list instrumentation command prints all instrumentations,
or only those that target a specified package.  Options:
  -f: see their associated file.

The list features command prints all features of the system.

The path command prints the path to the .apk of a package.

The install command installs a package to the system.  Options:
      -l: install the package with FORWARD_LOCK.
  -r: reinstall an exisiting app, keeping its data.
  -t: allow test .apks to be installed.
  -i: specify the installer package name.
  -s: install package on sdcard.
  -f: install package on internal flash.

The uninstall command removes a package from the system. Options:
  -k: keep the data and cache directories around.
after the package removal.

The enable and disable commands change the enabled state of
a given package or component (written as "package/class").

The getInstallLocation command gets the current install location
  0 [auto]: Let system decide the best location
  1 [internal]: Install on internal device storage
  2 [external]: Install on external media

The setInstallLocation command changes the default install location
  0 [auto]: Let system decide the best location
  1 [internal]: Install on internal device storage
  2 [external]: Install on external media
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文