hook原生打包流程

将实际执行的Transform换成了MatrixTraceTransform

public static void inject(Project project, def variant) {
//获取Matrix trace的gradle配置参数
def configuration = project.matrix.trace
//hook的Task名
String hackTransformTaskName = getTransformTaskName(
configuration.hasProperty('customDexTransformName') ? configuration.customDexTransformName : "",
"",variant.name
)
//同上
String hackTransformTaskNameForWrapper = getTransformTaskName(
configuration.hasProperty('customDexTransformName') ? configuration.customDexTransformName : "",
"Builder",variant.name
)

project.logger.info("prepare inject dex transform :" + hackTransformTaskName +" hackTransformTaskNameForWrapper:"+hackTransformTaskNameForWrapper)

project.getGradle().getTaskGraph().addTaskExecutionGraphListener(new TaskExecutionGraphListener() {
@Override
public void graphPopulated(TaskExecutionGraph taskGraph) {
for (Task task : taskGraph.getAllTasks()) {
//找到需要hook的Task名称
if ((task.name.equalsIgnoreCase(hackTransformTaskName) || task.name.equalsIgnoreCase(hackTransformTaskNameForWrapper))
&& !(((TransformTask) task).getTransform() instanceof MatrixTraceTransform)) {
project.logger.warn("find dex transform. transform class: " + task.transform.getClass() + " . task name: " + task.name)
project.logger.info("variant name: " + variant.name)
Field field = TransformTask.class.getDeclaredField("transform")
field.setAccessible(true)
//反射替换成MatrixTraceTransform,并将原transform传入,最后执行原transform逻辑
field.set(task, new MatrixTraceTransform(project, variant, task.transform))
project.logger.warn("transform class after hook: " + task.transform.getClass())
break
}
}
}
})

posted on 2019-08-02 20:59  激流勇进1  阅读(219)  评论(0编辑  收藏  举报