问题描述
我有一个非常大的apk文件,我正在努力减小它的大小. 已经使用了所有常用技术,例如ProOguard和图像压缩.但是,APK非常大 - 约25MB.
wikipedia 说:
apk文件是一种存档文件,具体是以zip格式的 基于JAR文件格式的包,用.apk作为文件名 扩展.
我最近注意到,如果我将解压缩APK(Android Studio的伪影输出),请使用7-zip重新zip并签名,然后尺寸致以减少2.5mb(至约22.5mb).我可以在没有问题的情况下上传它播放,安装和运行它.
和这里我的问题:
- 在解压缩和重新划分过程中有任何擦除的数据?
- 如果 no ,为什么aapt(Android Studio使用的那个)包文件 以如此低效的方式?
- 如果 是,正在擦除哪些数据(请在其中发布一些链接 可以阅读更多关于它的问题)?如果发生了什么问题 我将使用这种方法?
谢谢!
编辑[5/13/2015]:
压缩APK内容适用于我.但是,我必须对原始资源(通常在RES/RAW下)持谨慎态度.例如,调用资源#openRawresourcefd 压缩资源作为参数将以以下异常结束:
java.io.filenotfoundException:这个文件无法作为文件打开 描述符;它可能是压缩
因此,请记住将原始资源从压缩中排除.
推荐答案
我能够在没有问题的情况下上传它播放,安装和运行它.
仅在您尝试的设备上.例如,我怀疑您没有尝试API级别1设备.
在解压缩和重新划分过程中擦除了任何数据?
我们没有回答这个问题.唯一能回答这个问题的人是你,因为你是那些做了你的特定"解压缩过程"的人.您应该能够分析您的两个ZIP文件,并查看差异的位置,例如某些文件类型上的更高的压缩比,通过您运行"Rezip进程"等的方式丢失的文件.
一般来说,如果你没有重新申请自己,唯一应该丢失的东西就是任何zipalign-ing.
如果是,为什么AAPT(Android Studio使用的那个)包中的文件效率低下的方式?
尺寸不是唯一的考虑因素.访问速度是另一个,尽可能多的事情(例如,资源,资产)保存在APK文件中,并根据需要在飞行中读出来.减压逻辑的内存消耗又是另一个考虑因素.
Android设备,特别是早期的设备,有许多约束,磁盘空间,但其中一个.尽管这些约束中的一些限制已被放宽为硬件,但构建工具致力于向后兼容 - 例如,您应该能够在API级别1设备上运行的应用程序.这对工具的限制是在它们随时间变化的方式方面的约束.
如果我使用这种方法,可能会出错?
您的应用可能无法在Android设备上工作,在其中设置其运行时的运行时会对APK ZIP压缩算法进行某些假设.理想情况下,您的应用程序将到处运行良好.至少,您希望在您支持的每个API级别测试您的应用程序 - 旧的API级别可能会有些可能会更有可能"剪切角落"并使您的方法无效的假设.
其他推荐答案
假设您正在强制使用其zip兼容模式(-tzip)(-tzip),那么这是一个完全有效的操作(但是在@commonsware笔记时,您可以将自己打开到一些越野车在某些手机上的ZIP解压缩程序的实现).
您能够减少尺寸的原因是双重:
- 7zip有一个更好的压缩机,它使用更多的CPU对压缩来提出完全兼容但更压缩的表示
- AAPT,传统上用于DATROID文件的压缩/打包有很长的文件型列表(例如JPG,PNG,MP3),即它根本不会尝试压缩这些文件型已经拥有的原因内部压缩.即便如此,通过在APK文件中压缩它们通常会有几个百分率的增益
其他推荐答案
-
仅在创建APK时签署"V1(jar(jar(jar(jar签名)"选项.这是在Android Studio生成签名的APK时对话框的选项.阅读此处的谨慎, https://developer.android.com/studio/command-line/zipalign
-
将APK解压缩到文件夹,例如App-Release.
-
转到文件夹应用程序版本中的命令提示符.
-
然后重新zip:
"c:\ program files \ 7-zip \ 7z.exe"a -tzip -mx9 -r app-release.zip *.*
-
然后对齐:
"C:\用户\管理员\ appdata \ local \ android \ sdk \ build-tools \ 30.0.0 \ zipalign.exe"-f -v 4 app-reelease.zip app-refoy.apk
然后
然后验证对齐:
"c:\ users \管理员\ appdata \ local \ android \ sdk \ build-tools \ 30.0.0 \ zipalign.exe"-c -v 4 app-develay.apk
问题描述
I have a pretty large apk file, and I'm trying to reduce its size. Already used all the common techniques, such as Proguard and image compression. Still, the apk is quite large - about 25mb.
Wikipedia says that:
APK files are a type of archive file, specifically in zip format packages based on the JAR file format, with .apk as the filename extension.
I've recently noticed that if I'll unzip the apk (Android Studio's artifact output), re-zip it using 7-Zip and sign it, then the size magically decreases by 2.5mb (to ~22.5mb). I'm able to upload it to Play, install and run it without an issue.
And here're my questions:
- Is there any data that wiped during unzip & rezip process?
- If no, why aapt (the one that Android Studio uses) packages files in so inefficient manner?
- If yes, what data is being wiped (please post some links where I could read more about it)? What could go wrong if I'll use this method?
Thanks!
Edit [5/13/2015]:
Compressing the APK contents worked well for me. However, I had to be cautious with raw resources (typically placed under res/raw). For example, invoking Resources#openRawResourceFd with a compressed resource as parameter will end with the following exception:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
Therefore, remember to exclude raw resources from compression.
推荐答案
I'm able to upload it to Play, install and run it without an issue.
Only on the devices that you tried. I suspect, for example, that you did not try an API Level 1 device.
Is there any data that wiped during unzip & rezip process?
We have no way of answering that question. The only person who can answer that question is you, as you are the one who did your specific "unzip & rezip process". You should be able to analyze your two ZIP files and see where the differences are, such as higher compression ratios on certain file types, files that got lost by the way you ran your "rezip process", etc.
In general, the only thing that should be lost would be any zipalign-ing, if you did not reapply that yourself.
If no, why aapt (the one that Android Studio uses) packages files in so inefficient manner?
Size is not the only consideration. Speed of access is another, as many things (e.g., resources, assets) are kept in the APK file and read out of there on the fly as needed. Memory consumption for the decompression logic is yet another consideration.
Android devices, particularly early ones, have many constraints, disk space being but one of them. Even though some of these constraints have been relaxed as hardware has advanced, the build tools are dedicated to backwards compatibility -- you should be able to write an app today that can run on an API Level 1 device, for example. That puts constraints on the tools in terms of how they can change over time.
What could go wrong if I'll use this method?
Your app may not work on Android devices, where their runtimes are set up making certain assumptions about the APK ZIP compression algorithms use. Ideally, your app will run fine everywhere. At minimum, you would want to test your app on every API level that you support -- older API levels may be somewhat more likely to "cut corners" and make assumptions that your approach will invalidate.
其他推荐答案
Assuming you are forcing 7zip to use its zip compatible mode (-tzip) then this is an entirely valid operation (though as @CommonsWare notes, there is a tiny possibility that you are opening yourself up to some buggy implementations of the zip unpacking routine on some phones).
The reason that you are able to get a size reduction are twofold:
- 7zip has a better compressor, that uses more CPU on the compression to come up with a perfectly compatible but more compressed representation
- aapt which is traditionally used to do the compression/packaging of the Android file has a long list of filetypes (such as jpg, png, mp3) that it simply does not try to compress, on the grounds that these filetypes already have internal compression. Even so, there's often a few percent gain to be had by compressing them inside the apk file
其他推荐答案
Only sign the apk with the "V1 (Jar Signature)" option when creating the apk. This is an option on the dialog when generating a signed apk in Android Studio. Read the caution here, https://developer.android.com/studio/command-line/zipalign.
Unzip the apk to a folder, for example app-release.
Go to a command prompt in the folder app-release.
Then re-zip:
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx9 -r app-release.zip *.*
Then align:
"C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\30.0.0\zipalign.exe" -f -v 4 app-release.zip app-release.apk
Then verify alignment:
"C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\30.0.0\zipalign.exe" -c -v 4 app-release.apk