Library弱依賴打包

爲減小強依賴,運行時動態監測依賴是否存在。android

例如:內置的 HTTP client 能夠是 OkHttpClient 或者是 HttpURLConnection。前者擁有更高的性能,但須要引入 OkHttp 做爲依賴。若是用戶不肯意引入 OKHttp 的話,它將會自動用回標準庫的 HttpURLConnection。git

public final class PlacesHttpClientResolver {
  public static final PlacesHttpClient PLACES_HTTP_CLIENT;

  static {
    boolean hasOkHttp;
    
    try {
      Class.forName("com.squareup.okhttp.OkHttpClient");
      hasOkHttp = true;
    } catch (ClassNotFoundException e) {
      hasOkHttp = false;
    }

    PlacesApiJsonParser parser = JsonParserResolver.JSON_PARSER;

    PLACES_HTTP_CLIENT = hasOkHttp ? new OkHttpPlacesHttpClient(parser) : new HttpUrlConnectionMapsHttpClient(parser);
  }

  private PlacesHttpClientResolver() {
    throw new RuntimeException("No Instances!");
  }
}

 

 

參考:https://github.com/hehonghui/android-tech-frontier/blob/master/issue-33/Android%20Libraries%E7%9A%84%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86.mdgithub

相關文章
相關標籤/搜索