如何在android中减少和压缩apk文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25380319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reduce and compress an apk file in android
提问by Shreyash Rabadiya
I have one apk file but its file size is 22.4 MB. It's huge, so I need to reduce or compress the apk file in android. What is the right way to reduce and compress the apk file size?
我有一个 apk 文件,但它的文件大小为 22.4 MB。它很大,所以我需要减少或压缩android中的apk文件。减少和压缩 apk 文件大小的正确方法是什么?
回答by Mdlc
You can compress an APK file (rar, zip), but it has to be decompressed in order to work.
您可以压缩 APK 文件(rar、zip),但必须对其进行解压缩才能工作。
If the apk file to large to distribute you can:
如果要分发的 apk 文件很大,您可以:
- Use expansion files: http://developer.android.com/google/play/expansion-files.html
- Create multiple versions of your apk: http://developer.android.com/google/play/publishing/multiple-apks.html(e.g. no hdpi files for ldpi devices)
Use ProGuard
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or higher, or for Java Micro Edition.
If you use Eclipse, A default configuration file will automatically be added to your Project. But this default configuration only covers general cases, so you most likely have to edit it for your own needs.
Remove unused resources: https://code.google.com/p/android-unused-resources/
- Optimize your images by using tools like 9patchand png optimizers
- Remove everything that is only used for debugging purposes (debug classes, or even Log() methods). Also try removing unnecessary .so files
- 使用扩展文件:http: //developer.android.com/google/play/expansion-files.html
- 创建多个版本的 apk:http: //developer.android.com/google/play/publishing/multiple-apks.html(例如 ldpi 设备没有 hdpi 文件)
使用混淆器
ProGuard 是一个免费的 Java 类文件压缩器、优化器、混淆器和预验证器。它检测并删除未使用的类、字段、方法和属性。它优化字节码并删除未使用的指令。它使用无意义的简短名称重命名剩余的类、字段和方法。最后,它会针对 Java 6 或更高版本或 Java Micro Edition 预验证已处理的代码。
如果您使用 Eclipse,默认配置文件将自动添加到您的项目中。但是此默认配置仅涵盖一般情况,因此您很可能必须根据自己的需要对其进行编辑。
删除未使用的资源:https: //code.google.com/p/android-unused-resources/
- 使用9patch和png 优化器等工具优化图像
- 删除仅用于调试目的的所有内容(调试类,甚至 Log() 方法)。还可以尝试删除不必要的 .so 文件
回答by Mehul Joisar
You can ZipAlignthe signed apk to compress it.
您可以ZipAlign签名的 apk 来压缩它。
Usage:
用法:
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
Example:
例子:
D:\android-sdk\android-sdk\tools>zipalign -f -v 4 "C:\Users\Joisar\Desktop\project_name\appname_signed.apk" "C:\Users\Joisar\Desktop\project_name\appname__zipaligned.apk"
Note:
笔记:
Kindly checkout the apk whether it is zipaligned or not by following command, if it's not zipaligned, then do it.
请通过以下命令检查apk是否是zipaligned,如果它不是zipaligned,则执行它。
zipalign -c -v 4 "C:\Users\Joisar\Desktop\project_name\appname_signed.apk"
回答by serv-inc
Find the problem
找出问题
You can Analyse APKfrom Android Studio. This shows you the various files in their directories and their sizes, both absolute and relative to the whole APK:
您可以从 Android Studio分析 APK。这将向您显示其目录中的各种文件及其大小,包括相对于整个 APK 的绝对和相对大小:
Check for Unused Resources
检查未使用的资源
This is the easiest: shows you, among others, the unused resources:
Just remove them from your project.
只需将它们从您的项目中删除。
Use Vector Images
使用矢量图像
Vector Drawablesprovide one sharp image for all resolutions, greatly reducing the size for your graphics. As usually, you can use these for icons etc, but not for Photos. Non-vectorized images can be reduced with trimage.
Vector Drawables为所有分辨率提供一张清晰的图像,大大减少了图形的大小。通常,您可以将这些用于图标等,但不能用于照片。非矢量化图像可以使用trimage 缩小。
proguard minify
proguard 缩小
Add the following to app/build.gradle
to enable proguard to reduce unused classes from your project:
添加以下内容以app/build.gradle
启用 proguard 以减少项目中未使用的类:
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
回答by Shubham Nikam
- Use .svg format icon set.
- Compress PNGs.
- Use only specific libraries of Google Play Services.
Use Proguard
build.gradle
android { ... buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
Shrink Resources
build.gradle
android { ... buildTypes { release { shrinkResources true minifyEnabled true ... } } }
- 使用 .svg 格式图标集。
- 压缩PNG。
- 仅使用特定的 Google Play 服务库。
使用混淆器
构建.gradle
android { ... buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
收缩资源
构建.gradle
android { ... buildTypes { release { shrinkResources true minifyEnabled true ... } } }