At present, there are many hot repair schemes. Today, I studied Tinker of wechat, and the effect is good. It is better to use it with Tinker Server. The use of both is introduced below, so that we can quickly access.
Tinker Access Guide
Install the Tinker Gradle plugin
1 Add the tinker-patch-gradle-plugin dependency in build.gradle
buildscript {
dependencies {
classpath ('com. Tencent. Tinker: tinker - patch - gradle - plugin: 1.7.5')}}Copy the code
2 Then in app gradle file app/build.gradle, we need to add tinker library dependencies and apply Tinker gradle plugin.
/ / the apply tinker plug-in
apply plugin: ‘com.tencent.tinker.patch’
Dependencies {// Create the application class provided('com. Tencent. Tinker: tinker - android - anno: 1.7.5'// Compile (tinker's core library)'com. Tencent. Tinker: tinker - android - lib: 1.7.5')}Copy the code
Configuration tinker task
Configure basic packages, TinkerID, dexMode, etc. See Gradle Configuration: Tinker Task Configuration for details
I made the following changes:
1 Change tinkerID to version number, skip the pit that needs to commit once 🙂
def getTinkerIdValue() {// version as idreturn android.defaultConfig.versionName
}
Copy the code
2 Move the backup file to /tinker/bakApk/ to prevent clean base package files
3. Rename the backup file, for example, base-app-debug-v1.0.1-2016-1125.apk. App-debug-v1.0.1-2016-1125.apk is automatically generated. The subsequent multiple compilations will not overwrite the base package, nor will they produce many files named by seconds like in the official demo…
4 Change the Tinker message to the I am the Patch APK-V version number
5 Change patchVersion to the version number, which is required by Tinker Server
-configField("patchVersion"."1.0.7")
+configField("patchVersion", android.defaultConfig.versionName)
Copy the code
Notice that there are some changes in there, the package name is changed to your package name, etc. I marked it with todo
Generate the Application
If you have an Application class, then you need to customize a DefaultApplicationLike and let Tinker generate the Application for you
Public Class SampleApplicationLike extends DefaultApplicationLike {
Add an annotation to the class, such as:
@DefaultLifeCycle(
application = "tinker.sample.android.app.SampleApplication", //application name to generate
flags = ShareConstants.TINKER_ENABLE_ALL)
Copy the code
Once compiled, a SampleApplication will be generated, which will be used as your Application and written to the manifest file
Ok, tinker is configured here, let’s start patching
Patch package
1 command line
Debug patch:./gradlew tinkerPatchDebug
Release patch:./gradlew tinkerReleaseDebug./gradlew tinkerReleaseDebug
/gradlew, which means the gradlew of the current project. If you write gradlew, you can download gradle, etc., because it is global. For example, AS2.2.2 is 2.14.1 and I have the latest version 3.2.1, you can enter./gradlew -v and gradlew -v. For Windows, you can say gradlew
Note that debug and Release are configured with different base packages, which correspond to them one by one. Additionally, release also needs to configure the mapping file.
2 Double-click the task
Go to Gradle Projects and find the task, double-click it and execute it.
For example, to make a Debug patch, double-click tinkerPatchDebug
The next patch can be selected from the shortcut bar, and then click on the right side to run, as shown below:
Install and uninstall patches
Load the patch
The second parameter is the path where the patch package is stored. The name is arbitrary and does not end with.bak
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), patchPath);
You can also customize interactions such as load success, see SampleResultService, and don’t forget to add it to the list
Clear patch
When there is an exception to the patch or something, we may want to clear the entire patch by calling:
Tinker.with(context).cleanPatch();
Of course, we can also choose to uninstall a certain version of the patch file:
Tinker.with(context).cleanPatchByVersion();
We don’t have to manually remove patches when we upgrade, the framework already does that for us. Note that clearing a patch after the patch is loaded may cause a crash. This is a good time to restart all processes.
Check whether the patch is loaded
boolean isPatched = tinker.isTinkerLoaded();
Connecting to and using tinker Server
Tinker Server provides tinker patch package delivery and monitoring, which is easy to use
Gradle configuration environment
Gradle remote repository depends on JCenter
repositories {
jcenter()
}
Copy the code
Add dependencies to the SDK library.
dependencies {
compile("Com. Tencent. Tinker: tinker - server - android: 0.3.0")}Copy the code
AppKey and AppVersion from TinkerPatch and write them to buildConfig:
Such as:
buildConfigField "String"."APP_KEY"."\"f938475486f91936\""
buildConfigField "String"."APP_VERSION"."\" 3.0.0 \ ""
Copy the code
Platform link: tinkerPatch.com
The AppKey can be obtained after the new APP is added. As for the AppVersion, it is the version of the patch. I have the version number here, you can refer to this issue: About AppVersion
4 Configure read and write permissions on the network and SD card
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Copy the code
Code initialization
TinkerServerManager.installTinkerServer(getApplication(), Tinker.with(getApplication()), 3);
The following 3 indicates that the server is requested every 3 hours to check for updates
Requesting patch update
1 Actively request updates
TinkerServerManager.checkTinkerUpdate(true);
2 Obtain the new parameters
TinkerServerManager.getDynamicConfig(new ConfigRequestCallback() {…
Here is a tinker Server screenshot of the demo:
reference
For more usage and problems, please refer to the official documents:
Tinker — wechat Android hot patch solution
Tinker Access Guide
Tinker API overview
Tinker custom extension
Tinker FAQs
Tinker Platform Usage documents
The code is the official Tinker example, I made some changes, can be viewed here: github.com/jp1017/tink…
Finally, thank you very much for reading. If you have any questions, please feel free to comment. Thank you!
Amazing Android development site: Androidcat.com/
Android open source library collection: github.com/XXApple/And…
Sharing is a virtue and a way of life!!
You may say I am a dreamer, but I am not the only one.
Happy share, the more happy ^_^
Welcome to exchange, reprint please indicate the source, thank you!