Android(安卓)源码分析实战 - 把代码写得更优雅
16lz
2021-01-26
1. 源码版本适配
一般情况下来说,如果涉及到源码反射,通常都需要适配各个版本,因此我们把与 so 修复相关的各大版本源码都翻出来,具体的源码细节,大家可以参考《Android 源码分析实战 - 动态加载修复 so 库》
public void injectLoadPath(String soDir) throws Exception { ... int version = Build.VERSION.SDK_INT; if(version < Build.VERSION_CODES.M){ nativeLibraryPathElementsField = ReflectUtil.getFiled(pathList,"nativeLibraryDirectories"); }else if(version >= Build.VERSION_CODES.M){ nativeLibraryPathElementsField = ReflectUtil.getFiled(pathList, "nativeLibraryPathElements"); } ... Object firstElement = null; if(version < Build.VERSION_CODES.M){ firstElement = new File(soDir); }else if(version >= Build.VERSION_CODES.M && version < Build.VERSION_CODES.O){ Constructor<?> elementConstructor = elementClass.getConstructor(File.class); elementConstructor.setAccessible(true); firstElement = elementConstructor.newInstance(new File(soDir)); }else if(version >= Build.VERSION_CODES.O){ Constructor<?> elementConstructor = elementClass.getConstructor(File.class, boolean.class, File.class, DexFile.class); elementConstructor.setAccessible(true); firstElement = elementConstructor.newInstance(new File(soDir),true, null, null); } ... }
写出上面这样的代码,个人觉得应该差不多了。但我们平时写代码的时候,往往是想过要把代码写得更优雅,但就是感觉不知道该怎么写?因此我建议大家关于设计模式,一定要仔细学习理解但不要生搬硬套,学过之后要选择性的忘记这些内容。我也曾听同学说过,千万不要学设计模式,设计模式会固化我们的思想,希望大家不要有这样的错觉。
2. AppCompatDelegate 源码分析
我们可以先参考 Android 源码,也就是看下 Google 工程师,是怎么写源码适配代码的
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { getDelegate().installViewFactory(); getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); } public AppCompatDelegate getDelegate() { if (mDelegate == null) { mDelegate = AppCompatDelegate.create(this, this); } return mDelegate; } private static AppCompatDelegate create(Context context, Window window, AppCompatCallback callback) { final int sdk = Build.VERSION.SDK_INT; if (sdk >= 23) { return new AppCompatDelegateImplV23(context, window, callback); } else if (sdk >= 14) { return new AppCompatDelegateImplV14(context, window, callback); } else if (sdk >= 11) { return new AppCompatDelegateImplV11(context, window, callback); } else { return new AppCompatDelegateImplV7(context, window, callback); } }
3. 改造版本适配代码
身经百战后我们自然知道该怎么写,并不一定非得模仿系统源码,就按照原来的代码也行,这里我只是举一个例子。
public class SoHotFix { private SoFix mSoFix; public SoHotFix(Context context) { Context applicationContext = context.getApplicationContext(); // 各大版本适配 final int sdk = Build.VERSION.SDK_INT; if (sdk >= Build.VERSION_CODES.O) { mSoFix = new SoFixImpV26(applicationContext); } else if (sdk >= Build.VERSION_CODES.M) { mSoFix = new SoFixImpV23(applicationContext); } else { mSoFix = new SoFixImpV20(applicationContext); } } public void injectLoadPath(String soDir) throws Exception { mSoFix.hotFix(soDir); }}
这是我们随便挑的一个简单的例子,只是想表达源码其实是最好的学习资料,Linus Torvalds 曾说过 Read The Fucking Source Code。
视频地址:https://pan.baidu.com/s/1COarOAlmOPCUlKsazho0Cw
视频密码:8lr0
更多相关文章
- Android(安卓)Studio出现The selected directory is not a valid
- 尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的
- android 基站定位
- Alibaba-AndFix Bug热修复框架原理及源码解析
- 开源项目cNode Android版本
- android studio 使用备忘
- 自定义模态提示框
- Android(安卓)Fragment 体系 源码追踪笔记(4)
- Android(安卓)Service完全解析(下)