问题描述
我试图以编程方式安装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下载到路径名/ 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上的应用程序
- Install应用程序默默,与授予INSTALL_PACKAGES许可
您不能静默安装的应用程序,它不是出于显而易见的原因支持Android系统。应用程序的安装需要用户干预以继续。
您需要的应用程序,以有 android.permission.INSTALL_PACKAGES
许可。
Workarounds?
You need the app to have the android.permission.INSTALL_PACKAGES
permission.
有关于如何,如果你有一定的priveleges做到这一点,尽管它可能很难得到你的应用程序,以这种方式运行这些线程一些提示。您可能需要安装一个特殊的目录,并且/或者您可能需要为特殊用户(这可能是很难做到的)运行。
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).
运行具有提升的权限的应用程序的一种可能方式:How我可以通过Android SDK获得root权限?
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.
这篇关于以编程方式安装一个Android APK作为一个测试框架的组成部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!