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

时间:2011-12-08 01:54:44

标签: android automated-tests robotium

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

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

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进行确认。

2 个答案:

答案 0 :(得分:2)

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

  

我已经接受了一段时间,现在无法在Android上静默安装应用程序

  

你无法安静地安装应用程序,Android不支持它,原因很明显。应用程序安装需要用户干预才能继续。

变通?

您需要该应用获得android.permission.INSTALL_PACKAGES权限。

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

使用提升权限运行应用的一种可能方式:How can I get root permissions through the Android SDK?

在这个帖子中,他们提到你可能必须“根”你的手机以启用该权限:

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

some mention on this thread of exploits that some apps use,但我认为它们不受支持。如果他们仍然有效,他们可能会在某个时候停止工作。

答案 1 :(得分:0)

我正在尝试做同样的事情,以推动我们控制的设备的更新。在我们的例子中,它们已经被root了,并且应用程序已被授予超级用户,所以我认为只是将.apk复制到现有文件的顶部可能会有效,但这看起来非常糟糕。

似乎更好的方法(如果可行)是使用pm程序包管理器应用程序:

# /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