Reusable Libraries

Reusable Libraries

How to make reusable code work on Android.

Ryan Harter

April 10, 2015

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

59 slides
  1. 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
  2. Identify Good Candidates • It’s not a library if it’s

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

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

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

    only used once • Don’t reinvent the wheel • Two Categories • Utility • UI
  6. Identify Good Candidates • Google for libraries • Check Android

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

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

    Arsenal • Look at what other apps use • Be Confident • Count Stars • Inspect Source
  9. Limit Scope and Requirements • Choose a single problem to

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

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

    solve • Picasso - Efficient image loading and caching • Retrofit - Easy REST API access • Avoid “Apache Syndrome”
  12. 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
  13. Limit Scope and Requirements • Library mission statement • One

    sentence • What the library does “Allow the user to load an image from any source.”
  14. Design a Good API • Who is your user? •

    If user needs to mess with code, it’s not a library
  15. 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
  16. 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
  17. API Strategies - Utility Requirements: Persist filter state to support

    undo/redo. public class HistoryManager<T> { public T undo(); public T redo(); public void push(T state); public void clear(); }
  18. 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<String> 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) { } }
  19. 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);
  20. 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); <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> ... <item name="inspirationActivityStyle">@style/InspirationActivityStyle</item> <item name="inspirationTextStyle">@style/TextAppearance.AppCompat.Inverse</item> <item name="inspirationFollowButtonStyle">@style/Widget.InspirationButton.Follow</item> <item name="inspirationMoreButtonStyle">@style/Widget.InspirationButton.More</item> <item name="inspirationRowStyle">@style/Widget.InspirationRow</item> <item name="inspirationRowTextStyle">@style/TextAppearance.InspirationRow</item> </style>
  21. 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
  22. Document and Communicate • If a library is only used

    once, is it a library? • Communicate outside of engineering
  23. 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
  24. 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
  25. 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
  26. Manage the Project • Treat internal libraries like a client

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

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

    project • Dedicate resources • Manage requests • Define requirements • Enforce versioned release cycle

More Decks by Ryan Harter

Other Decks in Programming