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

Building complex apps easily

Building complex apps easily

Building a good app is hard and you don't need to solve problems that have already been solved before. This talk covers a bunch of libraries that make your life as an Android developer easier and help you focus on implementing your actual idea and not worry about mundane stuff.

Code shown in this talk can be found here - github.com/mustafa01ali/Sherlock

Mustafa Ali

March 14, 2015
Tweet

More Decks by Mustafa Ali

Other Decks in Technology

Transcript

  1. “If I have seen farther than others, it is by

    standing on the shoulders of giants.” - Isaac Newton Mt. St. Helens Crater Rim by brookehoyer (CC-BY SA)
  2. Butter Knife View “injection” library for Android Eliminates boilerplate code

    Performs annotation-based code generation No reflection Can “inject” listeners as well jakewharton.github.io/butterknife
  3. Butter Knife Usage public class ExampleActivity extends Activity { @InjectView(R.id.title)

    TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } } public void inject(ExampleActivity a) { a.subtitle = (android.widget.TextView) a.findViewById(2130963232); a.footer = (android.widget.TextView) a.findViewById(21309685455); a.title = (android.widget.TextView) a.findViewById(21309686566); }
  4. Butter Knife Usage @OnClick(R.id.submit) public void sayHi(Button button) { button.setText("Live

    long and prosper"); } @OnClick({ R.id.door1, R.id.door2, R.id.door3 }) public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, "You win!", LENGTH_SHORT).show(); } else { Toast.makeText(this, "DIE DIE DIE!", LENGTH_SHORT).show(); } } @OnItemSelected(R.id.list_view) void onItemSelected(int position) { // Do something with the “chosen one” } @OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED) void onNothingSelected() { // Y u no select anything? }
  5. Retrofit Type-safe REST client for Android and Java Turns your

    REST API into a Java interface Each HTTP verb is represented by an annotation Requests can be both synchronous & asynchronous Automatic deserialization using GSON, Simple, etc square.github.io/retrofit
  6. Retrofit Usage public interface GoogleBooksService { @GET("/books/v1/volumes") void search(@Query("q") String

    query, Callback<SearchResults> callback); } GET https://www.googleapis.com/books/v1/volumes?q=android RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://www.googleapis.com") .setLogLevel(RestAdapter.LogLevel.FULL) .build(); GoogleBooksService service = restAdapter.create(GoogleBooksService.class);
  7. Synchronous SearchResult result = service.search(query); Retrofit Usage Asynchronous service.search(query, callback);

    private Callback<SearchResults> callback = new Callback<SearchResults>() { @Override public void success(SearchResults searchResults, Response response) { displayResults(searchResults); } @Override public void failure(RetrofitError error) { displayError(error); } };
  8. Picasso Hassle-free image downloading and caching Handles ImageView recycling automatically

    Performs download cancellations automatically Allows complex image transformations Automatic memory and disk caching square.github.io/picasso
  9. Picasso Usage Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); Picasso.with(context) .load(url) .resize(50, 50) .centerCrop() .placeholder(R.drawable.user_placeholder) .error(R.drawable.user_placeholder_error)

    .into(imageView) Picasso.with(context).load(R.drawable.landing_screen).into(imageView1); Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2); Picasso.with(context).load(new File(...)).into(imageView3);
  10. Timber Logger built on top of android.util.Log Small and extensible

    API Automatic smart tagging No logging in production, ever! jakewharton.github.io/timber
  11. Timber Usage public class SherlockApplication extends Application { @Override public

    void onCreate() { super.onCreate(); if (BuildConfig.DEBUG) { Timber.plant(new Timber.DebugTree()); } else { Timber.plant(new CrashReportingTree()); } } } Timber.i("A button with ID %s was clicked to say '%s'.", button.getId(), button.getText()); Timber.e("Search failed with error: " + error.getKind());
  12. Timber Usage private static class CrashReportingTree extends Timber.HollowTree { @Override

    public void i(String message, Object... args) { // TODO e.g., Crashlytics.log(String.format(message, args)); } @Override public void i(Throwable t, String message, Object... args) { i(message, args); // Just add to the log. } @Override public void e(String message, Object... args) { i("ERROR: " + message, args); // Just add to the log. } @Override public void e(Throwable t, String message, Object... args) { e(message, args); // TODO e.g., Crashlytics.logException(t); } }
  13. Q & A Ask me questions! Code available at -

    github.com/mustafa01ali/Sherlock