How to reduce and compress an apk file in android

前端 未结 4 625
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 21:06

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 f

相关标签:
4条回答
  • 2020-12-28 21:42

    You can ZipAlign the signed apk to compress it.

    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.

    zipalign -c -v 4 "C:\Users\Joisar\Desktop\project_name\appname_signed.apk"
    
    0 讨论(0)
  • 2020-12-28 21:45

    Find the problem

    You can Analyse APK from Android Studio. This shows you the various files in their directories and their sizes, both absolute and relative to the whole 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 Drawables provide 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.

    proguard minify

    Add the following to app/build.gradle to enable proguard to reduce unused classes from your project:

    buildTypes {
        release {
          minifyEnabled true
          shrinkResources true
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
        }
    }
    
    0 讨论(0)
  • 2020-12-28 21:46

    You can compress an APK file (rar, zip), but it has to be decompressed in order to work.

    If the apk file to large to distribute you can:

    1. Use expansion files: http://developer.android.com/google/play/expansion-files.html
    2. Create multiple versions of your apk: http://developer.android.com/google/play/publishing/multiple-apks.html (e.g. no hdpi files for ldpi devices)
    3. 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.

    4. Remove unused resources: https://code.google.com/p/android-unused-resources/

    5. Optimize your images by using tools like 9patch and png optimizers
    6. Remove everything that is only used for debugging purposes (debug classes, or even Log() methods). Also try removing unnecessary .so files
    0 讨论(0)
  • 2020-12-28 21:49
    1. Use .svg format icon set.
    2. Compress PNGs.
    3. Use only specific libraries of Google Play Services.
    4. Use Proguard

      build.gradle

      android {
          ...
          buildTypes {
              release {
                  minifyEnabled true
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
          }
      }
      
    5. Shrink Resources

      build.gradle

      android {
          ...
          buildTypes {
              release {
                  shrinkResources true
                  minifyEnabled true
                  ...
              }
          }
      }
      
    0 讨论(0)
提交回复
热议问题