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

Developing an Android library

Dino Kovač
September 17, 2016

Developing an Android library

Slides from my barcamp talk at Droidcon Vienna 2016.

Dino Kovač

September 17, 2016
Tweet

More Decks by Dino Kovač

Other Decks in Programming

Transcript

  1. WHY A LIBRARY? • DRY - don’t repeat yourself •

    easier than copy/pasting classes
  2. WHY AN OPEN SOURCE LIBRARY? • saving others time •

    useful contributions from community • eternal fame!
  3. DOES IT MAKES SENSE TO BUILD IT? • is there

    a real need? • any existing libraries?
  4. PICK A GOOD NAME • one that doesn’t already exist

    • should make sense • bonus points if it’s amusing • example: • HTTParty - Makes http fun again!
  5. MIND THE API • primary way users interact with your

    lib • make it easy to use • make it robust (avoid footguns!)
  6. HTTPURLCONNECTION HttpURLConnection urlConnection = null;
 try {
 URL url =

    new URL("https://api.github.com/markdown/raw");
 connection = (HttpURLConnection) url.openConnection();
 connection.setDoOutput(true);
 connection.setChunkedStreamingMode(0);
 
 OutputStream out = new BufferedOutputStream(connection.getOutputStream());
 writeStream(out);
 
 InputStream in = new BufferedInputStream(connection.getInputStream());
 readStream(in);
 } catch (IOException e) {
 // handle this
 } finally {
 if (connection != null) {
 connection.disconnect();
 }
 }
  7. OKHTTP Request request = new Request.Builder()
 .url("https://api.github.com/markdown/raw")
 .post(RequestBody.create(MediaType.parse("text/plain"), "post body"))


    .build();
 
 try {
 Response response = client.newCall(request).execute();
 } catch (IOException e) {
 // handle this
 }
  8. FAIL FAST public Builder post(RequestBody body) {
 return method("POST", body);


    }
 
 public Builder method(String method, RequestBody body) {
 if (method == null || method.length() == 0) {
 throw new IllegalArgumentException("method == null || method.length() == 0");
 }
 if (body != null && !HttpMethod.permitsRequestBody(method)) {
 throw new IllegalArgumentException("method " + method + " must not have a request body.");
 }
 if (body == null && HttpMethod.requiresRequestBody(method)) {
 throw new IllegalArgumentException("method " + method + " must have a request body.");
 }
 this.method = method;
 this.body = body;
 return this;
 }
  9. APP RESOURCES LIBRARY RESOURCES MERGED RESOURCES @string/app_name (en) @string/app_name (fr)

    @string/app_name (de) @string/app_name (en) @string/app_name (fr) @string/app_name (de) @string/app_name
  10. WRITE THE DOCS • what does it do? • how

    do I use it? • maybe an example app?
  11. Any questions? [email protected] @DINO_BLACKSMITH Visit infinum.co or find us on

    social networks: infinum.co infinumco infinumco infinum