Android support library increases APK size a lot

前端 未结 4 1070
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 02:46

I\'m using AppCompat support library in my Android project. AppCompat has plenty of drawables and resources which I don\'t use in my app. That unne

相关标签:
4条回答
  • 2020-12-30 03:23

    From Android Gradle Build System, since version 0.7.0:

    New option on product Flavor (and defaultConfig) allow filtering of resources through the -c option of aapt You can pass single value (resConfig) or multiple values (resConfigs) through the DSL. All values from the default config and flavors get combined and passed to aapt. See "basic" sample. In the "basic" sample:

    defaultConfig {
        ...
        resConfig "en"
        resConfigs "nodpi", "hdpi"
    }
    

    So, try the following to achieve what you asked for:

    productFlavors {
        ...
        frOnly {
            resConfig "fr"
        }
        ...
    }
    

    Note that you might also want to include *dpi, port, land, etc.. as well.

    Answer is from: Android Studio exports strings from support library to APK, thanks to Primoz990

    0 讨论(0)
  • 2020-12-30 03:29

    Starting from version 24.2.0, the v4 Support Library has been split into several smaller modules.

    So, apart from using shrinkResources and proguardFiles, also make sure that you are using only the specific modules that your app needs. e.g.

    If your app only uses Compat utils like NotificationCompat, ContextCompat or ResourcesCompat etc., use only the compat module as:

    compile 'com.android.support:support-compat:24.2.0'
    
    0 讨论(0)
  • 2020-12-30 03:33

    shrinkResources can also be an option for you. It is available since 0.14 - but be careful - it still has some pits like protect resources when using shrinkResources

    0 讨论(0)
  • 2020-12-30 03:41

    Although OP has cleared up that he is using proguard, I would like to post some code if it helps someone because I am able to shrink my app from 3.8 MB to 3.1 MB using the accepted answer and further to mere 1.8 MB through proguard. I used this configuration in my app level build.gradle file:

    buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
    }
    
    0 讨论(0)
提交回复
热议问题