1.修改tinker官方demo为Android11:
1.1.修改targetSdkVersion为30
1.2.修改compileSdkVersion为30
2.AndroidManifest.xml下增加权限
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
3.替换申请权限的方法
找到demo中MainActivity下的askForRequiredPermissions(),替换为以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// 先判断有没有权限
if (Environment.isExternalStorageManager()) {
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 先判断有没有权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
} else {
askForRequiredPermissions();
}
} else {
}
其余步骤按照官方文档执行即可。
注意:
1.IDE生成的补丁包名称是:app-debug-patch_signed_7zip.apk
而demo中加载的补丁包代码为:
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");
要修改为名称一致
2.加载补丁文件成功后补丁文件会被删除,所以如果每次卸载就要重新按照步骤生成一次补丁,否则会报code:-2的错误
|