As i need to reduce the size of APK file, I have followed Apk Expansion guide to divide APK in chunks.
The Downloader library defines ways to download the expansion file
, but i need to know the way to exclude resource files
and aar
files from the apk.
I found following, but these are neither removing any resource-drawable files
nor any arr
files, and the size of apk remains same.
For testing purpose, i have added drawables
of around 4 MB
and couple of arr
files of size 3 MB
. I am creating apk
from Build->Build APK
option. I don't know if following will effect only on signed APK.
sourceSets {
main {
resources {
exclude '**/drawable/*'
}
}
}
android {
packagingOptions {
exclude 'lib/armeabi/a.so'
}
}
Expansion files may not contain any executable code. This is partly a Google Play policy, but also for security. Because they are written to a directory accessible to both your app and Play, and possibly to an SD card, if you put code their it would open your app to security exploits.
Because of this, you don't want to put AAR files in expansion files, as these normally have code. And many resources might not be appropriate, as these get compiled with your app and so have resource ids etc. Instead you should split out large elements that are not part of the explicit compile. Good candidates are things like:
All of these could potentially be in the assets directory of your app and are prime candidates for expansion files.
If you have none of the above, if you are going over 100Mb in size it is likely that you are not Proguarding your code correctly, and including a lot of code your app doesn't use. If this is the case, then learning to use Proguard correctly is probably a bigger improvement than switching to expansion files. SO users may be able to advise you more if you can say where the size in your APK is going? How much on images? How much on executable code? Are you using Android Studio and java, native code, or a technology like Unity? All of these have slightly different approaches to APK size minimization.
If there are specific resources you wish to keep or discard, create an XML file in your project with a <resources>
tag and specify each resource to keep in the tools:keep attribute and each resource to discard in the tools:discard attribute. Both attributes accept a comma-separated list of resource names.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
tools:discard="@layout/unused2" />
Save this file in your project resources, for example, at res/drawable/keep.xml. The build does not package this file into your APK. This way you can customize which resources to keep.
There are 2 things that you can do.
Firstly, you can use Lint. Lint will help to highlight and remove all the resources that you are not using in your code including the drawables.
Second you can use Proguard. Using Proguard
you can choose which version of APK you want to shrink including the debug
(or main, as in your example) version. Just insert the following code.
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With