Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Remove Unused Resources

Remove Unused Resources

potatotips #51

konifar

May 23, 2018
Tweet

More Decks by konifar

Other Decks in Programming

Transcript

  1. Lint "unused resources" is useless! It sometimes (or always) tells

    a lie... Especially in the case of using DataBinding 3
  2. Keep clean Android project It's enough to use gradle option

    shrinkResources and minifyEnabled to shrink apk. But unused resources bother you to inspect code and to search le on project tree in Android Studio. It's better to remove unused resources and keep clean your project to write code easy! 4
  3. Features Easy to remove unused resources by simple gradle command.

    Customize extra/exclude les con guration. Support DataBinding & Multi module project. Integrate with CI to send PullRequest automatically. https://gist.github.com/konifar/2f793dd5c4f3064 aad8d757ebb7b093f 6
  4. Supported resources - (1) |--res |--anim | |--*.xml |--animator |

    |--*.xml |--drawable* // drawable-v21 as well | |--*.xml | |--*.png | |--*.jpg | |--*.9.png // 9-patch |--layout* | |--*.xml |--menu | |--*.xml |--mipmap* | |--*.xml | |--*.png 7
  5. Supported resources - (2) |--res |--values* // values-ja, values-v23 as

    well |--attrs*.xml |--bools*.xml |--colors*.xml |--dimens*.xml |--floats*.xml |--ids*.xml |--integers*.xml |--strings*.xml |--styles*.xml |--themes*.xml 8
  6. Simple usage Just run gradle command! $ ./gradlew removeUnusedResources ...

    [layout] ======== Start layout checking ======== [layout] example [layout] Remove unused_layout.xml [layout] Complete to remove files. [layout] example-module [layout] No unused layout files. [layout] plugin [menu] ======== Start menu checking ======== ... 9
  7. Option 1 : extraRemovers If you want to check unsupported

    resource le, you can add custom remover settings in app/build.gradle . 10
  8. Option 1 : extraRemovers Ex) text_appearance.xml which contains text_appearance's <style>

    You can add custom remover like this. unusedResourcesRemover { extraRemovers = [ createXmlValueRemover("text_appearance", "style", "style", "style") ] } 11
  9. createXmlValueRemover() Name Description leType Pre x of the le name

    like font , text_appearance resourceName Name to check its existence like @ string /app_name, $. string /app_name tagName Name to extract value from xml like < string name="app_name"> searchType Search regex pattern type. style , drawable , layout , default are supported. 12
  10. Option 2 : exludeNames If you want to skip checking

    some le, you can add white list to excludeNames setting. resources.getIdentifier("img_welcome_${number}", "drawable", context.getPackageName()) unusedResourcesRemover { excludeNames = [ "img_welcome_", // img_welcome.*.xml is never checked. ] } 13
  11. 16

  12. 17

  13. 18