4

My assets and drawable are only 2mb java and xml sources is only 1mb but after build project the apk size is 20mb!

I set shrinkResources true and remove unused resources and generate app with proguard.

Is there a way to reduce the size of apk?

Marged
  • 10,577
  • 10
  • 57
  • 99
alirezaashrafi
  • 133
  • 1
  • 1
  • 9

5 Answers5

3

Android Studio has its own apk analyzer which is very useful for cases like yours.

Analyze your apk file and check which files are using this much space.

https://developer.android.com/studio/build/apk-analyzer.html

enter image description here

Also using ProGuard helps to reduce apk size.

Additionally, avoid using unnecessary libraries. For example,

if you need to use Google Analytics, import gradle only analytics library like this:

compile 'com.google.android.gms:play-services-analytics:10.2.4'

do not use like this:

compile 'com.google.android.gms:play-services:10.2.4'

second example uses too much space and redundant classes and files.

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
1

at your gradle, normally debug apk will be larger than release APK about 50%. If you care about the debug size, just do the same config like release on debug config

buildTypes {
        release {
            minifyEnabled true  <-- minify your code
            shrinkResources true <-- remove any unused resources
            zipAlignEnabled true <-- optimization
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  <-- enable proguard is important to shrink byte code
        }
    }

Second, if you are using google services dependencies, please use individual dependencies.. refer here In android studio 2.2 and above they have added apk analyser tool in Build menu. Use that to analyse APK.

Not only for google services, others library also. some library put android design or appcompat in their library. so you need to exclude those module (if you already have in your dependencies)

Community
  • 1
  • 1
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
0

According to the "Resource Shrinking" webpage of Andriod documentations (here), you can minimize the app's size via the build.gradle file, by using these lines:

    android {
    ...

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
Hamid Goodarzi
  • 1,364
  • 2
  • 13
  • 22
0

You can do following things

  • Remove non required libraries. even use required libraries (like map or gcm individual instead of full play service library)

  • Use vector images instead of multiple png.

  • Use zipAlignEnabled command in build file

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
0

Check my blog Different ways to reduce apk size

Main Points are :

  • android.enableR8=true // enable New R8 Code Shrinker
  • minifyEnabled true && Add Proguard Rules
  • Examine Your APK Using Android Studio’s APK Analyzer
  • Enable Resource Shrinking
  • Convert Your PNGs, JPEGs, and BMPs Into WebP
  • Avoid enumerations and use @IntDef annotation
  • Use aaptOptions { cruncherEnabled = false } to compress images
  • Use Remove Unused Resources android studio built-in function to remove all the unused resources in the application
  • Use Code Cleanup android studio built-in function to remove

Note: Go enable it! (*just double and triple check everything works afterwards)

Sai's Stack
  • 1,345
  • 2
  • 16
  • 29