最近学习了下热修复tinker框架的应用,参考了网上很多人的demo,踩了不少坑,主要是我水平还比较菜,对gradle的结构也不太清楚,所以浪费了不少时间,现在做下笔记,也许能给后来的人提供方便,少走弯路。
由于官方demo考虑比较多,所以代码也很多,容易混淆视听,这里我放上我自己写的demo,对官方demo进行了极大的精简,为的是快速熟悉接入流程,和tinker的使用步骤。
1.首先在工程下的build.gradle(project目录下)里添加依赖
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
//tinker依懒
classpath "com.tencent.tinker:tinker-patch-gradle-plugin:${TINKER_VERSION}"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
同时记得在gradle.properties里加一行:
TINKER_VERSION=1.7.7
2.修改module里的build.gradle,因为涉及较多,二话不说,直接去拷贝现成的,传送门
复制所有到自己的build.gradle,如果需要别的依赖,再单独添加。
3.编写Application文件,这个也二话不说,直接去拷贝现成的,传送门
SimpleTinkerInApplicationLike.java是基于编译时注解的类,编译时自动生成SimpleTinkerInApplication.java,这里记得先将这个名字拷贝到清单文件里:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".SimpleTinkerInApplication"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
4.编写测试代码。以修改布局文件中的一个textView为例,
*修改前首先执行clean,以清除build历史,先执行一次assembleRelease,在build目录下bakApk文件夹里会生成三个文件,将其名字拷贝到build.gradle中对应位置。
*试验修改一行代码,我修改的是布局文件中的hello world,修改为“修改了一个BUG”,然后执行tinkerPatchRelease命令,之后build目录下outPuts文件夹里会生成tinkerPatch文件夹,其中就有补丁文件,把patch_signed_7zip.apk拷贝或者push到手机内置SD卡根目录,这里注意不是外置sd存储卡.
*好了,打开应用,点击path按钮,应用会退出,重新进入,会发现文本已修改成功,点show按钮看到补丁生效了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.practice.dev.tinker_fix.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/patch"
android:text="PATCH"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="SHOW"
/>
</LinearLayout>
MainActivity.java代码如下:
package com.practice.dev.tinker_fix;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.shareutil.ShareConstants;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;
public class MainActivity extends AppCompatActivity {
private Button patch;
private Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
patch = (Button) findViewById(R.id.patch);
show = (Button) findViewById(R.id.show);
//执行修补动作
patch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),
Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/patch_signed_7zip.apk");//
}
});
//查看补丁是否加载
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInfo(MainActivity.this);
}
});
}
public boolean showInfo(Context context) {
// add more Build Info
final StringBuilder sb = new StringBuilder();
Tinker tinker = Tinker.with(getApplicationContext());
//判断补丁是否加载
if (tinker.isTinkerLoaded()) {
sb.append(String.format("[patch is loaded] \n"));
sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildConfig.TINKER_ID));
// sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BuildConfig.BASE_TINKER_ID));
sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildConfig.MESSAGE));
sb.append(String.format("[TINKER_ID] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName(ShareConstants.TINKER_ID)));
sb.append(String.format("[packageConfig patchMessage] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName("patchMessage")));
sb.append(String.format("[TINKER_ID Rom Space] %d k \n", tinker.getTinkerRomSpace()));
} else {
sb.append(String.format("[patch is not loaded] \n"));
sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildConfig.TINKER_ID));
// sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildConfig.MESSAGE));
sb.append(String.format("[TINKER_ID] %s \n", ShareTinkerInternals.getManifestTinkerID(getApplicationContext())));
}
// sb.append(String.format("[BaseBuildInfo Message] %s \n", BuildConfig.TEST_MESSAGE));
final TextView v = new TextView(context);
v.setText(sb);
v.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
v.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
v.setTextColor(0xFF000000);
v.setTypeface(Typeface.MONOSPACE);
final int padding = 16;
v.setPadding(padding, padding, padding, padding);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setView(v);
final AlertDialog alert = builder.create();
alert.show();
return true;
}
}
最终效果图如下:
//需要注意的地方,关于签名,我是把签名文件放到module目录下了,在build.gradle中配置。
源码:https://github.com/hiliving/Tinker-Fix
附上官网指南:https://github.com/Tencent/tinker/wiki