个人比较喜欢命令行操作,特别是在 android 开发的时候,方便!
在终端敲入命令,会出现关于adb工具的使用帮助。
- adb -help
- adb install **
- 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,很少使用该参数!