Alibaba-AndFix Bug热修复框架原理及源码解析
生活随笔
收集整理的這篇文章主要介紹了
Alibaba-AndFix Bug热修复框架原理及源码解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小憩之后,繼續為你解讀AndFix熱修復框架,呵呵。
上一篇Alibaba-AndFix Bug熱修復框架的使用已經介紹了AndFix的使用,這篇主要介紹AndFix原理以及源碼解析。
-
- AndFix原理
- 源碼解析
- 在自定義Application中初始化PatchManger
- init方法源碼
- Patch文件的加載
- loadPatch方法源碼
- fix bug
- Dalvik replaceMethod的實現
- Art replaceMethod的實現
- 添加Patch
- 移除Patch
- 反編譯Patch dex文件代碼
AndFix原理
AndFix的原理就是方法的替換,把有bug的方法替換成補丁文件中的方法。?
注:在Native層使用指針替換的方式替換bug方法,已達到修復bug的目的。
使用AndFix修復熱修復的整體流程:
方法替換過程:
源碼解析
解析源碼從使用的方法一一解析。
在自定義Application中初始化PatchManger:
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">PatchManager mPatchManager </span>=<span class="hljs-string" style="color:#0880;box-sizing: border-box;"> new PatchManager();</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>直接實例化了一個PatchManger實例對象,接下看PatchManager類源碼:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">PatchManager</span>(Context context) {mContext = context;mAndFixManager = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> AndFixManager(mContext);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化AndFixManager</span>mPatchDir = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> File(mContext.getFilesDir(), DIR);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化存放patch補丁文件的文件夾</span>mPatchs = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ConcurrentSkipListSet<Patch>();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化存在Patch類的集合,此類適合大并發</span>mLoaders = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ConcurrentHashMap<String, ClassLoader>();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化存放類對應的類加載器集合</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>然后看AndFixManager的初始化:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">AndFixManager</span>(Context context) {mContext = context;mSupport = Compat.isSupport();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判斷Android機型是否適支持AndFix</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mSupport) {mSecurityChecker = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> SecurityChecker(mContext);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化簽名判斷類</span>mOptDir = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> File(mContext.getFilesDir(), DIR);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化patch文件存放的文件夾</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!mOptDir.exists() && !mOptDir.mkdirs()) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// make directory fail</span>mSupport = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>;Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"opt dir create error."</span>);} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!mOptDir.isDirectory()) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// not directory</span>mOptDir.delete();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//如果不是文件目錄就刪除</span>mSupport = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>;}} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">synchronized</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> <span class="hljs-title" style="box-sizing: border-box;">isSupport</span>() {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//此處加了同步鎖機制</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (isChecked)<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> isSupport;isChecked = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// not support alibaba's YunOs</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> isYunOs = isYunOS();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判斷系統是否是YunOs系統,YunOs系統是阿里巴巴的系統</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> setup =AndFix.setup();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判斷是Dalvik還是Art虛擬機,來注冊Native方法</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> isSupportSDKVersion = isSupportSDKVersion();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//根據sdk版本判斷是否支持</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!isYunOs && setup && isSupportSDKVersion) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//根據上面三個boolean值判斷是否支持</span>isSupport = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>;}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (inBlackList()) {isSupport = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>;}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> isSupport; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> <span class="hljs-title" style="box-sizing: border-box;">isSupportSDKVersion</span>() {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (android.os.Build.VERSION.SDK_INT >= <span class="hljs-number" style="color:#06666;box-sizing: border-box;">8</span>&& android.os.Build.VERSION.SDK_INT <= <span class="hljs-number" style="color:#06666;box-sizing: border-box;">23</span>) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>;}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> <span class="hljs-title" style="box-sizing: border-box;">setup</span>() {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">final</span> String vmVersion = System.getProperty(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"java.vm.version"</span>);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> isArt = vmVersion != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span> && vmVersion.startsWith(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"2"</span>);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span> apilevel = Build.VERSION.SDK_INT;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> setup(isArt, apilevel);} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Exception e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"setup"</span>, e);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>;} } <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//簽名機制的初始化過程</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">SecurityChecker</span>(Context context) {mContext = context;init(mContext); } <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//主要是獲取當前應用的簽名及其他信息,為了判斷與patch文件的簽名是否一致</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">init</span>(Context context) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {PackageManager pm = context.getPackageManager();String packageName = context.getPackageName();PackageInfo packageInfo = pm.getPackageInfo(packageName,PackageManager.GET_SIGNATURES);CertificateFactory certFactory = CertificateFactory.getInstance(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"X.509"</span>);ByteArrayInputStream stream = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ByteArrayInputStream(packageInfo.signatures[<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>].toByteArray());X509Certificate cert = (X509Certificate) certFactory.generateCertificate(stream);mDebuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);mPublicKey = cert.getPublicKey();} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (NameNotFoundException e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"init"</span>, e);} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (CertificateException e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"init"</span>, e);} }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li></ul>接下,看一下版本的初始化:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mPatchManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.init</span>(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"version"</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>init方法源碼:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">init</span>(String appVersion) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!mPatchDir.exists() && !mPatchDir.mkdirs()) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// make directory fail</span>Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"patch dir create error."</span>);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>;} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!mPatchDir.isDirectory()) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// not directory</span>mPatchDir.delete();<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>;}SharedPreferences sp = mContext.getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//存儲關于patch文件的信息</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//根據你傳入的版本號和之前的對比,做不同的處理</span>String ver = sp.getString(SP_VERSION, <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (ver == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span> || !ver.equalsIgnoreCase(appVersion)) {cleanPatch();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//刪除本地patch文件</span>sp.edit().putString(SP_VERSION, appVersion).commit();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//并把傳入的版本號保存</span>} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> {initPatchs();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化patch列表,把本地的patch文件加載到內存</span>} }<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">cleanPatch</span>() {File[] files = mPatchDir.listFiles();<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">for</span> (File file : files) {mAndFixManager.removeOptFile(file);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//刪除所有的本地緩存patch文件</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!FileUtil.deleteFile(file)) {Log.e(TAG, file.getName() + <span class="hljs-string" style="color:#0880;box-sizing: border-box;">" delete error."</span>);}} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">initPatchs</span>() {File[] files = mPatchDir.listFiles();<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">for</span> (File file : files) {addPatch(file);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//加載Patch文件</span>} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> Patch <span class="hljs-title" style="box-sizing: border-box;">addPatch</span>(File file) {Patch patch = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (file.getName().endsWith(SUFFIX)) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {patch = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> Patch(file);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//實例化Patch對象</span>mPatchs.add(patch);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//把patch實例存儲到內存的集合中,在PatchManager實例化集合</span>} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (IOException e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"addPatch"</span>, e);}}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> patch; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li></ul>Patch文件的加載
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">Patch</span>(File file) <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">throws</span> IOException {mFile = file;init(); }<span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@SuppressWarnings</span>(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"deprecation"</span>) <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">init</span>() <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">throws</span> IOException {JarFile jarFile = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>;InputStream inputStream = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {jarFile = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> JarFile(mFile);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//使用JarFile讀取Patch文件</span>JarEntry entry = jarFile.getJarEntry(ENTRY_NAME);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取META-INF/PATCH.MF文件</span>inputStream = jarFile.getInputStream(entry);Manifest manifest = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> Manifest(inputStream);Attributes main = manifest.getMainAttributes();mName = main.getValue(PATCH_NAME);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取PATCH.MF屬性Patch-Name</span>mTime = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> Date(main.getValue(CREATED_TIME));<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取PATCH.MF屬性Created-Time</span>mClassesMap = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> HashMap<String, List<String>>();Attributes.Name attrName;String name;List<String> strings;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">for</span> (Iterator<?> it = main.keySet().iterator(); it.hasNext();) {attrName = (Attributes.Name) it.next();name = attrName.toString();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判斷name的后綴是否是-Classes,并把name對應的值加入到集合中,對應的值就是class類名的列表</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (name.endsWith(CLASSES)) {strings = Arrays.asList(main.getValue(attrName).split(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">","</span>));<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (name.equalsIgnoreCase(PATCH_CLASSES)) {mClassesMap.put(mName, strings);} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> {mClassesMap.put(name.trim().substring(<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>, name.length() - <span class="hljs-number" style="color:#06666;box-sizing: border-box;">8</span>),<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// remove</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// "-Classes"</span>strings);}}}} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">finally</span> {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (jarFile != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {jarFile.close();}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (inputStream != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {inputStream.close();}}}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li></ul>loadPatch方法源碼
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mPatchManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.loadPatch</span>()<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>loadPatch源碼:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span> loadPatch() {mLoaders<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"*"</span>, mContext<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getClassLoader());<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// wildcard</span><span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">Set</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;"><</span><span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">String</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">></span> patchNames;<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">List</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;"><</span><span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">String</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">></span> classes;for (Patch patch : mPatchs) {patchNames <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> patch<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getPatchNames();for (<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">String</span> patchName : patchNames) {classes <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> patch<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getClasses(patchName);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取patch對用的class類的集合List</span>mAndFixManager<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>fix(patch<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getFile(), mContext<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getClassLoader(),classes);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//修復bug方法</span>}} }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>fix bug
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">synchronized</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">fix</span>(File file, ClassLoader classLoader,List<String> classes) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!mSupport) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>;}<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判斷patch文件的簽名</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!mSecurityChecker.verifyApk(file)) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// security check fail</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>;}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {File optfile = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> File(mOptDir, file.getName());<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> saveFingerprint = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (optfile.exists()) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// need to verify fingerprint when the optimize file exist,</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// prevent someone attack on jailbreak device with</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// Vulnerability-Parasyte.</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// btw:exaggerated android Vulnerability-Parasyte</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// http://secauo.com/Exaggerated-Android-Vulnerability-Parasyte.html</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mSecurityChecker.verifyOpt(optfile)) {saveFingerprint = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>;} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!optfile.delete()) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>;}}<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//加載patch文件中的dex</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">final</span> DexFile dexFile = DexFile.loadDex(file.getAbsolutePath(),optfile.getAbsolutePath(), Context.MODE_PRIVATE);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (saveFingerprint) {mSecurityChecker.saveOptSig(optfile);}ClassLoader patchClassLoader = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ClassLoader(classLoader) {<span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">protected</span> Class<?> <span class="hljs-title" style="box-sizing: border-box;">findClass</span>(String className)<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">throws</span> ClassNotFoundException {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//重寫ClasLoader的findClass方法</span>Class<?> clazz = dexFile.loadClass(className, <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">this</span>);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (clazz == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>&& className.startsWith(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"com.alipay.euler.andfix"</span>)) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> Class.forName(className);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// annotation’s class</span><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// not found</span>}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (clazz == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">throw</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ClassNotFoundException(className);}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> clazz;}};Enumeration<String> entrys = dexFile.entries();Class<?> clazz = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">while</span> (entrys.hasMoreElements()) {String entry = entrys.nextElement();<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (classes != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span> && !classes.contains(entry)) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">continue</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// skip, not need fix</span>}clazz = dexFile.loadClass(entry, patchClassLoader);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取有bug的類文件</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (clazz != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {fixClass(clazz, classLoader);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// next code</span>}}} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (IOException e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"pacth"</span>, e);} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">fixClass</span>(Class<?> clazz, ClassLoader classLoader) {Method[] methods = clazz.getDeclaredMethods();MethodReplace methodReplace;String clz;String meth;<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">for</span> (Method method : methods) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取此方法的注解,因為有bug的方法在生成的patch的類中的方法都是有注解的,下面會給圖進行展示</span>methodReplace = method.getAnnotation(MethodReplace.class);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (methodReplace == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>)<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">continue</span>;clz = methodReplace.clazz();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取注解中clazz的值</span>meth = methodReplace.method();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//獲取注解中method的值</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!isEmpty(clz) && !isEmpty(meth)) {replaceMethod(classLoader, clz, meth, method);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//next code</span>}} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">replaceMethod</span>(ClassLoader classLoader, String clz,String meth, Method method) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {String key = clz + <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"@"</span> + classLoader.toString();Class<?> clazz = mFixedClass.get(key);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判斷此類是否被fix</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (clazz == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// class not load</span>Class<?> clzz = classLoader.loadClass(clz);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// initialize target class</span>clazz = AndFix.initTargetClass(clzz);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//初始化class</span>}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (clazz != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// initialize class OK</span>mFixedClass.put(key, clazz);Method src = clazz.getDeclaredMethod(meth,method.getParameterTypes());<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//根據反射獲取到有bug的類的方法(有bug的apk)</span>AndFix.addReplaceMethod(src, method);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//src是有bug的方法,method是補丁方法</span>}} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Exception e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"replaceMethod"</span>, e);} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">addReplaceMethod</span>(Method src, Method dest) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> {replaceMethod(src, dest);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//調用了native方法,next code</span>initFields(dest.getDeclaringClass());} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Throwable e) {Log.e(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"addReplaceMethod"</span>, e);} } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">native</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">replaceMethod</span>(Method dest, Method src);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li><li style="box-sizing: border-box; padding: 0px 5px;">92</li><li style="box-sizing: border-box; padding: 0px 5px;">93</li><li style="box-sizing: border-box; padding: 0px 5px;">94</li><li style="box-sizing: border-box; padding: 0px 5px;">95</li><li style="box-sizing: border-box; padding: 0px 5px;">96</li><li style="box-sizing: border-box; padding: 0px 5px;">97</li><li style="box-sizing: border-box; padding: 0px 5px;">98</li><li style="box-sizing: border-box; padding: 0px 5px;">99</li><li style="box-sizing: border-box; padding: 0px 5px;">100</li><li style="box-sizing: border-box; padding: 0px 5px;">101</li><li style="box-sizing: border-box; padding: 0px 5px;">102</li><li style="box-sizing: border-box; padding: 0px 5px;">103</li><li style="box-sizing: border-box; padding: 0px 5px;">104</li><li style="box-sizing: border-box; padding: 0px 5px;">105</li><li style="box-sizing: border-box; padding: 0px 5px;">106</li><li style="box-sizing: border-box; padding: 0px 5px;">107</li><li style="box-sizing: border-box; padding: 0px 5px;">108</li><li style="box-sizing: border-box; padding: 0px 5px;">109</li><li style="box-sizing: border-box; padding: 0px 5px;">110</li><li style="box-sizing: border-box; padding: 0px 5px;">111</li></ul>由于Android4.4后才用的Art虛擬機,之前的系統都是Dalvik虛擬機,因此Natice層寫了2個方法,對不同的系統做不同的處理方式。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> dalvik_replaceMethod(JNIEnv* env, jobject src, jobject dest);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//Dalvik</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> art_replaceMethod(JNIEnv* env, jobject src, jobject dest);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//Art</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>Dalvik replaceMethod的實現:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">extern <span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span> __attribute__ ((visibility (<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"hidden"</span>))) dalvik_replaceMethod(JNIEnv<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> env, jobject src, jobject dest) {jobject clazz <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> env<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>CallObjectMethod(dest, jClassMethod);ClassObject<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> clz <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> (ClassObject<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>) dvmDecodeIndirectRef_fnPtr(dvmThreadSelf_fnPtr(), clazz);clz<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>status <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> CLASS_INITIALIZED;Method<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> meth <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> (Method<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>) env<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>FromReflectedMethod(src);Method<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> target <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> (Method<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>) env<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>FromReflectedMethod(dest);LOGD(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"dalvikMethod: %s"</span>, meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>name);meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>jniArgInfo <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0x80000000</span>;meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>accessFlags <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">|=</span> ACC_NATIVE;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//把Method的屬性設置成Native方法</span>int argsSize <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dvmComputeMethodArgsSize_fnPtr(meth);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">!</span>dvmIsStaticMethod(meth))argsSize<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">++</span>;meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>registersSize <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>insSize <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> argsSize;meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>insns <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> (<span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>) target;meth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>nativeFunc <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dalvik_dispatcher;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//把方法的實現替換成native方法</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>Art replaceMethod的實現:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//不同的art系統版本不同處理也不同</span> extern <span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span> __attribute__ ((visibility (<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"hidden"</span>))) art_replaceMethod(JNIEnv<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> env, jobject src, jobject dest) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (apilevel <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">></span> <span class="hljs-number" style="color:#06666;box-sizing: border-box;">22</span>) {replace_6_0(env, src, dest);} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (apilevel <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">></span> <span class="hljs-number" style="color:#06666;box-sizing: border-box;">21</span>) {replace_5_1(env, src, dest);} <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> {replace_5_0(env, src, dest);} } <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//以5.0為例:</span> <span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span> replace_5_0(JNIEnv<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> env, jobject src, jobject dest) {art<span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::mirror</span><span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::ArtMethod</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> smeth <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span>(art<span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::mirror</span><span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::ArtMethod</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>) env<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>FromReflectedMethod(src);art<span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::mirror</span><span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::ArtMethod</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span> dmeth <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span>(art<span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::mirror</span><span class="hljs-tag" style="color:#06666;box-sizing: border-box;">::ArtMethod</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>) env<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>FromReflectedMethod(dest);dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>class_loader_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span>smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>class_loader_; <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//for plugin classloader</span>dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>clinit_thread_id_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span>smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>clinit_thread_id_;dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>status_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> (<span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span> <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">*</span>)((int)smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>status_<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//把一些參數的指針給補丁方法</span>smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>declaring_class_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>access_flags_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>access_flags_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>frame_size_in_bytes_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>frame_size_in_bytes_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>dex_cache_initialized_static_storage_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span>dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>dex_cache_initialized_static_storage_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>dex_cache_resolved_types_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>dex_cache_resolved_types_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>dex_cache_resolved_methods_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>dex_cache_resolved_methods_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>vmap_table_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>vmap_table_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>core_spill_mask_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>core_spill_mask_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>fp_spill_mask_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>fp_spill_mask_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>mapping_table_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>mapping_table_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>code_item_offset_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>code_item_offset_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>entry_point_from_compiled_code_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span>dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>entry_point_from_compiled_code_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>entry_point_from_interpreter_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>entry_point_from_interpreter_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>native_method_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>native_method_;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//把補丁方法替換掉</span>smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>method_index_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>method_index_;smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>method_dex_index_ <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>method_dex_index_;LOGD(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"replace_5_0: %d , %d"</span>, smeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>entry_point_from_compiled_code_,dmeth<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-></span>entry_point_from_compiled_code_);}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li></ul>添加Patch
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mPatchManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.addPatch</span>(path)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>源碼:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">addPatch</span>(String path) <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">throws</span> IOException {File src = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> File(path);File dest = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> File(mPatchDir, src.getName());<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!src.exists()) {<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">throw</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> FileNotFoundException(path);}<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (dest.exists()) {Log.d(TAG, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"patch ["</span> + path + <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"] has be loaded."</span>);<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>;}FileUtil.copyFile(src, dest);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// copy to patch's directory</span>Patch patch = addPatch(dest);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//同loadPatch中的addPatch一樣的操作</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (patch != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) {loadPatch(patch);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//加載pach,同上loadPatch</span>}}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>移除Patch
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mPatchManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.removeAllPatch</span>()<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>源碼:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">removeAllPatch</span>() {cleanPatch();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//刪除本地緩存的patch文件列表</span>SharedPreferences sp = mContext.getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);sp.edit().clear().commit();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//把關于patch的數據進行清空</span>}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>到此源代碼就解析結束。
反編譯Patch dex文件代碼
patch文件中.dex文件反編譯后,看到源碼效果如下:?
?
紅框標注的部分,就是補丁方法。方法的注解部分寫了clazz和method的值,對應著apk包中的類名和方法名稱。
原文地址: http://blog.csdn.net/qxs965266509/article/details/49816007
總結
以上是生活随笔為你收集整理的Alibaba-AndFix Bug热修复框架原理及源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Alibaba-Dexposed框架在线
- 下一篇: Alibaba-AndFix Bug热修