tinker接入

對於熱修復無非就是兩大類,一類是tencent表明的classloader模式的,另外一類是阿里系表明的底層方面替換。html

下面以本人的經驗介紹下微信的tinker接入:java

    命令行接入方式; gradle接入方式linux

1. 核心庫引入,在你應用的app模塊下build.gradle文件加入android

//tinker hotfix
//可選,用於生成application類
// compileOnly("com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}")
//tinker's main Android lib
implementation "com.tencent.tinker:tinker-android-lib:${TINKER_VERSION}"

若是應用開啓了multiDex(通常應用都會用到),還必須加入以下配置
defaultConfig {
  ...
   ...

multiDexEnabled true
//必須打入第一個dex包的java類
multiDexKeepProguard file("tinker_multidexkeep.pro")

}
固然其實上述採用的是proguard方式加入到Main dex中,固然也能夠採用multiDexKeepFile方式(一行一個類形式)
主要目的就是確保tinker的包中類分到第一個dex中,具體能夠參考:https://juejin.im/entry/5893e54f128fe100654763a0
https://www.kancloud.cn/alex_wsc/artist/481985https://blog.csdn.net/zhangbuzhangbu/article/details/52770939

其實關於分包模式的問題,能夠參考個人一片博文: http://www.javashuo.com/article/p-ghntponr-eu.htmlhttps://www.kancloud.cn/alex_wsc/android_plugin/481528

二、通常應用的application都有不少特殊的初始化和配置的內容,因此這裏不打算使用註解生成application的方式,而是本身加入tinker的內容。
明文寫出Application的構造函數,在裏面調用super類TinkerApplication的構造函數
  public class WishApplication extends TinkerApplication {
    public WishApplication(){
    super(//tinkerFlags, which types is supported dex only, library only, all support
  ShareConstants.TINKER_ENABLE_ALL,
  // This is passed as a string so the shell application does not
  // have a binary dependency on your ApplicationLifeCycle class.
  "com.yunzhiyuan100.wish.SimpleApplicationLike");
    }
}
若是原本extends MultiApplictiaon的話就要換成 extends TinkerApplication了,而後重寫attachBaseContext()方法,在該方法里加入MultiDex的內容。
以下

protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
MultiDex.install(this);
}
而後就是你原來Application的代碼邏輯的內容,能夠不用變更
3. 而後自定義Application中初始化函數用到ApplicationLike類參數實例,能夠直接按照tinker給的代碼拷貝
 1 public class SimpleApplicationLike extends ApplicationLike {
 2 
 3     public SimpleApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag,
 4                                  long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
 5         super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
 6     }
 7 
 8     @Override
 9     public void onBaseContextAttached(Context base) {
10         super.onBaseContextAttached(base);
11 
12     }
13     @Override
14     public void onCreate() {
15         super.onCreate();
16         TinkerInstaller.install(this);
17     }
18 
19     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
20     public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
21         getApplication().registerActivityLifecycleCallbacks(callback);
22     }
23 
24 }

 4. proguard改變git

1 #your dex.loader pattern here
2 -keep class com.tencent.tinker.loader.**
3 #注意 AndroidManifest.xml中的applicaion
4 -keep class com.yunzhiyuan100.wish.WishApplication
5 
6 # ***************** Tinker patch包
7 # 打替換apk包的時候須要
8 -applymapping mapping.txt

而後就能夠打包了,打old.apk的時候自動生成mapping.txt, 第二次打new.apk包的時候爲了保持混淆的一致性,要使用第一混淆的時候的mapping文件,github

-applymapping mapping.txt

5.  AndroidManifest修改,application標籤下面加入shell

1 <meta-data
2     android:name="TINKER_ID"
3     android:value="20190604" />
6.下載Thinker的項目 

  tinker提供了patch生成的工具,源碼見:tinker-patch-cli,打成一個jar就可使用,而且提供了命令行相關的參數以及文件。微信

       tinker的項目地址: https://github.com/Tencent/tinkerapp

       而後經過android studio打開整個項目ide

  (1). 在控制檯輸入:./gradlew buildTinkerSdk    (window上gradlew buildTinkerSdk)

      在指定路徑看到生成的文件以後,將剛纔生成的old.apk和new.apk拷貝進去。

       (2). 修改tinker_config.xml

    loader修改爲本身的application

    <issue id="dex">
        <!--only can be 'raw' or 'jar'. for raw, we would keep its original format-->
        <!--for jar, we would repack dexes with zip format.-->
        <!--if you want to support below 14, you must use jar-->
        <!--or you want to save rom or check quicker, you can use raw mode also-->
        <dexMode value="jar"/>

        <!--what dexes in apk are expected to deal with tinkerPatch-->
        <!--it support * or ? pattern.-->
        <pattern value="classes*.dex"/>
        <pattern value="assets/secondary-dex-?.jar"/>

        <!--Warning, it is very very important, loader classes can't change with patch.-->
        <!--thus, they will be removed from patch dexes.-->
        <!--you must put the following class into main dex.-->
        <!--Simply, you should add your own application {@code tinker.sample.android.SampleApplication}-->
        <!--own tinkerLoader {@code SampleTinkerLoader}, and the classes you use in them-->
        <loader value="com.tencent.tinker.loader.*"/>
        <loader value="com.yunzhiyuan100.wish.WishApplication"/>
    </issue>

另外,若是是正式包,須要修改簽名配置爲你項目的簽名,還須要把簽名文件yunzhiyuan.jks放到buildSDK的目錄build下面

  <!--sign, if you want to sign the apk, and if you want to use 7zip, you must fill in the following data-->
    <issue id="sign">
        <!--the signature file path, in window use \, in linux use /, and the default path is the running location-->
        <path value="yunzhiyuan.jks"/>
        <!--storepass-->
        <storepass value="bgsb1709@hzyzy"/>
        <!--keypass-->
        <keypass value="bgsb1709@hzyzy"/>
        <!--alias-->
        <alias value="wish"/>
    </issue>

(3)最後就能夠執行patch命令打出差別包了

  命令行以下: 

  java -jar tinker-patch-cli-1.9.13.jar -old old.apk -new new.apk -config tinker_config.xml -out output

注意tinker-patch-cli這個jar包的名字要和buildSdk/build/目錄下的該jar包版本名稱一致

把打出來的差別包adb push 到sdcard目錄下: adb push patch_sign.apk   /sdcard/

而後在MainActivity中加載差別包,經過加入一個按鈕的點擊調用下面的loadFix()函數

1 public void loadFix(){
2   TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),
3   Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed.apk");
4 }

這裏調用loadFix()以後,應用會被關閉,再次打開,測試獲得想要的修改結果。

大功告成了

參考:

    http://www.javashuo.com/article/p-njirosii-gs.html

    http://www.javashuo.com/article/p-gkuqasag-da.html

    https://github.com/Tencent/tinker

    https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97

    https://blog.csdn.net/cjw8990/article/details/79117880

相關文章
相關標籤/搜索