今天我们来看一款使用非常简单的由google提供的Android 路由框架:AutoService(https://github.com/google/auto/tree/master/service)
话不多说,直接撸代码
1.添加依赖
dependencies{
implementation'com.google.auto.service:auto-service:1.0-rc4'
}
2.定义接口ITest
public interface ITest {
void test();
}
3.实现接口并添加注解
import android.util.Log;
import com.google.auto.service.AutoService;
@AutoService(ITest.class)
public class Testimplements ITest{
@Override
public void test() {
Log.d("@@","sdd");
}
}
4.调用接口
ServiceLoader.load(ITest.class).iterator().next().test();
5.看结果
接下来我们看看是怎么实现的:
进入java.util.ServiceLoader 类,ServiceLoader 实现了Iterable 接口,
public final class ServiceLoader implements Iterable
看到这里,出现了两个Iterable 对象,一个是providers,一个是lookupIterator ,他们又是哪来的呢?
load的时候返回了ServiceLoader对象
public static ServiceLoader load(Class service) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return ServiceLoader.load(service, cl);
}
这里惊喜的发现了lookupIterator 的来源,LazyIterator也实现了Iterable接口
public void reload() {
providers.clear();
lookupIterator = new LazyIterator(service, loader);
}
上面我们看到ServiceLoader 进行Iterable的时候,都会优先调用providers.hasNext(), providers初始化的时候被clear了,就一定会进入lookupIterator.hasNext(),跟踪代码发现,在加载文件名为 PREFIX +service.getName()的文件,并从文件parse出一个Iterable<String>对象,fullName 和 pending 先卖个关子
接下来再看lookupIterator.next(),跟踪发现,在找Class并newCInstance一个对象,并存入providers中
看到这,揭晓fullName ,service就是我们调用ServiceLoader.load(ITest.class)传入类,fullName 就是META-INF/services/接口的类名,
private static final String PREFIX ="META-INF/services/";
总结一下:
fullName 就是META-INF/services/接口的类名,
providers就是一个存路由对象的Iterator,
pending 就是fullName文件中的实现类
我们打开APK包验证下
好了,最后提两个需要注意的点:
1.AutoService使用了APT技术,需要在build.gradle文件中添加配置
defaultConfig{
javaCompileOptions{ annotationProcessorOptions{ includeCompileClasspath =true } }
}
2.如果需要混淆,需要注意接口文件需要keep,例如
-keep public class com.demo.** {*;}