$30 off During Our Annual Pro Sale. View Details »

The art of organizing resources

The art of organizing resources

Slides from my talk at Android Makers Paris 2017
video: https://www.youtube.com/watch?v=AjSgAHZT9a0

Do you remember the last time you had to dig into strings.xml to find the right String to use? Or that you manually had to go over all drawables to find the one you needed?

Whenever we start a new project, we take a lot of care in setting up our architecture, CI, build flavors,... But do you also have a strategy to name your resources?

You should! Because the lack of XML namespaces, makes managing Android resources tedious. And causes things to grow out of control easily, especially in large projects.

So let's introduce a simple scheme that will solve your pains:
- easy lookup of any resource (autocomplete)
- logical, predictable names
- clean ordering of resources
- strongly typed resources

This talk will explain the mechanism, its advantages, limitations and provide an easy to use cheat sheet.

Jeroen Mols

April 10, 2017
Tweet

More Decks by Jeroen Mols

Other Decks in Programming

Transcript

  1. @MOLSJEROEN
    THE ART OF ORGANIZING
    RESOURCES

    View Slide

  2. @MOLSJEROEN
    @MOLSJEROEN

    View Slide

  3. DO YOU HAVE A STRATEGY TO ORGANISE
    RESOURCES?
    Jeroen Mols

    View Slide

  4. @MOLSJEROEN
    SETTING UP A NEW PROJECT
    ▸ Architecture
    ▸ Unit tests
    ▸ Build flavours
    ▸ Code style
    ▸ Continuous integration & deployment
    ▸ …

    View Slide

  5. @MOLSJEROEN
    SETTING UP A NEW PROJECT
    ▸ Architecture
    ▸ Unit tests
    ▸ Build flavours
    ▸ Code style
    ▸ Continuous integration & deployment
    ▸ …
    ▸ What about resources?

    View Slide

  6. @MOLSJEROEN
    IMPORTANCE OF RESOURCES
    Facebook Twitter Hangouts
    Layouts 3167 1128 372
    Strings 7995 3243 2982
    Drawables 4907 2136 1703
    Ids 7258 2276 1039
    Dimensions 4842 1470 636

    View Slide

  7. @MOLSJEROEN
    CHALLENGES ORGANISING RESOURCES
    ▸ No XML name spaces
    ▸ Huge amount of resources
    ▸ A lot of types: layouts, strings, drawables, ids,…
    ▸ Reuse across screens
    ▸ Tooling support

    View Slide

  8. IF SOMETHING IS WORTH KEEPING, ITS
    WORTH KEEPING WHERE YOU CAN FIND IT.
    David Allen

    View Slide

  9. @MOLSJEROEN
    GOALS
    ▸ Logical, predictable names
    ▸ Clean ordering, even in folders
    ▸ Leverage Android Studio autocomplete
    ▸ Strongly typed resources

    View Slide

  10. @MOLSJEROEN




    GENERAL RULE
    fixed set of options per resource, often Android view class
    custom part Android view subclass or “all”
    differentiate multiple elements in one screen
    precise “xdpi" or bucket “small”, optional
    ___
    ___

    View Slide

  11. @MOLSJEROEN




    GENERAL RULE
    fixed set of options per resource, often Android view class
    custom part Android view subclass or “all”
    differentiate multiple elements in one screen
    precise “xdpi" or bucket “small”, optional
    ___
    ___

    View Slide

  12. @MOLSJEROEN




    GENERAL RULE
    fixed set of options per resource, often Android view class
    custom part Android view subclass or “all”
    differentiate multiple elements in one screen
    precise “xdpi" or bucket “small”, optional
    ___
    ___

    View Slide

  13. @MOLSJEROEN




    GENERAL RULE
    fixed set of options per resource, often Android view class
    custom part Android view subclass or “all”
    differentiate multiple elements in one screen
    precise “xdpi" or bucket “small”, optional
    ___
    ___

    View Slide

  14. @MOLSJEROEN




    GENERAL RULE
    fixed set of options per resource, often Android view class
    custom part Android view subclass or “all”
    differentiate multiple elements in one screen
    precise “xdpi" or bucket “small”, optional
    ___
    ___

    View Slide

  15. @MOLSJEROEN
    ADVANTAGES
    1. Easy to use

    View Slide

  16. @MOLSJEROEN
    • Examples
    STRINGS
    - articledetail_title
    - feedback_explanation
    - feedback_namehint
    - all_done
    _

    View Slide

  17. @MOLSJEROEN
    STRINGS
    _
    @MOLSJEROEN

    View Slide

  18. @MOLSJEROEN
    ADVANTAGES
    1. Easy to use
    2. Ordering by screen

    View Slide

  19. @MOLSJEROEN
    • Examples
    DRAWABLES
    - articledetail_placeholder
    - all_infoicon
    - all_infoicon_large
    - all_infoicon_24dp
    __

    View Slide

  20. @MOLSJEROEN
    DRAWABLES
    __
    @MOLSJEROEN

    View Slide

  21. @MOLSJEROEN
    ADVANTAGES
    1. Easy to use
    2. Ordering by screen
    3. Folder organisation

    View Slide

  22. @MOLSJEROEN

    • Examples
    LAYOUTS
    activity, fragment, view, item or layout
    - activity_main.xml
    - fragment_articledetail.xml
    - view_menu.xml
    - layout_actionbar.xml
    _.XML

    View Slide

  23. @MOLSJEROEN
    LAYOUTS
    _.XML
    @MOLSJEROEN

    View Slide

  24. @MOLSJEROEN
    ADVANTAGES
    1. Easy to use
    2. Ordering by screen
    3. Folder organisation
    4. Autocomplete
    - based on where and what
    - camel casing

    View Slide

  25. @MOLSJEROEN

    • Examples
    IDS
    name of Android/Custom view class
    - tablayout_main
    - imageview_menu_profile
    - textview_articledetail_title
    __

    View Slide

  26. @MOLSJEROEN
    IDS
    __
    @MOLSJEROEN

    View Slide

  27. @MOLSJEROEN
    ADVANTAGES
    1. Easy to use
    2. Ordering by screen
    3. Folder organisation
    4. Autocomplete
    - based on where and what
    - camel casing
    5. Strong typing

    View Slide

  28. @MOLSJEROEN
    • 

    • Examples
    DIMENSIONS
    width, height, size, margin, padding, elevation, keyline or
    textsize
    - height_all_toolbar
    - keyline_all_listtext
    - textsize_all_medium
    - size_menu_icon
    ___

    View Slide

  29. @MOLSJEROEN
    DIMENSIONS
    ___
    @MOLSJEROEN

    View Slide

  30. @MOLSJEROEN
    ADVANTAGES
    1. Easy to use
    2. Ordering by screen
    3. Folder organisation
    4. Autocomplete
    - based on where and what
    - camel casing
    5. Strong typing
    6. Can be supported by tools

    View Slide

  31. @MOLSJEROEN
    CHEAT SHEET
    ANDROID RESOURCE NAMING CHEAT SHEET
    ___
    LAYOUTS
    STRINGS
    DRAWABLES
    DIMENSIONS
    _.XML
    is activity, fragment, view, item or layout
    _
    ___
    __
    “all” if reused in ≠ screens
    is width, height, size, margin, padding, elevation, keyline or textsize
    IDS
    __
    is name of Android/Custom view class
    custom part Android view subclass
    fixed set of options differentiate multiple elements in one screen always optional
    e.g. activity_main.xml
    all_done
    e.g. main_intro
    [xdp] or bucket [small]
    main_background
    e.g. all_infoicon_small
    e.g. linearlayout_main_fragmentcontainer
    e.g. keyline_all_text
    choose the right one below
    @MOLSJEROEN

    View Slide

  32. @MOLSJEROEN
    1. Easy to use
    2. Ordering by screen
    3. Folder organisation
    4. Autocomplete
    5. Strong typing
    6. Can be supported by tools
    CONCLUSION
    ___

    View Slide

  33. @MOLSJEROEN
    REFERENCES AND IMAGE CREDITS
    • Blogpost http://jeroenmols.com/blog/2016/03/07/resourcenaming
    • Source code https://github.com/JeroenMols/ResourceNamingExample
    • Welcome image credit https://pixabay.com/p-642689/

    View Slide

  34. @MOLSJEROEN
    MANY THANKS
    ▸ Jeroen Mols (Belgium)
    ▸ @MolsJeroen
    ▸ http://jeroenmols.com/blog

    View Slide