Why when creating a .apk file in Android Studio, do you add a lot (dozens) of .png .xml resource files that are not used in the application?

eg:
in the \res\drawable

abc_btn_borderless_material.xml
abc_cab_background_internal_bg.xml
common_google_signin_btn_icon_dark.xml
common_google_signin_btn_icon_dark_focused.xml
..................................................

in the \res\layout folder

abc_activity_chooser_view.xml
abc_action_menu_layout.xml
abc_search_view.xml
...................

I guess this is related to the theme used (I use Theme.AppCompat.Light ) and the libraries I use:

 dependencies { compile 'com.android.support:appcompat-v7:25.1.0' compile 'com.google.firebase:firebase-ads:10.0.1' } 

This leads to an increase in the size of the apk file and probably slows down the loading of the application.

Can I delete these files from the project?

    2 answers 2

    Enable the removal of unused resources.

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

    https://developer.android.com/studio/build/shrink-code.html#shrink-resources

    There is an article about setting up ProGuard: http://jollydroid.ru/notebook/2016-12-29-ProGuard-1

    • Thanks @ tse. Using Proguard (shrink + optimize) reduced the size of apk from 2.5Mb to 2.0MB mainly due to classes.dex. But the "extra" resource files are still present in the apk file. And the values ​​folders from the res folder have disappeared somewhere. The generated apk installs and works fine - Vlad.Sh

    In addition to eliminating unused resources, you can specify the list of localizations you need , as well as other alternative resources in the gradle config parameter in the shrinkResources true config, which will significantly reduce the size of the resulting file:

     android { defaultConfig { ... //для версий gradle <= 2.10 resConfigs "en", "fr", "ru" resConfigs "nodpi", "xhdpi", "xxhdpi", "xxxhdpi" // для версий gradle > 2.10 resConfigs = ["en", "fr", "ru"] resConfigs = ["nodpi", "xhdpi", "xxhdpi", "xxxhdpi"] } ... buildTypes { release { shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } 

    Here - leave only English, French and Russian locale and support for screens with XHDPI to XXXHDPI (new versions of gradle changed the format of the line, both formats are indicated in the config, but you need to choose only one depending on the version of gradle used in the project).

    It’s no secret that by default the Android application contains localizations in over one hundred languages ​​and alternative resources on all possible devices, including TVs. If you can control your resources, the libraries used in the project drag with them all possible localizations and sizes, this will help get rid of them. Similarly, you can exclude other alternative resources, such as support for tablets, if your application does not support them.
    For excluded resources, when you start your application, the default resources will be used, but you can generally exclude them by specifying the necessary restrictions in the manifest.

    Also now there is a tool in gradle - Multiply APK and a very powerful tool for configuring project content - the splits section , which allows you to manipulate not only resources, locales, but also supported architectures. I did not use it myself, so I will leave only a link to the offdoc and a brief example:

     android { defaultConfig { ... } ... splits { density { // Кофигурация разрешений экрана enable true // Будут исключены из сборки exclude "ldpi", "xxhdpi", "xxxhdpi" // Оставить поддержку только следующих экранов compatibleScreens 'small', 'normal', 'large', 'xlarge' } language { enable true // включены в сборку языки include ""en", "fr", "ru" } // Конфигурация архитектур abi { enable true reset() // Включенные в сборку архитектуры include "x86", "armeabi-v7a", "mips" // не делать универсальную сборку universalApk false } } } 

    There are commands that indicate which resources will be included in the project (include), which are excluded (exclude) and so on. many different features worth exploring.