android 微信热修复Tinker接入过程以及使用方法

来源:互联网 发布:手机相片制作软件 编辑:程序博客网 时间:2024/06/05 10:11

github :  https://github.com/Tencent/tinker




1 添加依赖

compile('com.tencent.tinker:tinker-android-anno:1.6.2')compile('com.tencent.tinker:tinker-android-lib:1.6.2')
根目录配置文件添加tinker的gradle插件

classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.6.2')




2 添加(或修改)项目的 Application ,使之继承DefaultApplicationLike; 这块有点奇葩,这个DefaultApplicationLike不是继承自Application,需要用注解来设置项目中真正的Application,Tinker插件会自动生成真正的Application

@DefaultLifeCycle(        application = "ook.yzx.tinker.Application",        flags = ShareConstants.TINKER_ENABLE_ALL)public class MyApplication extends DefaultApplicationLike{    public MyApplication(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent, Resources[] resources, ClassLoader[] classLoader, AssetManager[] assetManager) {        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent, resources, classLoader, assetManager);    }}
上边的"ook.yzx.tinker.Application"就是真正的Application,不用我们自己写,是自动生成的;然后修改manifest.xml将application指向ook.yzx.tinker.Application就行,开始会报错,build一下项目就好了;



3 在build.gradle中的最外层添加下边的配置,这些个配置的意义可以看:tinker接入指南 ; 这些配置是从demo中挑出来的,有的配置也不是必须的;

apply plugin: 'com.tencent.tinker.patch'tinkerPatch {    //有问题的apk的地址  oldApk = "D://app_bug.apk"    ignoreWarning = false    useSign = true    buildConfig{        tinkerId = "1.0"    }    packageConfig{        //写这个为了修复一个bug,详见github issue #22        configField("TINKER_ID", "1.0")    }    dex{        dexMode = "jar"        pattern = ["classes*.dex", "assets/secondary-dex-?.jar"]        loader = ["com.tencent.tinker.loader.*", "ook.yzx.tinker.Application"]    }    lib{        pattern = ["lib/armeabi/*.so","lib/arm64-v8a/*.so","lib/armeabi-v7a/*.so","lib/mips/*.so","lib/mips64/*.so","lib/x86/*.so","lib/x86_64/*.so"]    }    res{        pattern = ["res/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]        largeModSize = 100    }    sevenZip{        zipArtifact = "com.tencent.mm:SevenZip:1.1.10"    }
}


4 java代码中的使用

首先是初始化 :在继承DefaultApplicationLike的类中重写onBaseContextAttached方法,然后在super之后执行TinkerInstaller.install(this)就行了;

然后是安装补丁 : 当补丁从自己的服务器下载好后, 执行

TinkerInstaller.onReceiveUpgradePatch(this.getApplication(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/patch.xxx");
详细见 : Tinker API概览;
PS:那个github上的demo写的真是墨墨迹迹的,各种和核心没用的代码,就这么几个主要的API,愣是写了那么一堆
5 现在万事俱备,就差执行了 ;   
大概测试步骤 : 写一个Activity
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TinkerInstaller.onReceiveUpgradePatch(this.getApplication(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/a1");        Toast.makeText(this, "错误的 !!!!! ~", Toast.LENGTH_SHORT).show();    }}
然后打包;生成一个apk,安装到手机,在拷贝一份到oldApk指定的目录(我的是D://app_bug.apk);
然后修改Toast的内容为其他的,打开android studio的底下的terminal标签卡(使用cmd也行), cd到对应的module文件夹下边,执行命令:gradle tinkerPatchRelease,然后是一顿等,第一次会慢点因为要下载一堆东西;之后会在build文件夹下生成一个outputs/tikerPatch文件夹,里边就是补丁文件以及其他的一些有用的文件,详见github的文档说明;将补丁文件放到手机中对应的地方后,打开一次app,然后kill掉,在打开,就变了...



6 Tinker的接入比较墨迹,研究AndFix时候用了一小时左右就能接入成功, 这玩意浪费了一下午才完事,主要是github上有一个issue22比较坑爹...




demo地址 :   http://download.csdn.net/detail/a750457103/9657279








4 0
原创粉丝点击