10

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'
    }
}
dev90
  • 7,187
  • 15
  • 80
  • 153

3 Answers3

8

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • For official docs, see https://developer.android.com/studio/write/tool-attributes#resource_shrinking_attributes There's also the nifty https://developer.android.com/studio/build/apk-analyzer tool to verify whether or not certain files were packaged into your apk. – qix Jul 20 '18 at 08:48
4

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:

  • Open GL textures
  • large sound files for sound effects
  • large level data or maps for games
  • large images

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.

Nick Fortescue
  • 13,530
  • 1
  • 31
  • 37
  • Hi @Nick did you have any android developer links for the above statement? – Chetan Joshi Oct 29 '21 at 10:57
  • This answer was correct at the time, but now best practice is to use Google Play Feature delivery: https://developer.android.com/guide/playcore/feature-delivery – Nick Fortescue Nov 01 '21 at 06:56
  • Thanks, @Nick I have gone through with the link mentioned above, But Its modular features from the base app. what we need to do in the case of 100 MB .aar file need for the main functionality of our application. – Chetan Joshi Nov 02 '21 at 09:36
1

There are 2 things that you can do.

  1. 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.

  2. 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'
        }
    } }
    
Abhishek
  • 1,261
  • 1
  • 13
  • 30