Android Exception: Build failed,unaligned.apks 拒绝访问
Question
Android Studio中,Build时报错,Execution failed for task ‘:app:packageDebug’,详见如下:
:app:mergeDebugJniLibFolders
:app:transformNative_libsWithMergeJniLibsForDebug
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug
:app:validateDebugSigning
:app:packageDebug FAILED
Error:Execution failed for task ':app:packageDebug'.
> Failed to create 'D:\...\app\build\outputs\apk\app-debug-unaligned.apks': 拒绝访问。
Information:BUILD FAILED
Information:Total time: 24.975 secs
Information:1 error
发生报错后,若直接去该目录下删除文件,发现无法删除,即便是用管理员权限也无法删除apks文件。
Solution
重启Windows操作系统。重启后,再次Build后成功。
另外,据说使用电脑管家等防护软件的文件粉碎功能,对apk文件夹进行粉碎,然后重新编译也同样有效。
Extension
What is an aligned apk
Aligned APKs are optimized for RAM usage so they will consume less RAM in the devices.
Zipalign is an archive alignment tool that provides important optimization to Android application (.apk) files.....
The benefit is a reduction in the amount of RAM consumed when running the application.
Why unaligned?
通过Gradle构建后,在app/build/apk文件夹下会先生成app-debug-unaligned.apks,然后再从unaligned APK变为aligned,并生成aligned APK。
The unaligned(signed) APK is needed because the signing an aligned apk will undo the alignment.
app-debug-unaligned.apk = Unaligned Signed APK
app-debug.apk = Aligned Signed APK (RAM optimized using zipalign)
APP的构建过程
官方文档解释:
Caution: zipalign must only be performed after the .apk file has been signed with your private key.
If you perform zipalign before signing, then the signing procedure will undo the alignment.
作者:Pierre_
来源链接:https://blog.csdn.net/pierre_/article/details/51132179