Tinker接入简单实践

1.添加依赖
在项目的build.gradle中,添加tinker-patch-gradle-plugin的依赖

buildscript {
    dependencies {
        classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.9')
    }
}

然后在app的gradle文件app/build.gradle,我们需要添加tinker的库依赖以及apply tinker的gradle插件.

dependencies {
    //可选,用于生成application类 
    provided('com.tencent.tinker:tinker-android-anno:1.7.9')
    //tinker的核心库
    compile('com.tencent.tinker:tinker-android-lib:1.7.9') 
}
...
...
//apply tinker插件
apply plugin: 'com.tencent.tinker.patch'
2.添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.app_bulid.gradle

拷贝sample中的

4.自定义application实现DefaultApplicationLike或者拷贝SampleApplicationLike到自己项目中
同时要在AndroidManifest.xml中修改appName为com.zhongchao.myhotfixtest4.SampleApplication即自己修改过得名字
注意:此时application是没有的,会报红,但会在编译时生成,因此不用担心
5.初始化Tinker
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onBaseContextAttached(Context base) {
    super.onBaseContextAttached(base);
    //you must install multiDex whatever tinker is installed!
    MultiDex.install(base);

    //installTinker after load multiDex
    //or you can put com.tencent.tinker.** to main dex
    TinkerInstaller.install(this);
    Tinker tinker = Tinker.with(getApplication());
}

此处做了修改,没有用实例中的,最重要的是这句TinkerInstaller.install(this);
6.加载补丁

注意:加载补丁要等生成补丁,并且推送到手机上以后,才能加载

loadPatchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");
            }
        });
7.签名打包apk
8.改动并配置路径

注意:原apk路径

def bakPath = file("${buildDir}/bakApk/")
tinkerOldApkPath = "${bakPath}/app-release-0504-11-40-20.apk"
//proguard mapping file to build patch apk
tinkerApplyMappingPath = "${bakPath}/app-release-0504-11-40-20-mapping.txt"
//resource R.txt to build patch apk, must input if there is resource changed
tinkerApplyResourcePath = "${bakPath}/app-release-0504-11-40-20-R.txt"

这些都要更改

9.生成补丁

studio右侧,点击gradle–>tinker–>tinkerPath…debug或release
生成之后推送即可

问题:tinkerId
自己修改一下即可

def getTinkerIdValue() {
    return hasProperty("TINKER_ID") ? TINKER_ID : "tinker_id_2333"
}

tinker已知问题
由于原理与系统限制,Tinker有以下已知问题:

  • Tinker不支持修改AndroidManifest.xml,Tinker不支持新增四大组件;
  • 由于Google Play的开发者条款限制,不建议在GP渠道动态更新代码;
  • 在Android N上,补丁对应用启动时间有轻微的影响;
  • 不支持部分三星android-21机型,加载补丁时会主动抛出”TinkerRuntimeException:checkDexInstall failed”;
  • 对于资源替换,不支持修改remoteView。例如transition动画,notification icon以及桌面图标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值