# tinkerSample **Repository Path**: xjdd/tinkerSample ## Basic Information - **Project Name**: tinkerSample - **Description**: tingker Demo - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-10-16 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # tinkerSample #### 添加gradle依赖 在项目build.gradle中添加一下代码

dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        //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
然后在app/build.gradle中添加从Tinker模板TinkerDemo .app/build.gradle中拷贝代码到里面去 当配置完成,点击Sync Now,bug就出来了,这是大部分人会遇到的问题! tinkerId is not set!!! ![输入图片说明](https://images.gitee.com/uploads/images/2019/1016/143609_10daf5f5_71723.png "9GVUCR1X}@`RKUP@@6{(NCM.png") 出现这个直接可以修改gradle设置一个默认参数就可以了。 ![输入图片说明](https://images.gitee.com/uploads/images/2019/1016/143853_6fc73408_71723.png "X67M[%66E1`{H]3OZ)HK~7E.png") #### 编写生成Application的类,因为SimpleTinkerInApplicationLike会在编译期生成SimpleTinkerInApplication

@DefaultLifeCycle(application = ".SimpleTinkerInApplication",
        flags = ShareConstants.TINKER_ENABLE_ALL,
        loadVerifyFlag = false)
public class SimpleTinkerInApplicationLike extends ApplicationLike {

    public SimpleTinkerInApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        TinkerInstaller.install(this);
    }
}
别忘了添加到清单文件里去 ``` ``` #### 编写MainActivity,很简单,就一行代码 配置补丁包读取的位置

public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.tv);
        mButton = (Button) findViewById(R.id.btn);
//        mTextView.setText("这不是bug");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),
                        getCacheDir + "/patch_signed_7zip.apk");//直接存放到包的缓存目录中,以达到更新的目的
            }
        });
    }
}
#### 安装“有bug”的应用 这里不能直接点运行,要选择assembleDebug。这时会在app/build/bakApk下生成编译好的安装包(编译时会报错,需要把debug时的签名文件放到相应路径里面) ![输入图片说明](https://images.gitee.com/uploads/images/2019/1016/144042_a7b0dabc_71723.png "M$L8[{7MQ70@{ILB2_7OUSX.png") 一般我们debug不需要签名文件,所以只需要把与debug签名相关的代码注释即可),然后将该安装包安装到手机上。可以使用adb命令将安装包安装到手机。如果应 用已经在手机上已经安装了,那么就要使用这个命令。(如果提示不是内部命令则需要将adb.exe所在的文件地址添加到电脑的系统变量Path里面去) #### 生成补丁包及修复“bug 然后需求配置构建补丁的参数 ![输入图片说明](https://images.gitee.com/uploads/images/2019/1016/144143_5ee19a72_71723.png "UF3HQ7PK4K8`)~5`~GWXL}C.png")

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-1016-13-39-55.apk"
    //proguard mapping file to build patch apk 老的生成的mapping映射文件
    tinkerApplyMappingPath = "${bakPath}/app-debug-1018-17-32-47-mapping.txt"
    //resource R.txt to build patch apk, must input if there is resource changed 配置资源文件R
    tinkerApplyResourcePath = "${bakPath}/app-debug-0424-15-02-56-R.txt"

    //only use for build all flavor, if not, just ignore this field 构建其他配置的一般可以不用
    tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47"
}
![输入图片说明](https://images.gitee.com/uploads/images/2019/1016/144254_d0aa93f2_71723.png "Q}EDHA%XNY_2KE646E(INOH.png") 配置完成之后,运行gradle task 任务中找到tinkerPatchDebug(tinkerPatchRelease)任务完成后,会在/build/outputs/tinkerPatch/生成补丁包, 然后我们需要将patch_signed_7zip.apk推送到sd卡的中 路径的位置就是前面MainActivity中设置的路径。 存放位置成功之后,触发onReceiveUpgradePatch了 重启进程就可以看到我们的bug被修复了。