减少 android 应用程序 (apk) 大小 [英] Reducing android app (apk) size

查看:31
本文介绍了减少 android 应用程序 (apk) 大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在将在 Google Play 商店发布我的第一个应用.我已经压缩了我的应用程序中使用的图像.我对应用大小有一些疑问.

I would be now publishing my first app on Google play store. I have already compressed images used in my app. And I have some questions regarding the app size.

如果应用程序的大小小于 2 MB,那么用户卸载该应用程序的机会就会减少,对于属于教育领域的应用程序,我的这种说法是否正确?

但是,当我在 Windows 中看到我的 apk 文件时,它显示为 3.10 MB,但是当它安装在设备中时(从应用信息中查看),它显示:

However, When I see my apk file in windows it shows 3.10 MB but when it gets installed in device as viewed from App info it shows:

Total............................8.68 MB
App..............................7.02 MB
USB storage app..................0.00 B
Data.............................1.66 MB (perhaps it is the size of sqlite db + ttf's)
SD card..........................0.00B

那么,为什么我看到我的应用大小增加了这么多,可以最小化吗?

而且,我在我的项目中使用了 4 个库(jar),它们位于 Android 私有库 中,但这些库也在此文件夹外有它们的副本.

And, I am using 4 libraries (jars) in my project which are in Android Private Libraries, but these library have their copy outside this folder also.

删除它们是否更安全,&删除它们可以帮助减小 apk 大小吗?

此外,我访问了许多描述 Proguard 功能的网页,我真正了解 Proguard 如何通过删除未使用的代码和重命名类、字段来缩小 apk 的大小,缩小、优化和混淆我们的代码, 以及具有语义模糊名称的方法.结果是较小的 .apk 文件更难逆向工程.但我不知道我的 proguard-project.txt 应该是什么样子的?
我在我的应用程序中使用了 4 个库,即 easyfacebookandroidsdk_2.3.jar,android-support-v7-appcompat.jar,google-play-services.jar &android-support-v4.jar.

Also I have visited many web pages describing what Proguard does, I truly understand how Proguard reduces size of apk, it shrinks, optimizes, and obfuscates our code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer. But I do not know How should be my proguard-project.txt should look like ?
I am using 4 libraries in my app namely easyfacebookandroidsdk_2.3.jar,android-support-v7-appcompat.jar,google-play-services.jar & android-support-v4.jar.

目前我的 proguard-properties.txt 看起来像这样 &它也不使用 WebView

Currently my proguard-properties.txt looks like this & also it do not uses WebView

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;  
#}
-keep class com.facebook.** { *; }
-keepattributes Signature

那么,我应该在我的 proguard-properties.txt 中添加哪些行,以便它可以删除未使用的引用、类等,以减小 apk 大小?

So, what lines should I add in my proguard-properties.txt so that it can remove unused references,classes,etc., in order to reduce the apk size ?

使用 AndroidUnusedResources.jar 可在 https://code.google.com/p/android-unused-resources/ 在 android 项目中查找未使用的资源.

Using AndroidUnusedResources.jar available at https://code.google.com/p/android-unused-resources/ finds unused resources in android project.

我认为这与 proguard 已经做过的事情相同?还是应该在启用 proguard 后使用它?

此外,您可以在回答中提及您认为应该分享的任何遗漏之处.
提前致谢...

Also you can mention in your answer anything that is missing which you feel should be shared.
Thanks in Advance...

推荐答案

那么,为什么我的应用程序大小增加了这么多,可以最小化吗?

几乎完全是因为你的 res 文件夹图片!您应该在 drawable-mdpi、-hdpi、xhdpi 文件夹中拥有每个图像的大约 4 个副本.唯一的区别是它们的大小不同.

So, Why do I see that much increase in my app size, Can it be minimized?

Almost solely because of your res folder images! You should have around 4 copies of each image in the drawable-mdpi, -hdpi, xhdpi folders. The only difference is that theu are all different sizes.

可能没有这些 jar 文件的多个副本!一份带有链接的副本!

Chances are there aren't multiple copies of those jar files! Just one copy with links to it!

是的,您对 Proguard 的看法是正确的.但是,请意识到您在这里谈论的是文本文件.他们真的很小.我的意思是尺寸非常小(以千字节为单位).您的问题是兆字节 (MB) 大小的问题.那是图片!!!!

Yes, what you said was right about Proguard. However, realize that you are talking about text files here. They are really small. I mean really small in size (kilobyte-wise). Your problem is something in the megabyte (MB) size. That is images!!!!

重复.那是图片!!!!

启用 proguard 将节省一点大小.

Enabling proguard will save a little bit size-wise.

是的,这些对您来说都更为重要.请阅读并应用这些.祝你好运!

Yes, these are all more important in your case. Please read and apply these. Good luck!

如果用户发现您的应用有帮助,他们就不会卸载它;但是,您可以做几件事

If a user finds your app helpful, then they won't uninstall it; however, there are several things you can do

  • 您能否将图像移动到网络并将它们async到您的应用中?
  • 尽可能确保您的文件完全压缩.使用 pngquanttinypng.
  • 检查图像中是否有任何重复.您或许可以使用 9-patch 来实现它.
  • 您或许可以为某些图像制作自己的可绘制对象.
  • 允许用户将数据移动到 SD 卡.

  • Can you move images to the web and async them into your app?
  • Make sure your files are completely compressed as much as possible. Use pngquant or tinypng.
  • Check for any repetition in the images. You might be able to use 9-patch for it.
  • You might be able to make your own drawables for certain images.
  • Allow users to move data to an SD card.

在应用在 Play 商店中出现一段时间后,检查开发者控制台并查看使用最少的屏幕尺寸/密度.em> 从您的应用中删除那个可绘制文件夹!这可以大大减少大小!但是,请检查下面我的编辑以了解这种方法的缺点!!

After the app has been in the Play Store for a while, check the Developer Console and see which screen size/density is used the least. Remove that drawable folder from your app! This could reduce the size a good amount! But, check my edit below for the cons of this approach!!

您可能想展示一些图片,这样我们就可以看到您在处理什么

You might want to show a few of the images, as that would allow us to see what you are dealing with

@Ashwin N Bhanushali 有一个很好的答案这里对于我没有回答的部分问题,但他基本上告诉你除了我说的,还有一个或两个想法之外,你真的什么也做不了.

@Ashwin N Bhanushali Has a great answer here for the part of the question I did not answer, but he basically told you that there was nothing you could really do except what I said, plus an idea or 2 more.

我确实有另一个基于他的一个想法的想法,它会更好,因为 Android Studio 不再在项目中包含 drawable-ldpi 文件夹.所以如果你使用 Eclipse,它只会有帮助,我的扩展

I did have another idea that was based on one of his ideas that would be even better, because Android Studio does not include the drawable-ldpi folder in the project anymore. So it would only be helpful if you use Eclipse, mine expands

  1. 在完成建议的修复后将 APK 发布到 Google Play 商店
  2. 一段时间过去了 -- 一周、一个月等 -- 您可以查看应用程序的统计数据Google Play 开发者控制台.
  3. 寻找使用最少的屏幕尺寸桶(drawable-mdpi、-hdpi、-xhdpi、-xxhdpi)!您可以从您的应用中删除这些存储桶
  1. Release the APK into the Google Play Store after making the recommended fixes
  2. After some time has passed -- a week, a month, etc -- you can check the stats of your application in the Google Play Developer Console.
  3. Look for the screen size buckets(drawable-mdpi, -hdpi, -xhdpi, -xxhdpi) that are used the least! You could remove those buckets from your app altogether!

这是有效的,因为图片比文本文件占用更多的字节.所有这些可绘制对象都其中包含相同的图像(只是大小不同).从较少使用的存储桶中删除图像可以节省大量空间!!

This works because pictures take up way more bytes than text files. And all those drawables have the same images in them (just different sizes). Removing the images from less used buckets can save a lot of space!!

请注意,从您的应用中删除这些存储分区意味着您的应用将不得不在使用已删除存储分区的手机上执行更多工作.对于这些用户来说,应用程序可能会变慢;但是,应用程序的大小可以减少更多.

Take note that removing those buckets from your app will mean that your app will have to do more work on those phones that use the removed buckets. The app might be slower for those users; however, the app size can be reduced more.

文本文件几乎不占用任何内容.查看您项目的文件系统!

Text files take up almost nothing. Take a look in the file system for your project!

如下图.检查大小...

In the picture below. Check the size of...

  • java 文件夹
  • res 文件夹
  • the java folder
  • the res folder

在您的情况下,您会看到 java 文件夹可能非常小(10 到几百 KB);但是,您会看到 res 文件夹非常大(几 MB!).

In your case, you will see that the java folder is probably really small in size (10 through a few hundred KBs); however, you will see that the res folder is really large in size (several MBs!).

这意味着任何压缩或减少代码库都将非常小而且不值得!!

因此,减小 APK 大小的最佳方法是按照我上面的说明进行操作!

这是一个您可以立即进行的测试,并查看 ProguardAndroidUnusedResources.jar 等工具将减少的大小的绝对最大值(以及更多)APK 文件!

Here is a test that you can do right away and see the absolute maximum (plus more) that tools like Proguard, and AndroidUnusedResources.jar will reduce the size of the APK file!

您需要做的是从您的项目中删除所有代码,但类声明除外.对抽象类、接口、枚举等执行相同的操作...您的项目结构应该保持不变,并且您根本不应该使用任何外部库.

What you have to do is remove ALL code from your project except for the class declarations. Do the same for Abstract classes, Interfaces, Enums, etc... Your project structure should stay the same, and you should not be using any external libraries at all then.

  1. 所以你的所有课程都会有这样的东西......

  1. So you will have something like this for all of your classes...

public class CustomClass {

    private String variable; // List of variables

    public CustomClass() { ... } // List of constructors

    public getVariable() { ... } // List of Assessors

    public setVariable(String val) { ... } // List of Mutators

    private String helperMethods() { ... } // List of helper and other methods

}

  • 您应该删除类、接口、枚举等中的所有代码,如下所示;然而,不要管你所有的图片!!...

  • You should remove all the code inside of the classes, interfaces, enums, etc to look like the following; however, leave all your images alone!! ...

    public CustomClass {
    
        // Everything is removed!! No libraries used, nothing. Unless certain classes 
        //     require some methods like Activity. Those methods should be empty though!
    
    }
    

  • 现在编译并构建您的项目.

  • Now compile and build your project.

    根据您当前的 APK 检查新的 APK 大小.

    Check the new APK size to your current APK.

    • 这将是您可以使用代码实现的最小压缩文件.是的,你的程序不会做任何事情,但这不是重点.您在意尺寸,这只是一个测试.

    在您的手机上安装该应用,并将新的应用大小与您当前的应用大小相匹配

    Install the app on your phone, and check the new app size to your current app size

    • 这将是您可以使用代码实现的最小的未压缩文件.是的,你的程序不会做任何事情,这只是一个测试.

    如果您注意到这两个应用程序的大小仍然大致相同,那么在此阶段将所有时间都花在 Proguard 和 AndroidUnusedResources.jar 上是没有意义的.这是一项测试,旨在了解您将个人资源用于减少应用大小的哪些方面.

    If you notice that the 2 apps are still around the same size, then spending all your time with Proguard and AndroidUnusedResources.jar is meaningless at this stage. This is a test to see where you would spend your personal resources in app size reduction.

    这篇关于减少 android 应用程序 (apk) 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆