这篇博客是基于Android 热修复框架 Tinker ( 一 )写的,在看这篇博客之前请先看Android 热修复框架 Tinker ( 一 )。
1.Tinker热修复简单案例演示
1.打包一个正式版的apk
打包apk是居于上一篇文章的项目(已经集成Tinker)
1.编写avtivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="xmg.com.tinkertest.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:gravity="center"
android:text="chick me !" />
</RelativeLayout>
2.编写MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//补丁所在的路劲
String path=Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk";
//添加补丁
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),path );
Log.d("TAG","path="+path);
}
}
3.修改app/build.gradle:
tinkerBuildFlavorDirectory
注释掉,因为暂时不用到多渠道打包。
ext {
//for some reason, you may want to ignore tinkerBuild, such as instant run debug build?
tinkerEnabled = true
//for normal build
//old apk file to build patch apk
tinkerOldApkPath = "${bakPath}/app-debug-1218-16-32-05.apk"
//proguard mapping file to build patch apk
tinkerApplyMappingPath = "${bakPath}/mapping.txt"
//resource R.txt to build patch apk, must input if there is resource changed
tinkerApplyResourcePath = "${bakPath}/app-debug-1218-16-32-05-R.txt"
//only use for build all flavor, if not, just ignore this field
// tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47"
}
4.开始打包:
点击assembleRelease后,在app/build/apk下生成:下面三个文件
5.将apk安装在模拟器中
安装:app-release-1220-10-49-26.apk
当点击chick me的时候没有任何反应
2.修复代码
1.修改代码,:
修改MainActivity给Check me! 添加一个点击事件冒出Toast:
public class MainActivity extends AppCompatActivity {
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path=Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk";
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),path );
Log.d("TAG","path="+path);
//添加修改的代码
tv_show=(TextView)findViewById(R.id.tv_show);
tv_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"3245",Toast.LENGTH_SHORT).show();
}
});
}
}
3.制作补丁包
1.修改build.gradle的配置文件
启用并修改下面两个属性,并启用它们在该gradle被引用的地方,因为在第一打包的时候已把它们注释了
tinkerOldApkPath = "${bakPath}/app-release-1220-10-49-26.apk" //上面打包生成的.apk
tinkerApplyMappingPath = "${bakPath}/app-release-1220-10-49-26-mapping.txt"//上面生成的mapping.txt
tinkerApplyResourcePath = "${bakPath}/app-release-1220-10-49-26-R.txt" //上面打包生成的R.txt
2.一件制作补丁包:
调用tinkerPatchRelease
, 补丁包与相关日志会保存在/build/outputs/tinkerPatch/
。
4.开始打补丁
1.打补丁:
然后我们将patch_signed_7zip.apk
补丁包 推送到手机的sdcard中
2.重新启动APP( 有时需要重新启动APP才能生效 )
再次点击chick me的时候就会冒出Toast:
现在我们就可以实现在没有重新安装apk的前提下动态发布代码,动态添加代码修复bugs。
5.总结:
1.Tinker可以实现对布局文件的修改,例如:添加id。
2.Tinker可以实现给布局文件中的控件添加点击事件
3.动态发布代码有时候需要重新启动apk才能生效
2.Tinker其它功能
1.检查补丁是否加载
Tinker提供了检查补丁是否已经加载的API
Tinker tinker = Tinker.with(getApplicationContext());
tinker.isTinkerLoaded() // true :patch is loaded]
2.清楚补丁
如果打的补丁有问题,还可以将补丁清除
注意:添加补丁和清除补丁都要重新启动app
Tinker.with(getApplicationContext()).cleanPatch();