background
Tencent’s Matrix must be known by many people, and is also used by many people. However, we have encountered a serious problem, that is, the Gradle plug-in written by Matrix is not compatible with Gradle6.5. However, several of our B-side and C-side projects have made gradle upgrades. It is also taking advantage of the source code analysis efforts to do an upgrade adaptation, and I looked at the Issues of Matrix Github, found that there are a lot of people struggling with this problem, so this period let me, with you to do a adaptation. I don’t make fun of its update speed ha, after all, has been used, Mired in the mire, unable to extricate themselves, can only go forward.
Start to open dry, directly download the source code, configuration upgrade
Source code address:Github.com/Tencent/mat…
After downloading the source code through Git, open the Matrix-Android project with Studio as follows:
It’s clean, isn’t it? Let’s do something about it
Go straight from 4.7 to 6.5, then go to the Matrix-Gradle-plugin project and modify the build-Gradle dependency
Upgrade from 2.1.0 to 4.1.0, and then rebuild the project, and you’ll see a nice picture, so I’m not going to let you look at it, so I’ll just go to the theme and see how I changed it
Variant. GetVariantData ().getScope() for variant. The API for variant com.android.tools.build 4.1 has been changed completely. We’ll definitely need this function, so let’s look at the Inject function.
Take a look at the comparison
GetTransformTaskName is a parameter required by getTransformTaskName. Debug or Release getBuildSrc is the build directory of the app. “DirName” can be productFlavorName + / + Debug or Release. “productFlavors” So easy? No, there are details. How do I verify? The first thing that comes to mind is to upload maven locally, so that you can debug it as soon as possible. Here’s how to do it
Local debugging
Upgrade the plugin version, then upload the local Maven, and add MavenLocal to the peg project I created earlier as follows:
Add this to your App build.gradle
Version Configuration:
If you want to build the samtest project, run the following command:
./gradlew :app:assembleDebug -Dorg.gradle.debug=true --no-daemon
Copy the code
Then create remote in the Matrix-Android project
Start the remote
Now you can debug in your code
What you want is clear. After debugging we found there was no entry
The reason is that I did not find a TransformTask at all. My own project just registered a TransformTask and configured customDexTransformName to Matrix. So that it can be stable into the field. The set (task, new MatrixTraceTransform (config, transformTask getTransform ())), as follows:
After the TransformTask was successfully executed this time, however, when I ran the project, there was a crash and I could not find the class. After repeated checks, I added the following code based on the interpolation I had written before:
MatrixTraceTransform doTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform = MatrixTraceTransform At present, the plug-in upgrade is completed by adding this, and the insertion of piles is normal, as follows:
Through decompilation, we found that the insertion was normal and the APP could run normally.
supplement
As discussed above, when the Matrix plug-in is combined with my self-defined plug-in, outputProvider needs to be added to obtain the target directory and copy the logic, because I changed the execution order of doTransform. Under normal circumstances, a Transform task is executed like this:So why is there no logic for the final step in the Matrix plugin? This is because the Matrix doTransform is executed first, as shown in the figure:Before build Gradle 3.6.0, origTransform was automatically registered as a transform. After build Gradle 3.6.0, origTransform automatically registered as a transform. GetTask is not available, so if your project is later than this version, you need to rely on existing plug-ins, which is the case in our project. In fact, the core reason why there is no Copy Dir logic in the picture is that Matrix exists as a proxy plug-in. He only hooks the existing Transform plug-in in the system or the project, and the system or the project’s customized plug-in will have the corresponding outputProvider copy logic after the completion of the Transform. In this way, the staked class file will be copied to the corresponding directory, and finally packaged by dexBuilder. So for build Gradle of higher versions, if there are no other Transform plug-ins in the project, then we need to upgrade the Matrix plugin to make it register itself and improve the copy dir and JAR logic.
conclusion
In fact, I have learned a lot of knowledge in analyzing the whole matrix source code, which is worth studying everywhere. If you want to see the matrix source code analysis series, please comment on it. So far, four articles have been prepared, including the overall architecture, App Checker, Trace Canary, etc., welcome to leave a message, free of charge. This code change has been uploaded to github.com/ibaozi-cn/m… Welcome to our house.