以编程方式安装 Android apk 作为测试框架的一部分 [英] Installing an Android apk programmatically as part of a test framework

查看:20
本文介绍了以编程方式安装 Android apk 作为测试框架的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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);

还有:

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

虽然我无法让第一种方法正常工作,但第二次尝试会创建一个系统对话框,要求用户确认安装(几乎就在那里,但并不完全).由于它是一个系统对话框,很遗憾,我无法使用 Robotium 进行确认.

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.

推荐答案

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

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:

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

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

  • 静默安装应用,并授予 INSTALL_PACKAGES 权限
  • 您不能静默安装应用程序,Android 不支持它,原因很明显.应用程序安装需要用户干预才能继续.

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

    解决方法?

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

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

    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).

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

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

    在此线程上,他们提到您可能必须root";您的手机以启用该权限:

    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.

    这篇关于以编程方式安装 Android apk 作为测试框架的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆