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

相关文章
相关标签/搜索