Android上INSTALL_FAILED_INSUFFICIENT_STORAGE错误的解决方法 [英] Solution to INSTALL_FAILED_INSUFFICIENT_STORAGE error on Android
问题描述
INSTALL_FAILED_INSUFFICIENT_STORAGE
错误是每个 Android 开发者生活的祸根.无论应用程序大小或有多少可用存储空间,它都会发生.重新启动目标设备可以暂时解决问题,但很快又会出现.有数百个(如果不是数千个)留言板帖子来自人们询问问题发生的原因,但 Google 的人对这个问题却令人沮丧地保持沉默.
有一个简单的解决方法.如果您的测试设备运行的是 Android 2.2 或更高版本,则将 android:installLocation
属性添加到您的应用程序的清单文件中,值为 "preferExternal"
.这将强制将应用安装在设备的外部存储设备上,例如手机的 SD 卡.
例如:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"包=com.andrewsmith.android.darkness"android:installLocation="preferExternal"
这与其说是解决办法,不如说是创可贴,如果您希望将完成的应用程序安装在设备的内存中,这可能并不理想.但这至少会让开发过程不那么令人沮丧.
这只是一个临时解决方法,并不是真正的解决方法.
在这件事发生在我身上并且对目前的反应不满意后,我开始工作试图从 AOSP 源中找出它.我找到了一个真正的解决方案.
说明
首先,关于 Android 安装的一些(简化)背景和更新
<块引用>首次安装应用程序:
APK 文件保存为
/data/app/
-1.apk (1.apk)
应用何时更新:
更新后的 APK 文件保存为:
/data/app/
-2.apk (2.apk) 第一个版本 (1.apk) 被删除.
关于我们的下一次更新:
- 新的 APK 保存为 (1.apk) 并删除 (2.apk)(永远重复).
我们大多数人遇到的问题发生在应用程序更新时,但删除旧 APK 失败.这本身还不会导致更新失败,但会导致 /data/app
中有两个 APK 文件.
下次您尝试更新应用程序时,系统无法移动其临时文件,因为 (1.apk) 和 (2.apk) 都不是空的.由于 File#renameTo(File) 不会抛出异常而是返回一个布尔值 PackageManager,因此它无法说明为什么它返回 INSTALL_FAILED_INSUFFICIENT_STORAGE 即使失败与可用空间量无关.>
解决方案
运行:
adb shell "pm uninstall "adb shell "rm -rf/data/app/-*"
或
卸载应用
使用您喜欢的方法删除BOTH:
<块引用>/data/app/
/data/app/
确保没有其他任何东西以类似的方式阻止未来的安装.就我而言,我有一个 /data/app-lib/
目录徘徊!在这种情况下,安装到 SD 卡 成功了,随后也移动到了内部存储器.(创建 /data/app-lib/
而没有 -1
结尾.)
为什么其他解决方案"有效
安装到外部存储的代码明显不同,没有相同的问题
卸载应用只会删除
/data/app
中的 APK 文件的一个版本.这就是为什么您可以重新安装它一次,但不能更新它.当这个错误发生时,模拟器中的可用空间量并不重要
The INSTALL_FAILED_INSUFFICIENT_STORAGE
error is the bane of every Android developer's life. It happens regardless of app size, or how much storage is available. Rebooting the target device fixes the problem briefly, but it soon comes back. There are hundreds (if not thousands) of message board posts from people asking why the problem occurs, but the folks at Google are frustratingly silent on the issue.
There is a simple workaround. If your test device is running Android 2.2 or later then add the android:installLocation
attribute to your application's manifest file, with the value "preferExternal"
. This will force the app to be installed on the device's external storage, such as a phone's SD card.
For example:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andrewsmith.android.darkness"
android:installLocation="preferExternal"
This is more of a band-aid than a fix, and it may not be ideal if you want your finished app to install on the device's internal memory. But it will at least make the development process a lot less frustrating.
This is only a temporary workaround and not a real fix.
After having this happen to me and not being pleased with the current responses I went to work trying to figure it out from the AOSP source. I have found a REAL solution.
Explanation
First off, a bit of (simplified) background on how Android installs and updates
The first time an app is installed:
The APK file is saved as
/data/app/<full.package.name>-1.apk (1.apk)
When the app is to be updated:
The updated APK file is saved as:
/data/app/<full.package.name>-2.apk (2.apk)
The first version (1.apk) gets deleted.
On our next update(s):
- The new APK is saved as (1.apk) and (2.apk) is deleted (Repeat forever).
The issue that most of us are having happens when the application is updated, but deleting of the old APK fails. Which itself does not yet cause the update to fail, but it does cause there to be two APK files in /data/app
.
The next time you try to update the app the system can't move its temporary file because neither (1.apk) nor (2.apk) are empty. Since File#renameTo(File) doesn't throw an exception but instead returns a boolean PackageManager, it doesn't have any way to tell why it returns INSTALL_FAILED_INSUFFICIENT_STORAGE even though the failure had nothing to do with the amount of free space.
Solution
Run:
adb shell "pm uninstall <full.packge.name>"
adb shell "rm -rf /data/app/<full.package.name>-*"
OR
Uninstall the app
Use your favorite method to delete BOTH:
/data/app/<full.package.name>-1.apk
/data/app/<full.package.name>-2.apk
Make sure nothing else blocks future installs in a similar way. In my case I had a /data/app-lib/<full.package.name>-1
directory lingering around! In this case, an install to the SD card worked, and a subsequent move to internal memory, too. (Creating /data/app-lib/<full.package.name>
without the -1
ending.)
Why other "solutions" worked
The code for installing to external storage is significantly different which does not have the same problems
Uninstalling the app only deletes one version of the APK file in
/data/app
. That's why you can reinstall it once, but not update it.The amount of free space in an emulator isn't really relevant when this bug occurs
这篇关于Android上INSTALL_FAILED_INSUFFICIENT_STORAGE错误的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!