安装APK错误码-ADB

本文详细列举了Android系统中APK安装过程中可能遇到的各种错误码及其含义,从安装成功到各种失败情况,帮助开发者快速定位并解决问题。
安装APK的错误码,定义在Android源码中的这个文件中:
frameworks\base\core\Java\android\content\pm\PackageManager.java

参考链接 http://blog.youkuaiyun.com/gaojinshan/article/details/7954680

/**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} on success.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_SUCCEEDED = 1;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the package is already installed.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_ALREADY_EXISTS = -1;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the package archive file is invalid.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_INVALID_APK = -2;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the URI passed in is invalid.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_INVALID_URI = -3;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the package manager service found that
     * the device didn't have enough storage space to install the app.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_INSUFFICIENT_STORAGE = -4;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if a package is already installed with
     * the same name.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_DUPLICATE_PACKAGE = -5;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the requested shared user does not
     * exist.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_NO_SHARED_USER = -6;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if a previously installed package of the
     * same name has a different signature than the new package (and the old
     * package's data was not removed).
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_UPDATE_INCOMPATIBLE = -7;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package is requested a shared
     * user which is already installed on the device and does not have matching
     * signature.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_SHARED_USER_INCOMPATIBLE = -8;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package uses a shared library
     * that is not available.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_MISSING_SHARED_LIBRARY = -9;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package uses a shared library
     * that is not available.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_REPLACE_COULDNT_DELETE = -10;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package failed while
     * optimizing and validating its dex files, either because there was not
     * enough storage or the validation failed.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_DEXOPT = -11;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package failed because the
     * current SDK version is older than that required by the package.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_OLDER_SDK = -12;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package failed because it
     * contains a content provider with the same authority as a provider already
     * installed in the system.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_CONFLICTING_PROVIDER = -13;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package failed because the
     * current SDK version is newer than that required by the package.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_NEWER_SDK = -14;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package failed because it has
     * specified that it is a test-only package and the caller has not supplied
     * the {@link #INSTALL_ALLOW_TEST} flag.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_TEST_ONLY = -15;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the package being installed contains
     * native code, but none that is compatible with the device's CPU_ABI.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_CPU_ABI_INCOMPATIBLE = -16;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package uses a feature that is
     * not available.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_MISSING_FEATURE = -17;

    // ------ Errors related to sdcard
    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if a secure container mount point
     * couldn't be accessed on external media.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_CONTAINER_ERROR = -18;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package couldn't be installed
     * in the specified install location.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_INVALID_INSTALL_LOCATION = -19;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package couldn't be installed
     * in the specified install location because the media is not available.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_MEDIA_UNAVAILABLE = -20;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package couldn't be installed
     * because the verification timed out.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_VERIFICATION_TIMEOUT = -21;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package couldn't be installed
     * because the verification did not succeed.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_VERIFICATION_FAILURE = -22;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the package changed from what the
     * calling program expected.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_PACKAGE_CHANGED = -23;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package is assigned a
     * different UID than it previously held.
     *
     * @hide
     */
    public static final int INSTALL_FAILED_UID_CHANGED = -24;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the new package has an older version
     * code than the currently installed package.
     *
     * @hide
     */
    public static final int INSTALL_FAILED_VERSION_DOWNGRADE = -25;

    /**
     * Installation return code: this is passed to the
     * {@link IPackageInstallObserver} if the old package has target SDK high
     * enough to support runtime permission and the new package has target SDK
     * low enough to not support runtime permissions.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_PERMISSION_MODEL_DOWNGRADE = -26;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser was given a path that is
     * not a file, or does not end with the expected '.apk' extension.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_NOT_APK = -100;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser was unable to retrieve the
     * AndroidManifest.xml file.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_BAD_MANIFEST = -101;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser encountered an unexpected
     * exception.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION = -102;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser did not find any
     * certificates in the .apk.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_NO_CERTIFICATES = -103;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser found inconsistent
     * certificates on the files in the .apk.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES = -104;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser encountered a
     * CertificateEncodingException in one of the files in the .apk.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING = -105;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser encountered a bad or
     * missing package name in the manifest.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME = -106;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser encountered a bad shared
     * user id name in the manifest.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID = -107;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser encountered some structural
     * problem in the manifest.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_MANIFEST_MALFORMED = -108;

    /**
     * Installation parse return code: this is passed to the
     * {@link IPackageInstallObserver} if the parser did not find any actionable
     * tags (instrumentation or application) in the manifest.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_PARSE_FAILED_MANIFEST_EMPTY = -109;

    /**
     * Installation failed return code: this is passed to the
     * {@link IPackageInstallObserver} if the system failed to install the
     * package because of system issues.
     *
     * @hide
     */
    @SystemApi
    public static final int INSTALL_FAILED_INTERNAL_ERROR = -110;

    /**
     * Installation failed return code: this is passed to the
     * {@link IPackageInstallObserver} if the system failed to install the
     * package because the user is restricted from installing apps.
     *
     * @hide
     */
    public static final int INSTALL_FAILED_USER_RESTRICTED = -111;

    /**
     * Installation failed return code: this is passed to the
     * {@link IPackageInstallObserver} if the system failed to install the
     * package because it is attempting to define a permission that is already
     * defined by some existing package.
     * <p>
     * The package name of the app which has already defined the permission is
     * passed to a {@link PackageInstallObserver}, if any, as the
     * {@link #EXTRA_FAILURE_EXISTING_PACKAGE} string extra; and the name of the
     * permission being redefined is passed in the
     * {@link #EXTRA_FAILURE_EXISTING_PERMISSION} string extra.
     *
     * @hide
     */
    public static final int INSTALL_FAILED_DUPLICATE_PERMISSION = -112;

    /**
     * Installation failed return code: this is passed to the
     * {@link IPackageInstallObserver} if the system failed to install the
     * package because its packaged native code did not match any of the ABIs
     * supported by the system.
     *
     * @hide
     */
    public static final int INSTALL_FAILED_NO_MATCHING_ABIS = -113;

    /**
     * Internal return code for NativeLibraryHelper methods to indicate that the package
     * being processed did not contain any native code. This is placed here only so that
     * it can belong to the same value space as the other install failure codes.
     *
     * @hide
     */
    public static final int NO_NATIVE_LIBRARIES = -114;

    /** {@hide} */
    public static final int INSTALL_FAILED_ABORTED = -115;
<think> 我们讨论了如何在Gradle中执行adb安装APK指令。根据引用[2],开发者通过在gradle脚本中挂载任务,在app:assembleDebug任务完成后使用adb执行模拟点击操作。类似地,我们可以创建一个任务来执行adb安装APK。 以下是实现步骤: 1. **创建自定义任务**:定义一个Gradle任务,类型为`Exec`,用于执行shell命令。 2. **指定依赖关系**:让这个任务在构建任务(如`assembleDebug`)之后执行。 3. **配置adb命令**:在任务中配置adb安装APK的命令,指定APK路径。 ### 实现方法 #### 步骤1:定义安装任务 在app模块的`build.gradle`文件中添加以下任务: ```groovy android { // Android配置... } // 定义安装Debug APK的任务 task installDebugApk(type: Exec, dependsOn: 'assembleDebug') { group = 'Install' description = 'Install debug APK on connected device' // 指定adb命令 commandLine 'adb', 'install', '-r', "${project.buildDir}/outputs/apk/debug/app-debug.apk" } ``` #### 步骤2:执行任务 在终端运行: ```bash gradlew installDebugApk ``` 或通过Android Studio的Gradle面板找到该任务并执行。 ### 关键点说明 1. **任务类型**: - `Exec`类型允许执行外部命令 - `dependsOn: 'assembleDebug'`确保先构建APK安装[^2] 2. **APK路径**: - Debug包默认路径:`app/build/outputs/apk/debug/app-debug.apk` - Release包路径需相应调整为`release`目录下的APK[^3] 3. **adb参数**: - `-r`:覆盖安装(保留数据) - 如需安装到特定设备,添加`-s 设备序列号`参数 ### 完整示例(支持多设备) ```groovy task installOnDevice(type: Exec) { def apkPath = "${project.buildDir}/outputs/apk/debug/app-debug.apk" def deviceId = "emulator-5554" // 替换为目标设备ID commandLine 'adb', '-s', deviceId, 'install', '-r', apkPath } // 设置依赖:必须在assembleDebug之后运行 installOnDevice.dependsOn('assembleDebug') ``` ### 动态获取最新APK 对于使用变体名称(Flavor)的复杂项目: ```groovy afterEvaluate { android.applicationVariants.all { variant -> if (variant.buildType.name == "debug") { def installTask = tasks.create("install${variant.name.capitalize()}", Exec) { dependsOn variant.assemble commandLine 'adb', 'install', '-r', variant.outputs.first().outputFile } } } } ``` ### 注意事项 - **adb路径**:确保adb在系统PATH中或使用完整路径(如Windows:`"${android.sdkDirectory}/platform-tools/adb"`) - **设备连接**:执行前确保设备已连接(`adb devices`可查看) - **权限问题**:Linux/macOS系统可能需要为adb添加执行权限 §§相关问题§§ 1. 如何在Gradle中卸载已安装APK? 2. 如何通过Gradle命令清除应用数据后再安装? 3. Gradle执行adb命令时如何避免权限错误? </think> ### 在Gradle中执行adb安装APK的方法 #### 核心实现原理 在Gradle中执行adb命令的核心是通过**自定义Exec类型任务**实现,该任务能直接调用系统命令[^2]。adb安装命令的标准格式如下: ```bash adb install -r apk路径 ``` 其中 `-r` 参数表示覆盖安装(reinstall),适用于调试场景。 --- #### 完整实现步骤(在app模块的build.gradle中添加) ##### 1. 定义安装任务 ```groovy // 安装Debug APK的任务 task installDebugApk(type: Exec) { dependsOn 'assembleDebug' // 确保先构建APK安装[^3] group = 'custom' // 任务分组 // 获取Debug APK路径(适配AGP 4.0+) def apkDir = layout.buildDirectory.dir('outputs/apk/debug').get().asFile def apkFile = fileTree(apkDir).filter { it.name.endsWith('.apk') }.singleFile // 配置adb命令 commandLine 'adb', 'install', '-r', apkFile.path } ``` ##### 2. 执行方式 ```bash # 终端执行(Windows) gradlew installDebugApk # 终端执行(Mac/Linux) ./gradlew installDebugApk ``` ##### 3. 多设备支持(需指定设备ID) ```groovy task installOnDevice(type: Exec) { dependsOn 'assembleDebug' def apkFile = ... // 获取APK路径(同上) def deviceId = "emulator-5554" // 通过adb devices获取 commandLine 'adb', '-s', deviceId, 'install', '-r', apkFile.path } ``` --- ### 关键优化技巧 #### 1. 动态获取APK路径 ```groovy android.applicationVariants.all { variant -> if (variant.buildType.name == "debug") { def installTask = tasks.register("install${variant.name.capitalize()}", Exec) { dependsOn variant.assemble commandLine 'adb', 'install', '-r', variant.outputs.first().outputFile } } } ``` 此方法自动适配所有构建变体(避免路径硬编码)[^3]。 #### 2. 添加安装后操作 ```groovy installDebugApk.doLast { println "APK安装成功!" // 可添加启动Activity的命令 // commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example.app/.MainActivity' } ``` #### 3. 错误处理 ```groovy installDebugApk { ignoreExitValue true // 忽略非0退出码 doLast { if (execResult.exitValue != 0) { throw new GradleException("安装失败: ${standardOutput.toString()}") } } } ``` --- ### 环境要求 1. **ADB路径配置**: - 确保adb在系统PATH中(Android SDK的platform-tools目录)[^1] - 或指定完整路径: ```groovy commandLine "${android.sdkDirectory}/platform-tools/adb", 'install', ... ``` 2. **设备连接**: - 执行前确保设备已连接(`adb devices`可见设备) - 避免多条USB线同时连接导致的设备选择问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值