Class ResourceUsageAnalyzer

java.lang.Object
com.android.build.gradle.tasks.ResourceUsageAnalyzer
All Implemented Interfaces:
com.android.build.shrinker.ResourceShrinker, AutoCloseable

public class ResourceUsageAnalyzer extends Object implements com.android.build.shrinker.ResourceShrinker
Class responsible for searching through a Gradle built tree (after resource merging, compilation and shrinking has been completed, but before final .apk assembly), which figures out which resources if any are unused, and removes them.

It does this by examining

  • The merged manifest, to find root resource references (such as drawables used for activity icons)
  • The merged R class (to find the actual integer constants assigned to resources)
  • The ProGuard mapping files (to find the mapping from original symbol names to short names)*
  • The merged resources (to find which resources reference other resources, e.g. drawable state lists including other drawables, or layouts including other layouts, or styles referencing other drawables, or menus items including action layouts, etc.)
  • The shrinked output classes (to find resource references in code that are actually reachable)
From all this, it builds up a reference graph, and based on the root references (e.g. from the manifest and from the remaining code) it computes which resources are actually reachable in the app, and anything that is not reachable is then marked for deletion.

A resource is referenced in code if either the field R.type.name is referenced (which is the case for non-final resource references, e.g. in libraries), or if the corresponding int value is referenced (for final resource values). We check this by looking at the shrinked output classes with an ASM visitor. One complication is that code can also call Resources#getIdentifier(String,String,String) where they can pass in the names of resources to look up. To handle this scenario, we use the ClassVisitor to see if there are any calls to the specific Resources#getIdentifier method. If not, great, the usage analysis is completely accurate. If we do find one, we check all the string constants found anywhere in the app, and look to see if any look relevant. For example, if we find the string "string/foo" or "my.pkg:string/foo", we will then mark the string resource named foo (if any) as potentially used. Similarly, if we find just "foo" or "/foo", we will mark all resources named "foo" as potentially used. However, if the string is "bar/foo" or " foo " these strings are ignored. This means we can potentially miss resources usages where the resource name is completed computed (e.g. by concatenating individual characters or taking substrings of strings that do not look like resource names), but that seems extremely unlikely to be a real-world scenario.

Analyzing dex files is also supported. It follows the same rules as analyzing class files.

For now, for reasons detailed in the code, this only applies to file-based resources like layouts, menus and drawables, not value-based resources like strings and dimensions.

  • Field Details

    • REPLACE_DELETED_WITH_EMPTY

      public static final boolean REPLACE_DELETED_WITH_EMPTY
      Whether we should create small/empty dummy files instead of actually removing file resources. This is to work around crashes on some devices where the device is traversing resources. See http://b.android.com/79325 for more.
      See Also:
    • TWO_PASS_AAPT

      public static final boolean TWO_PASS_AAPT
      Whether we support running aapt twice, to regenerate the resources.arsc file such that we can strip out value resources as well. We don't do this yet, for reasons detailed in the ShrinkResources task We have two options: (1) Copy the resource files over to a new destination directory, filtering out removed file resources and rewriting value resource files by stripping out the declarations for removed value resources. We then re-run aapt on this new destination directory. The problem with this approach is that when we re-run aapt it will assign new id's to all the resources, so we have to create dummy placeholders for all the removed resources. (The alternative would be to then run compilation one more time -- regenerating classes.jar, regenerating .dex) -- this would really slow down builds.) A cleaner solution than this is to get aapt to support using a predefined set of id's. It can emit R.txt symbol files now; if we can get it to read R.txt and use those numbers in its assignment, we can solve this cleanly. This request is tracked in https://code.google.com/p/android/issues/detail?id=70869 (2) Just rewrite the .ap_ file directly. It's just a .zip file which contains (a) binary files for bitmaps and XML file resources such as layouts and menus (b) a binary file, resources.arsc, containing all the values. The resources.arsc format is opaque to us. However, MOST of the resource bulk comes from the bitmap and other resource files. So here we don't even need to run aapt a second time; we simply rewrite the .ap_ zip file directly, filtering out res/ files we know to be unused. Approach #2 gives us most of the space savings without the risk of #1 (running aapt a second time introduces the possibility of aapt compilation errors if we haven't been careful enough to insert resource aliases for all necessary items (such as inline @+id declarations), or if we haven't carefully not created aliases for items already defined in other value files as aliases, and perhaps most importantly, introduces risk that aapt will pick a different resource order anyway, which we can only guard against by doing a full compilation over again. Therefore, for now the below code uses #2, but since we can solve #1 with support from aapt), we're preserving all the code to rewrite resource files since that will give additional space savings, particularly for apps with a lot of strings or a lot of translations.
      See Also:
    • FORMAT

      public static final Pattern FORMAT
  • Constructor Details

    • ResourceUsageAnalyzer

      public ResourceUsageAnalyzer(@NonNull File rClasses, @NonNull Iterable<File> classes, @NonNull File manifest, @Nullable File mapping, @NonNull Iterable<File> resources, @Nullable File reportFile)
    • ResourceUsageAnalyzer

      public ResourceUsageAnalyzer(@NonNull File rClasses, @NonNull Iterable<File> classes, @NonNull File manifest, @Nullable File mapping, @NonNull File resources, @Nullable File reportFile)
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • analyze

      public void analyze() throws IOException, ParserConfigurationException, SAXException
      Specified by:
      analyze in interface com.android.build.shrinker.ResourceShrinker
      Throws:
      IOException
      ParserConfigurationException
      SAXException
    • isDryRun

      public boolean isDryRun()
    • setDryRun

      public void setDryRun(boolean dryRun)
    • isVerbose

      public boolean isVerbose()
    • setVerbose

      public void setVerbose(boolean verbose)
    • isDebug

      public boolean isDebug()
    • setDebug

      public void setDebug(boolean verbose)
    • rewriteResourcesInBundleFormat

      public void rewriteResourcesInBundleFormat(@NonNull File source, @NonNull File dest, @NonNull Map<String,String> moduleToPackageName)
      Specified by:
      rewriteResourcesInBundleFormat in interface com.android.build.shrinker.ResourceShrinker
    • rewriteResourcesInApkFormat

      public void rewriteResourcesInApkFormat(@NonNull File source, @NonNull File dest, @NonNull com.android.build.shrinker.LinkedResourcesFormat format) throws IOException
      "Removes" resources from an .ap_ file by writing it out while filtering out unused resources. This won't touch the values XML data (resources.arsc) but will remove the individual file-based resources, which is where most of the data is anyway (usually in drawable bitmaps)
      Specified by:
      rewriteResourcesInApkFormat in interface com.android.build.shrinker.ResourceShrinker
      Parameters:
      source - the .ap_ file created by aapt
      dest - a new .ap_ file with unused file-based resources removed
      format - a format BINARY/PROTO in which compiled resources are represented in archive
      Throws:
      IOException
    • emitKeepResources

      public void emitKeepResources(Path destination) throws IOException
      Writes the keep resources string to file specified by destination
      Throws:
      IOException
    • emitConfig

      public void emitConfig(Path destination) throws IOException
      Throws:
      IOException
    • removeUnused

      public void removeUnused(@Nullable File destination) throws IOException, ParserConfigurationException, SAXException
      Remove resources (already identified by analyze()).

      This task will copy all remaining used resources over from the full resource directory to a new reduced resource directory. However, it can't just delete the resources, because it has no way to tell aapt to continue to use the same id's for the resources. When we re-run aapt on the stripped resource directory, it will assign new id's to some of the resources (to fill the gaps) which means the resource id's no longer match the constants compiled into the dex files, and as a result, the app crashes at runtime.

      Therefore, it needs to preserve all id's by actually keeping all the resource names. It can still save a lot of space by making these resources tiny; e.g. all strings are set to empty, all styles, arrays and plurals are set to not contain any children, and most importantly, all file based resources like bitmaps and layouts are replaced by simple resource aliases which just point to @null.

      Parameters:
      destination - directory to copy resources into; if null, delete resources in place
      Throws:
      IOException
      ParserConfigurationException
      SAXException
    • getUnusedResourceCount

      public int getUnusedResourceCount()
      Specified by:
      getUnusedResourceCount in interface com.android.build.shrinker.ResourceShrinker