Android元件化最佳實踐 ARetrofit原理
Hello,各位偉大的程式猿們,提前給各位拜年了,祝大家在新的一年收穫滿滿、掙錢多多、豬年吉祥。
開源ARetrofit大約半年左右的時間,沒有任何推廣和介紹,今天一看也有160+的stars了,這裡非常感謝大家的支援。趁著年關降至,也想寫一點東西來介紹一下這個框架。無論是ARetrofit的使用者也好,還是對原始碼感興趣的同學也好,希望能從這篇文章中有所收穫。
簡介
ARetrofit是一款針對Android元件之間通訊的框架,實現元件之間解耦的同時還可以通訊。
原始碼連結
歡迎star、issues、fork
元件化
Android元件化已經不是一個新鮮的概念了,出來了已經有很長一段時間了,大家可以自行Google,可以看到一堆相關的文章。
簡單的來說,所謂的元件就是Android Studio中的Module,每一個Module都遵循高內聚的原則,通過ARetrofit來實現無耦合的程式碼結構,如下圖:

元件化結構.png
每一個Module可單獨作為一個project執行,而打包到整體時Module之間的通訊通過ARetrofit完成。
ARetrofit原理
講原理之前,我想先說說為什麼要ARetrofit。開發ARetrofit這個專案的思路來源其實是Retrofit,Retrofit是Square公司開發的一款針對Android網路請求的框架,這裡不對Retrofit展開來講。主要是Retrofit框架使用非常多的設計模式,可以說Retrofit這個開源專案將Java的設計模式運用到了極致,當然最終提供的API也是非常簡潔的。如此簡潔的API,使得我們APP中的網路模組實現變得非常輕鬆,並且維護起來也很舒服。因此我覺得有必要將Android元件之間的通訊也變得輕鬆,使用者可以優雅的通過簡潔的API就可以實現通訊,更重要的是維護起來也非常的舒服。
ARetrofit基本原理可以簡化為下圖所示:

基本原理圖.png
- 通過註解宣告需要通訊的Activity/Fragment或者Class
- 每一個module通過annotationProcessor在編譯時生成待注入的RouteInject的實現類和AInterceptorInject的實現類。
這一步在執行app[build]時會輸出日誌,可以直觀的看到,如下圖所示:
注: AInjecton::Compiler >>> Apt interceptor Processor start... <<< 注: AInjecton::Compiler enclosindClass = null 注: AInjecton::Compiler value = 3 注: AInjecton::Compiler auto generate class = com$$sjtu$$yifei$$eCGVmTMvXG$$AInterceptorInject 注: AInjecton::Compiler add path= 3 and class= LoginInterceptor .... 注: AInjecton::Compiler >>> Apt route Processor start... <<< 注: AInjecton::Compiler enclosindClass = null 注: AInjecton::Compiler value = /login-module/ILoginProviderImpl 注: AInjecton::Compiler enclosindClass = null 注: AInjecton::Compiler value = /login-module/LoginActivity 注: AInjecton::Compiler enclosindClass = null 注: AInjecton::Compiler value = /login-module/Test2Activity 注: AInjecton::Compiler enclosindClass = null 注: AInjecton::Compiler value = /login-module/TestFragment 注: AInjecton::Compiler auto generate class = com$$sjtu$$yifei$$VWpdxWEuUx$$RouteInject 注: AInjecton::Compiler add path= /login-module/TestFragment and class= null 注: AInjecton::Compiler add path= /login-module/LoginActivity and class= null 注: AInjecton::Compiler add path= /login-module/Test2Activity and class= null 注: AInjecton::Compiler add path= /login-module/ILoginProviderImpl and class= null 注: AInjecton::Compiler >>> Apt route Processor succeed <<<
- 將編譯時生成的類注入到RouterRegister中,這個類主要用於維護路由表和攔截器,對應的[build]日誌如下:
TransformPluginLaunch >>> ========== Transform scan start =========== TransformPluginLaunch >>> ========== Transform scan end cost 0.238 secs and start inserting =========== TransformPluginLaunch >>> Inserting code to jar >> /Users/yifei/as_workspace/ARetrofit/app/build/intermediates/transforms/TransformPluginLaunch/release/8.jar TransformPluginLaunch >>> to class >> com/sjtu/yifei/route/RouteRegister.class InjectClassVisitor >>> inject to class: InjectClassVisitor >>> com/sjtu/yifei/route/RouteRegister{ InjectClassVisitor >>>public *** init() { InjectClassVisitor >>>register("com.sjtu.yifei.FBQWNfbTpY.com$$sjtu$$yifei$$FBQWNfbTpY$$RouteInject") InjectClassVisitor >>>register("com.sjtu.yifei.klBxerzbYV.com$$sjtu$$yifei$$klBxerzbYV$$RouteInject") InjectClassVisitor >>>register("com.sjtu.yifei.JmhcMMUhkR.com$$sjtu$$yifei$$JmhcMMUhkR$$RouteInject") InjectClassVisitor >>>register("com.sjtu.yifei.fpyxYyTCRm.com$$sjtu$$yifei$$fpyxYyTCRm$$AInterceptorInject") InjectClassVisitor >>>} InjectClassVisitor >>> } TransformPluginLaunch >>> ========== Transform insert cost 0.017 secs end ===========
- Routerfit.register(Class<T> service) 這一步主要是通過動態代理模式實現介面中宣告的服務。
前面講的是整體的框架設計思想,便於讀者從全域性的覺得來理解ARetrofit的框架的架構。接下來,將待大家個個擊破上面提到的annotationProcessor、 transform在專案中如何使用,以及動態代理、攔截器功能的實現等細節。
一、annotationProcessor生成程式碼
annotationProcessor(註解處理器)是javac內建的一個用於編譯時掃描和處理註解(Annotation)的工具。簡單的說,在原始碼編譯階段,通過註解處理器,我們可以獲取原始檔內註解(Annotation)相關內容。Android Gradle 2.2 及以上版本提供annotationProcessor的外掛。
在ARetrofit中annotationProcessor對應的module是auto-complier,在使用annotationProcessor之前首先需要宣告好註解。關於註解不太瞭解或者遺忘的同學可直接參考我之前寫的Java註解這篇文章,本專案中宣告的註解在auto-annotation這個module中,主要有:
- @Extra 路由引數
- @Flags intent flags
- @Go 路由路徑key
- @Interceptor 宣告自定義攔截器
- @RequestCode 路由引數
- @Route路由
- @Uri
- @IMethod 用於標記註冊程式碼將插入到此方法中(transform中使用)
- @Inject 用於標記需要被注入類,最近都將插入到標記了#com.sjtu.yifei.annotation.IMethod的方法中(transform中使用)
建立自定義的註解處理器,具體使用方法可參考 利用註解動態生成程式碼 ,本專案中的註解處理器如下所示:
//這是用來註冊註解處理器要處理的原始碼版本。 @SupportedSourceVersion(SourceVersion.RELEASE_8) //這個註解用來註冊註解處理器要處理的註解型別。有效值為完全限定名(就是帶所在包名和路徑的類全名 @SupportedAnnotationTypes({ANNOTATION_ROUTE, ANNOTATION_GO}) //來註解這個處理器,可以自動生成配置資訊 @AutoService(Processor.class) public class IProcessor extends AbstractProcessor { }
生成程式碼的關鍵部分在 GenerateAInterceptorInjectImpl
和 GenerateRouteInjectImpl
中,以下貼出關鍵程式碼:
public void generateAInterceptorInjectImpl(String pkName) { try { String name = pkName.replace(".",DECOLLATOR) + SUFFIX; logger.info(String.format("auto generate class = %s", name)); TypeSpec.Builder builder = TypeSpec.classBuilder(name) .addModifiers(Modifier.PUBLIC) .addAnnotation(Inject.class) .addSuperinterface(AInterceptorInject.class); ClassName hashMap = ClassName.get("java.util", "HashMap"); //Map<String, Class<?>> TypeName wildcard = WildcardTypeName.subtypeOf(Object.class); TypeName classOfAny = ParameterizedTypeName.get(ClassName.get(Class.class), wildcard); TypeName string = ClassName.get(Integer.class); TypeName map = ParameterizedTypeName.get(ClassName.get(Map.class), string, classOfAny); MethodSpec.Builder injectBuilder = MethodSpec.methodBuilder("getAInterceptors") .addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class) .returns(map) .addStatement("$T interceptorMap = new $T<>()", map, hashMap); for (Map.Entry<Integer, ClassName> entry : interceptorMap.entrySet()) { logger.info("add path= " + entry.getKey() + " and class= " + entry.getValue().simpleName()); injectBuilder.addStatement("interceptorMap.put($L, $T.class)", entry.getKey(), entry.getValue()); } injectBuilder.addStatement("return interceptorMap"); builder.addMethod(injectBuilder.build()); JavaFile javaFile = JavaFile.builder(pkName, builder.build()) .build(); javaFile.writeTo(filer); } catch (Exception e) { e.printStackTrace(); } } public void generateRouteInjectImpl(String pkName) { try { String name = pkName.replace(".",DECOLLATOR) + SUFFIX; logger.info(String.format("auto generate class = %s", name)); TypeSpec.Builder builder = TypeSpec.classBuilder(name) .addModifiers(Modifier.PUBLIC) .addAnnotation(Inject.class) .addSuperinterface(RouteInject.class); ClassName hashMap = ClassName.get("java.util", "HashMap"); //Map<String, String> TypeName wildcard = WildcardTypeName.subtypeOf(Object.class); TypeName classOfAny = ParameterizedTypeName.get(ClassName.get(Class.class), wildcard); TypeName string = ClassName.get(String.class); TypeName map = ParameterizedTypeName.get(ClassName.get(Map.class), string, classOfAny); MethodSpec.Builder injectBuilder = MethodSpec.methodBuilder("getRouteMap") .addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class) .returns(map) .addStatement("$T routMap = new $T<>()", map, hashMap); for (Map.Entry<String, ClassName> entry : routMap.entrySet()) { logger.info("add path= " + entry.getKey() + " and class= " + entry.getValue().enclosingClassName()); injectBuilder.addStatement("routMap.put($S, $T.class)", entry.getKey(), entry.getValue()); } injectBuilder.addStatement("return routMap"); builder.addMethod(injectBuilder.build()); JavaFile javaFile = JavaFile.builder(pkName, builder.build()) .build(); javaFile.writeTo(filer); } catch (Exception e) { e.printStackTrace(); } }
二、Transform
Android Gradle 工具在 1.5.0 版本後提供了 Transfrom API, 允許第三方 Plugin在打包dex檔案之前的編譯過程中操作 .class 檔案。這一部分面向高階Android工程師的,面向位元組碼程式設計,普通工程師可不做了解。
寫到這裡也許有人會有這樣一個疑問,既然annotationProcessor這麼好用為什麼還有Transform面向位元組碼注入呢?這裡需要解釋以下,annotationProcessor具有侷限性,annotationProcessor只能掃描當前module下的程式碼,切對於第三方的jar、aar檔案都掃描不到。而Transform就沒有這樣的侷限性,在打包dex檔案之前的編譯過程中操作.class 檔案。
關於Transfrom API在Android Studio中如何使用可以參考 Transform API — a real world example ,順便提供一下 位元組碼指令 方便我們讀懂ASM。
本專案中的Transform外掛在 auto-inject
module中,實現原始碼 TransformPluginLaunch
如下,貼出關鍵部分:
/** * * 標準transform的格式,一般實現transform可以直接拷貝一份重新命名即可 * * 兩處todo實現自己的位元組碼增強/優化操作 */ class TransformPluginLaunch extends Transform implements Plugin<Project> { @Override void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException { super.transform(transformInvocation) //todo step1: 先掃描 transformInvocation.inputs.each { TransformInput input -> input.jarInputs.each { JarInput jarInput -> ... } input.directoryInputs.each { DirectoryInput directoryInput -> //處理完輸入檔案之後,要把輸出給下一個任務 ... } } //todo step2: ...完成程式碼注入 if (InjectInfo.get().injectToClass != null) { ... } } /** * 掃描jar包 * @param jarFile */ static void scanJar(File jarFile, File destFile) { } /** * 掃描檔案 * @param file */ static void scanFile(File file, File dest) { ... } }
注入程式碼一般分為兩個步驟
- 第一步:掃描
這一部分主要是掃描的內容有:
注入類和方法的資訊,是AutoRegisterContract的實現類和其中@IMethod,@Inject的方法。
待注入類的和方法資訊,是RouteInject 和 AInterceptorInject實現類且被@Inject註解的。 - 第二步:注入
以上掃描的結果,將待注入類注入到注入類的過程。這一過程面向ASM操作,可參考 位元組碼指令 來讀懂以下的關鍵注入程式碼:
class InjectClassVisitor extends ClassVisitor { ... class InjectMethodAdapter extends MethodVisitor { InjectMethodAdapter(MethodVisitor mv) { super(Opcodes.ASM5, mv) } @Override void visitInsn(int opcode) { Log.e(TAG, "inject to class:") Log.e(TAG, own + "{") Log.e(TAG, "public *** " + InjectInfo.get().injectToMethodName + "() {") if (opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) { InjectInfo.get().injectClasses.each { injectClass -> injectClass = injectClass.replace('/', '.') Log.e(TAG, "" + method + "(\"" + injectClass + "\")") mv.visitVarInsn(Opcodes.ALOAD, 0) mv.visitLdcInsn(injectClass) mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, own, method, "(Ljava/lang/String;)V", false) } } Log.e(TAG, "}") Log.e(TAG, "}") super.visitInsn(opcode) } ... } ... }
三、動態代理
定義:為其它物件提供一種代理以控制對這個物件的訪問控制;在某些情況下,客戶不想或者不能直接引用另一個物件,這時候代理物件可以在客戶端和目標物件之間起到中介的作用。
Routerfit.register(Class<T> service) 這裡就是採用動態代理的模式,使得ARetrofit的API非常簡潔,使用者可以優雅定義出路由介面。關於動態代理的學習難度相對來說還比較小,想了解的同學可以參考這篇文章 java動態代理 。
本專案相關原始碼:
public final class Routerfit { ... private <T> T create(final Class<T> service) { RouterUtil.validateServiceInterface(service); return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[]{service}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, @Nullable Object[] args) throws Throwable { // If the method is a method from Object then defer to normal invocation. if (method.getDeclaringClass() == Object.class) { return method.invoke(this, args); } ServiceMethod<Object> serviceMethod = (ServiceMethod<Object>) loadServiceMethod(method, args); if (!TextUtils.isEmpty(serviceMethod.uristring)) { Call<T> call = (Call<T>) new ActivityCall(serviceMethod); return call.execute(); } try { if (serviceMethod.clazz == null) { throw new RouteNotFoundException("There is no route match the path \"" + serviceMethod.routerPath + "\""); } } catch (RouteNotFoundException e) { Toast.makeText(ActivityLifecycleMonitor.getApp(), e.getMessage(), Toast.LENGTH_SHORT).show(); e.printStackTrace(); } if (RouterUtil.isSpecificClass(serviceMethod.clazz, Activity.class)) { Call<T> call = (Call<T>) new ActivityCall(serviceMethod); return call.execute(); } else if (RouterUtil.isSpecificClass(serviceMethod.clazz, Fragment.class) || RouterUtil.isSpecificClass(serviceMethod.clazz, android.app.Fragment.class)) { Call<T> call = new FragmentCall(serviceMethod); return call.execute(); } else if (serviceMethod.clazz != null) { Call<T> call = new IProviderCall<>(serviceMethod); return call.execute(); } if (serviceMethod.returnType != null) { if (serviceMethod.returnType == Integer.TYPE) { return -1; } else if (serviceMethod.returnType == Boolean.TYPE) { return false; } else if (serviceMethod.returnType == Long.TYPE) { return 0L; } else if (serviceMethod.returnType == Double.TYPE) { return 0.0d; } else if (serviceMethod.returnType == Float.TYPE) { return 0.0f; } else if (serviceMethod.returnType == Void.TYPE) { return null; } else if (serviceMethod.returnType == Byte.TYPE) { return (byte)0; } else if (serviceMethod.returnType == Short.TYPE) { return (short)0; } else if (serviceMethod.returnType == Character.TYPE) { return null; } } return null; } }); } ... }
這裡ServiceMethod是一個非常重要的類,使用了外觀模式,主要用於解析方法中的被註解所有資訊並儲存起來。
四、攔截器鏈實現
本專案中的攔截器鏈設計,使得使用者可以非常優雅的處理業務邏輯。如下:
@Interceptor(priority = 3) public class LoginInterceptor implements AInterceptor { private static final String TAG = "LoginInterceptor"; @Override public void intercept(final Chain chain) { //Test2Activity 需要登入 if ("/login-module/Test2Activity".equalsIgnoreCase(chain.path())) { Routerfit.register(RouteService.class).launchLoginActivity(new ActivityCallback() { @Override public void onActivityResult(int i, Object data) { if (i == Routerfit.RESULT_OK) {//登入成功後繼續執行 Toast.makeText(ActivityLifecycleMonitor.getTopActivityOrApp(), "登入成功", Toast.LENGTH_LONG).show(); chain.proceed(); } else { Toast.makeText(ActivityLifecycleMonitor.getTopActivityOrApp(), "登入取消/失敗", Toast.LENGTH_LONG).show(); } } }); } else { chain.proceed(); } } }
這一部分實現的思想是參考了okhttp中的攔截器,這裡使用了java設計模式責任鏈模式,具體實現歡迎閱讀原始碼。
結束語
到此,ARetrofit開源專案基本都以及講完,在做這個專案的過程中其實遇到了各種各樣的問題,其中ASM這塊耗費的時間較長,對於當時的我還是個小白。當然收穫也是頗多,這也是本人的第一個開源專案,存在的不足之處歡迎讀者和使用者提出,可以直接在qq群裡提出。