public static < S > S loadService ( Class < S > serviceInterface , ClassLoader classLoader ) { Iterator < S > services = ServiceLoader . load ( serviceInterface , classLoader ) . iterator ( ) ; if ( services . hasNext ( ) ) { return services . next ( ) ; } // although not official, Android does not support ServiceLoader // there is no way to insert service descriptor files into apk META-INF // as an workaround uses a hard coded 'services' package to store service descriptors try { String serviceDescriptor = getResourceAsString ( "/services/" + serviceInterface . getName ( ) ) ; return newInstance ( serviceDescriptor ) ; } catch ( Throwable e ) { // log.dump("Service not found", e); } return null ; }
Load service of requested interface using given class loader . Throws exception if service implementation not found on run - time .