APK split by density still contains all resources

2019-06-16 10:27发布

问题:

I decided to try apk sliptting to reduce the size of my apk. I added the following to my gradle build file

splits {

    // Configures multiple APKs based on screen density.
    density {

        // Configures multiple APKs based on screen density.
        enable true

        // Specifies a list of screen densities Gradle should not create multiple APKs for.
        exclude "ldpi"

        // Specifies a list of compatible screen size settings for the manifest.
        compatibleScreens 'small', 'normal'
    }
}

This successfully generates separate apks for the various densities. However, I noticed that all of the apks were the same size, none of them were smaller than the universal apk. So, I loaded one (app-hdpi-release.apk) into the apk analyzer and found that it contained all resources. None were stripped out.

So effectively all the config did was to generate the same apk with different filenames. Am I missing something? Are there any other build options that could potentially be blocking the resources being removed?

回答1:

I did some hit and trial and finally it accepted. Before I was splitting on the basis of screen densities ONLY. Then I added the tag $compatibleScreens$ and it worked.

Here is the final split block-

android {
  ...
  splits {

    density {
      enable true

      reset()
      include "mdpi", "hdpi", "xhdpi",     "xxhdpi"

      // This is the line of code which got it right
      compatibleScreens 'small', 'normal', 'large', 'xlarge'
    }
  }
}