When i run my app size of apk will 21 MB. even i enabled proguard. i use android studio to run project. in project files src folder has 8.62 MB size and lib folder size is 4
The Best Way to Reduce The Size of Your APK File is reduce the use of the libaries that are used in build.gradle file . these libaries create a lot of redandancy and in turn increase the size of the project .. enter image description here
I solve this issue by adding this code in module app build.gradle file.
android {
packagingOptions{
exclude 'AndroidManifest.xml'
exclude 'lib/arm64-v8a/libcardioDecider.so'
exclude 'lib/arm64-v8a/libcardioRecognizer.so'
exclude 'lib/arm64-v8a/libcardioRecognizer_tegra2.so'
exclude 'lib/arm64-v8a/libopencv_core.so'
exclude 'lib/arm64-v8a/libopencv_imgproc.so'
exclude 'lib/armeabi/libcardioDecider.so'
exclude 'lib/armeabi-v7a/libcardioDecider.so'
exclude 'lib/armeabi-v7a/libcardioRecognizer.so'
exclude 'lib/armeabi-v7a/libcardioRecognizer_tegra2.so'
exclude 'lib/armeabi-v7a/libopencv_core.so'
exclude 'lib/armeabi-v7a/libopencv_imgproc.so'
exclude 'lib/mips/libcardioDecider.so'
exclude 'lib/x86/libcardioDecider.so'
exclude 'lib/x86/libcardioRecognizer.so'
exclude 'lib/x86/libcardioRecognizer_tegra2.so'
exclude 'lib/x86/libopencv_core.so'
exclude 'lib/x86/libopencv_imgproc.so'
exclude 'lib/x86_64/libcardioDecider.so'
exclude 'lib/x86_64/libcardioRecognizer.so'
exclude 'lib/x86_64/libcardioRecognizer_tegra2.so'
exclude 'lib/x86_64/libopencv_core.so'
exclude 'lib/x86_64/libopencv_imgproc.so'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.paypal.sdk:paypal-android-sdk:2.14.1'
}
These 4 things reduce your apk size almost 50-60%
proguard-android.txt
with proguard-android-optimize.txt
minifyEnabled
true
shrinkResources
true
Add resConfigs
buildTypes {
release {
minifyEnabled true
shrinkResources true
resConfigs "en"
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Details:
proguard-android-optimize.txt
is the more aggressive progurard configuration than proguard-android.txt
.
shrinkResources
attribute will remove all the resources, those are not used anywhere in the project.
resConfigs
attribute will remove all the other localized resources while building the application. Here, it strips other than english resources.
You can easily reduce the size of the app by reducing the image size. Please use https://tinypng.com/ to reduce your image size. This will ensure your app size is also reduced. The image quality will remain the same.
Also in case of libraries, please be sure to include ONLY those components that you really need. Also remove any unwanted and unused imports.
Before click Finish when open new project wizard, uncheck backward compatibility(appcompat). This is can reduce from 1.5 MB to 180 KB.
There are a few techniques you can use to reduce the apk size:
1. Proguard: In build.gradle
file for app-level, add the following code
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
2. Unnecessary Resources: Many times unnecessary resources are present in the source code. Inpect the source code by Analyze->Inspect Code. After the inspection ends, delete unnecessary resources.
3. Webp images: Webp images have less size than png files and also it retains the original image quality. More to that webp images are natively supported in Android. Right-click the drawable(/mipmap) folder -> Convert to webp. Choose appropriate options which suits your need.
4. Signed apk: The debug apk size is greater than the signed apk. Generate signed apk by going to Build-> Generate Signed Bundle/Apk->APK. Overall apk size is now reduced.
5. Android App Bundle: If you are going to upload the app on Google Play, generate Android App bundle instead of apk.To generate app bundle go to, Build-> Generate Signed Bundle/Apk-> Android App Bundle. It will reduce your app size for sure. For example, my apk size of 10 MB is reduced to just 6-7 MB using Android App Bundle.