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

Reusable Libraries

Reusable Libraries

How to make reusable code work on Android.

Ryan Harter

April 10, 2015
Tweet

More Decks by Ryan Harter

Other Decks in Programming

Transcript

  1. Reusable Libraries
    Ryan Harter
    @rharter
    +RyanHarter

    View Slide

  2. My Apps
    Fragment Shift

    View Slide

  3. My Apps
    Fragment Shift

    View Slide

  4. My Apps
    Fragment Shift

    View Slide

  5. Steps to make libraries work
    • Identify good candidates
    • Limit scope and requirements
    • Design a good API
    • Document and Communicate
    • Treat it like a client project

    View Slide

  6. Identify Good Candidates

    View Slide

  7. Identify Good Candidates

    View Slide

  8. Identify Good Candidates
    • It’s not a library if it’s only used once

    View Slide

  9. Identify Good Candidates
    • It’s not a library if it’s only used once
    • Don’t reinvent the wheel

    View Slide

  10. Identify Good Candidates
    • It’s not a library if it’s only used once
    • Don’t reinvent the wheel
    • Two Categories

    View Slide

  11. Identify Good Candidates
    • It’s not a library if it’s only used once
    • Don’t reinvent the wheel
    • Two Categories
    • Utility

    View Slide

  12. Identify Good Candidates
    • It’s not a library if it’s only used once
    • Don’t reinvent the wheel
    • Two Categories
    • Utility
    • UI

    View Slide

  13. Identify Good Candidates

    View Slide

  14. Identify Good Candidates
    • Google for libraries

    View Slide

  15. Identify Good Candidates
    • Google for libraries
    • Check Android Arsenal

    View Slide

  16. Identify Good Candidates
    • Google for libraries
    • Check Android Arsenal
    • Look at what other apps use

    View Slide

  17. Identify Good Candidates
    • Google for libraries
    • Check Android Arsenal
    • Look at what other apps use
    • Be Confident

    View Slide

  18. Identify Good Candidates
    • Google for libraries
    • Check Android Arsenal
    • Look at what other apps use
    • Be Confident
    • Count Stars

    View Slide

  19. Identify Good Candidates
    • Google for libraries
    • Check Android Arsenal
    • Look at what other apps use
    • Be Confident
    • Count Stars
    • Inspect Source

    View Slide

  20. Limit Scope and Requirements

    View Slide

  21. Limit Scope and Requirements

    View Slide

  22. Limit Scope and Requirements
    • Choose a single problem to solve

    View Slide

  23. Limit Scope and Requirements
    • Choose a single problem to solve
    • Picasso - Efficient image loading and caching

    View Slide

  24. Limit Scope and Requirements
    • Choose a single problem to solve
    • Picasso - Efficient image loading and caching
    • Retrofit - Easy REST API access

    View Slide

  25. Limit Scope and Requirements
    • Choose a single problem to solve
    • Picasso - Efficient image loading and caching
    • Retrofit - Easy REST API access
    • Avoid “Apache Syndrome”

    View Slide

  26. Limit Scope and Requirements
    • Choose a single problem to solve
    • Picasso - Efficient image loading and caching
    • Retrofit - Easy REST API access
    • Avoid “Apache Syndrome”
    • Every Apache library depends on every other Apache library

    View Slide

  27. Limit Scope and Requirements
    • Library mission statement
    • One sentence
    • What the library does

    View Slide

  28. Limit Scope and Requirements
    • Library mission statement
    • One sentence
    • What the library does
    “Allow the user to load an image from any source.”

    View Slide

  29. Design a Good API

    View Slide

  30. Design a Good API

    View Slide

  31. Design a Good API
    • Who is your user?

    View Slide

  32. Design a Good API
    • Who is your user?
    • If user needs to mess with code, it’s not a library

    View Slide

  33. Design a Good API
    • Who is your user?
    • If user needs to mess with code, it’s not a library
    • Design from the outside in

    View Slide

  34. Design a Good API
    • Who is your user?
    • If user needs to mess with code, it’s not a library
    • Design from the outside in
    • Package your library

    View Slide

  35. API Strategies - Utility
    Requirements: Persist filter state to support undo/redo.
    public class HistoryManager {
    public T undo();
    public T redo();
    public void push(T state);
    public void clear();
    }

    View Slide

  36. API Strategies - Superclass
    Fragment Shift

    View Slide

  37. API Strategies - Superclass
    public class BaseExportActivity extends Activity {
    /**
    * Returns a list of preferred packages which appear above the
    * divider with a slightly larger icon than the rest.
    *
    * @return the package ids of the preferred packages.
    */
    protected List getPreferredPackages() {
    return DEFAULT_PREFERRED_PACKAGES;
    }
    /**
    * @return the public folder name to save files to.
    */
    protected File getSaveFolder() {
    return new File(getPublicPicturesDirectory(), "Pixite");
    }
    /**
    * Allows the AppInfo to be updated for things like Refragment or
    * Refilter. Default implementation does nothing.
    *
    * @param info The app info to update.
    */
    protected void updateAppInfo(AppInfo info) {
    }
    }

    View Slide

  38. API Strategies - Isolated Activity
    Fragment Shift

    View Slide

  39. API Strategies - Isolated Activity

    View Slide

  40. API Strategies - Isolated Activity
    Intent i = new Intent(getActivity(), InspirationActivity.class);
    i.putExtra(InspirationActivity.EXTRA_USER_ID, "1234567890");
    i.putExtra(InspirationActivity.EXTRA_ACCESS_TOKEN, "super_secret_access_token");
    i.putExtra(InspirationActivity.EXTRA_SKIP_TAG, "shiftpromo");
    i.putExtra(InspirationActivity.EXTRA_USERNAME, "shift_by_pixite");
    startActivity(i);

    View Slide

  41. API Strategies - Isolated Activity
    Intent i = new Intent(getActivity(), InspirationActivity.class);
    i.putExtra(InspirationActivity.EXTRA_USER_ID, "1234567890");
    i.putExtra(InspirationActivity.EXTRA_ACCESS_TOKEN, "super_secret_access_token");
    i.putExtra(InspirationActivity.EXTRA_SKIP_TAG, "shiftpromo");
    i.putExtra(InspirationActivity.EXTRA_USERNAME, "shift_by_pixite");
    startActivity(i);
    <br/>...<br/><item name="inspirationActivityStyle">@style/InspirationActivityStyle</item><br/><item name="inspirationTextStyle">@style/TextAppearance.AppCompat.Inverse</item><br/><item name="inspirationFollowButtonStyle">@style/Widget.InspirationButton.Follow</item><br/><item name="inspirationMoreButtonStyle">@style/Widget.InspirationButton.More</item><br/><item name="inspirationRowStyle">@style/Widget.InspirationRow</item><br/><item name="inspirationRowTextStyle">@style/TextAppearance.InspirationRow</item><br/>

    View Slide

  42. API Strategies - Hybrid
    Intent i = new Intent(this, ImageChooserActivity.class);
    startActivityForResult(i, REQUEST_CHOOSE_PHOTO);

    View Slide

  43. Distribution
    • Open Source (jcenter, Maven Central)
    • Private Maven Repo
    • Git Repository

    View Slide

  44. Distribution - gradle-git-repo
    uploadArchives {
    repositories {
    mavenDeployer {
    repository(url: '[email protected]:rharter/maven-repo.git/releases')
    snapshotRepository(url: ‘[email protected]:rharter/maven-repo.git/snapshots')
    // standard pom settings
    }
    }
    }
    https://github.com/rharter/gradle-git-repo

    View Slide

  45. Document and Communicate

    View Slide

  46. Document and Communicate

    View Slide

  47. Document and Communicate
    • If a library is only used once, is it a library?

    View Slide

  48. Document and Communicate
    • If a library is only used once, is it a library?
    • Communicate outside of engineering

    View Slide

  49. Document and Communicate
    • If a library is only used once, is it a library?
    • Communicate outside of engineering
    • Sales needs to know what to sell

    View Slide

  50. Document and Communicate
    • If a library is only used once, is it a library?
    • Communicate outside of engineering
    • Sales needs to know what to sell
    • Design needs to know components and limitations

    View Slide

  51. Document and Communicate
    • If a library is only used once, is it a library?
    • Communicate outside of engineering
    • Sales needs to know what to sell
    • Design needs to know components and limitations
    • Javadoc for Engineering, Wiki for others

    View Slide

  52. Manage the Project

    View Slide

  53. Manage the Project

    View Slide

  54. Manage the Project
    • Treat internal libraries like a client project

    View Slide

  55. Manage the Project
    • Treat internal libraries like a client project
    • Dedicate resources

    View Slide

  56. Manage the Project
    • Treat internal libraries like a client project
    • Dedicate resources
    • Manage requests

    View Slide

  57. Manage the Project
    • Treat internal libraries like a client project
    • Dedicate resources
    • Manage requests
    • Define requirements

    View Slide

  58. Manage the Project
    • Treat internal libraries like a client project
    • Dedicate resources
    • Manage requests
    • Define requirements
    • Enforce versioned release cycle

    View Slide

  59. Reusable Libraries
    @rharter
    +RyanHarter
    http://ryanharter.com

    View Slide