個人比較喜歡命令列操作,特別是在 android 開發的時候,方便!
在終端敲入命令,會出現關於adb工具的使用協助。
adb -help
安裝 apk,一般都是使用命令:
adb install **
在 adb -help 可以看到如下協助:
adb install [-l] [-r] [-s] <file> - push this package file to the device and install it ('-l' means forward-lock the app) ('-r' means reinstall the app, keeping its data) ('-s' means install on SD card instead of internal storage)
如果你的手機或者模擬器已經安裝了某個 apk,如 my.apk
那麼你沒有卸載 my.apk 的話,(命令列操作)再次安裝該 apk 的話,會包如下錯誤:
Failure [INSTALL_FAILED_ALREADY_EXISTS]
-r 參數表示重新安裝 apk,所以加上這個參數就不會有上述錯誤。
-s 參數表示安裝 apk 到 SDcard,好了。鬱悶的時刻到來!-l 參數什麼意思?
自己做了很多測試,也不是很明白。最後在 sdk api上找到答案。感謝:sdk-path/docs/guide/appendix/market-filters.html
看下面這張,也許回得到點啟發。大致意思是在發布 apk 到 android market上時,可以設定相關標誌位來保護你的 app。
那麼,再從 PackageManager.java 源碼中尋找一些蛛絲馬跡、、、、、、
該類是一個抽象類別,聲明如下:
/** * Class for retrieving various kinds of information related to the application * packages that are currently installed on the device. * * You can find this class through {@link Context#getPackageManager}. */public abstract class PackageManager
PackageManager 主要是用於獲得安裝在裝置上應用的各種資訊。
看一個常量和一個方法,定義如下:
/** * Flag parameter for {@link #installPackage(android.net.Uri, IPackageInstallObserver, int)} to * indicate that this package should be installed as forward locked, i.e. only the app itself * should have access to its code and non-resource assets. * @hide */ public static final int INSTALL_FORWARD_LOCK = 0x00000001;
/** * @hide * * Install a package. Since this may take a little while, the result will * be posted back to the given observer. An installation will fail if the calling context * lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the * package named in the package file's manifest is already installed, or if there's no space * available on the device. * * @param packageURI The location of the package file to install. This can be a 'file:' or a * 'content:' URI. * @param observer An observer callback to get notified when the package installation is * complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be * called when that happens. observer may be null to indicate that no callback is desired. * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK}, * {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}. * @param installerPackageName Optional package name of the application that is performing the * installation. This identifies which market the package came from. */ public abstract void installPackage( Uri packageURI, IPackageInstallObserver observer, int flags, String installerPackageName);
可以看出:
INSTALL_FORWARD_LOCK 常量主要是用來保護自己的 app,installPackage 方法的參數 int flags 可以是 INSTALL_FORWARD_LOCK。
注意:在 android1.5 源碼中,INSTALL_FORWARD_LOCK 常量是 ORWARD_LOCK_PACKAGE。
好了,目前為止,-l 參數是用來保護自己的 app,即 forward-locked(正向鎖定)!
不過,平時安裝測試 app,很少使用該參數!