[英]Installing an Android apk programmatically as part of a test framework
我正在嘗試以編程方式安裝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進行確認。
很多人都試圖解決類似的問題。 我相信可能無法在沒有確認的情況下安裝APK,至少不容易:
我已經接受了一段時間,現在無法在Android上靜默安裝應用程序
您無法安靜地安裝應用程序,Android不支持它,原因很明顯。 應用程序安裝需要用戶干預才能繼續
您需要該應用程序具有android.permission.INSTALL_PACKAGES
權限。
如果您有某些權限,那么這些線程上有一些關於如何執行此操作的提示,盡管可能很難讓您的應用程序以這種方式運行。 您可能必須安裝到特殊目錄,和/或您可能必須以特殊用戶身份運行(這可能很難)。
使用提升權限運行應用程序的一種可能方法: 如何通過Android SDK獲得root權限?
在這個帖子中,他們提到你可能必須“root”手機以啟用該權限:
如果這會使保修失效,我不會感到驚訝。 您在帖子的評論中提到您沒有“控制設備”,因此也可能會殺死此選項。
在一些應用程序使用的漏洞利用程序中有一些提及 ,但我認為它們不受支持。 如果他們仍然有效,他們可能會在某個時候停止工作。
我正在嘗試做同樣的事情,以推動我們控制的設備的更新。 在我們的例子中,它們已經被root了,並且應用程序已被授予超級用戶,所以我認為只是將.apk復制到現有文件的頂部可能會有效,但這看起來非常糟糕。
看起來更好的方法(如果可行)是使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.