Developing an
Android library
DINO KOVAČ
01WHY?
WHY A LIBRARY?
• DRY - don’t repeat yourself
• easier than copy/pasting classes
WHY AN OPEN SOURCE LIBRARY?
• saving others time
• useful contributions from community
• eternal fame!
02WHAT?
DOES IT MAKES SENSE TO BUILD IT?
• is there a real need?
• any existing libraries?
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!
03HOW?
MIND THE API
• primary way users interact with your lib
• make it easy to use
• make it robust (avoid footguns!)
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();

}

}
HTTPURLCONNECTION
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

}
FAIL FAST
• as much validation as reasonably possible
• fail as fast as possible
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;

}
DON’T EXPOSE THE INTERNALS
• package-protected classes
• private or protected methods
WRITE THE DOCS
• what does it do?
• how do I use it?
• maybe an example app?
Any questions?
DINO@INFINUM.CO
@DINO_BLACKSMITH
Visit infinum.co or find us on social networks:
infinum.co infinumco infinumco infinum

Infinum Android Talks #17 - Developing an Android library by Dino Kovac

  • 1.
  • 2.
  • 3.
    WHY A LIBRARY? •DRY - don’t repeat yourself • easier than copy/pasting classes
  • 4.
    WHY AN OPENSOURCE LIBRARY? • saving others time • useful contributions from community • eternal fame!
  • 5.
  • 6.
    DOES IT MAKESSENSE TO BUILD IT? • is there a real need? • any existing libraries?
  • 7.
    PICK A GOODNAME • one that doesn’t already exist • should make sense • bonus points if it’s amusing • example: • HTTParty - Makes http fun again!
  • 8.
  • 9.
    MIND THE API •primary way users interact with your lib • make it easy to use • make it robust (avoid footguns!)
  • 10.
    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();
 }
 }
  • 11.
  • 12.
    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
 }
  • 13.
    FAIL FAST • asmuch validation as reasonably possible • fail as fast as possible
  • 14.
    FAIL FAST public Builderpost(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;
 }
  • 15.
    DON’T EXPOSE THEINTERNALS • package-protected classes • private or protected methods
  • 16.
    WRITE THE DOCS •what does it do? • how do I use it? • maybe an example app?
  • 17.
    Any questions? DINO@INFINUM.CO @DINO_BLACKSMITH Visit infinum.coor find us on social networks: infinum.co infinumco infinumco infinum