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

Android Dependencies You Can Depend On

Android Dependencies You Can Depend On

Daniel Lew

March 21, 2015
Tweet

More Decks by Daniel Lew

Other Decks in Programming

Transcript

  1. Why Libraries? • Stand on the shoulders of giants •

    Hit the ground running • Easier than ever to use
  2. Retrofit • …Because a lot of people use REST •

    …Because REST is easy to understand
  3. Define an Interface http://company.com/api/public/cards?id=someId public interface MyService {
 
 @GET("/api/{visibility}/cards")


    List<Card> getCards(@Path("visibility") String visibility, 
 @Query("id") String id);
 
 }
  4. JSON vs. Object • JSON {
 "id": "someId",
 "data": "Some

    data"
 } • Class public class Card {
 
 private String id;
 
 private String data;
 
 }
  5. Without GSON String jsonStr = /* …what we had from

    before… */;
 try {
 JSONObject jsonObject = new JSONObject(jsonStr);
 String id = jsonObject.getString("id");
 String data = jsonObject.getString("data");
 Card card = new Card(id, data);
 } catch (JSONException e) {
 e.printStackTrace();
 }
  6. With GSON String jsonStr = /* ...whatever you get from

    Retrofit... */;
 Gson gson = new Gson();
 Card card = gson.fromJson(jsonStr, Card.class);
  7. Customizable • Can handle differently named fields: public class Card

    {
 
 @SerializedName("theId")
 private String id;
 
 @SerializedName("theData")
 private String data;
 
 } • Custom deserializers (type adapters)
  8. Retrofit with GSON RestAdapter restAdapter = new RestAdapter.Builder()
 .setEndpoint("http://company.com")
 .setConverter(new

    GsonConverter(new Gson()))
 .build();
 
 MyService service = restAdapter.create(MyService.class);
  9. OkHttp • Consistent HTTP implementation • Better HTTP implementation •

    Easier HTTP implementation • Drives other libraries
  10. OkHttp + Retrofit • Add client to RestAdapter RestAdapter restAdapter

    = new RestAdapter.Builder()
 .setEndpoint("http://company.com")
 .setConverter(new GsonConverter(new Gson()))
 .setClient(new OkClient())
 .build();
  11. OkHttp + Picasso Picasso picasso = new Picasso.Builder()
 .downloader(new OkHttpDownloader())

    .build(); 
 picasso.load("http://path.to/image.png")
 .into(someImageView);
  12. BAD • DO NOT create new instances each time! RestAdapter

    restAdapter = new RestAdapter.Builder()
 .setEndpoint("http://company.com")
 .setConverter(new GsonConverter(new Gson()))
 .setClient(new OkClient())
 .build(); 
 MyService service = restAdapter.create(MyService.class); Picasso picasso = new Picasso.Builder()
 .downloader(new OkHttpDownloader()).build();
  13. It’s Not Complex • Without dependency injection public class SomeClass

    {
 private SomeService service;
 
 public SomeClass() {
 this.service = new SomeService();
 }
 } • With dependency injection public class SomeClass {
 private SomeService service;
 
 public SomeClass(SomeService service) {
 this.service = service;
 }
 }
  14. Dagger Modules @Module
 public class AppModule {
 
 @Provides @Singleton

    Gson provideGson() {
 return new Gson();
 }
 
 @Provides @Singleton Client provideClient() {
 return new OkClient();
 }
 
 @Provides @Singleton RestAdapter provideRestAdapter(Gson gson,
 Client client) {
 return new RestAdapter.Builder()
 .setConverter(new GsonConverter(gson))
 .setClient(client)
 .build();
 }
 
 @Provides @Singleton MyService provideMyService(RestAdapter restAdapter) {
 return restAdapter.create(MyService.class);
 }
 }
  15. Injecting Modules public class SomeClass {
 
 @Inject MyService myService;


    
 public SomeClass() {
 // In real life, ObjectGraph should be reused
 ObjectGraph objectGraph = ObjectGraph.create(new AppModule());
 objectGraph.inject(this);
 }
 
 }
  16. RxJava • Reactive framework • Less coding • Easy error

    handling • Easy concurrency • Just plain fun • …But not easy to understand at first
  17. Operators Observable.just("1", "2", "3")
 .map(new Func1<String, String>() {
 @Override public

    String call(String s) {
 return "item: " + s;
 }
 })
 .subscribe(new Action1<String>() {
 @Override public void call(String s) {
 System.out.println(s);
 }
 });
  18. Schedulers Observable.just("1", "2", "3")
 .map(new Func1<String, String>() {
 @Override public

    String call(String s) {
 return "item: " + s;
 }
 })
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new Action1<String>() {
 @Override
 public void call(String s) {
 System.out.println(s);
 }
 });
  19. Error Handling Observable.just("1", "2", "3")
 .map(new Func1<String, String>() {
 @Override


    public String call(String s) {
 return "item: " + s;
 }
 })
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 new Action1<String>() {
 @Override public void call(String s) {
 System.out.println(s);
 }
 },
 new Action1<Throwable>() {
 @Override public void call(Throwable throwable) {
 System.out.println("Something went wrong!");
 }
 });
  20. u2020 • Great sample for (most) of basic stack •

    https://github.com/JakeWharton/u2020/
  21. Hugo • Before: public void doSomething(String input) {
 Log.i("SomeTagYouMadeUp", "doSomething("

    + input + ")");
 // Shockingly, this does something
 Log.i("SomeTagYouMadeUp", "doSomething finished");
 } • After: @DebugLog
 public void doSomething(String input) {
 // Shockingly, this does something
 }

  22. Fun Libraries • Calligraphy - Automatic fonts! • Gradle retrolambda

    - Lambdas in Android! • Butterknife - Annotated views! • RoundedImageView - Easy rounded corners! • Mockito - Mocked objects for testing! • Victor - SVGs as resources!
  23. Google • A search engine: http://google.com • Type in what

    you want • Take a gamble with “I’m feeling lucky”