用于class.dex CRC计算的Gradle任务 [英] Gradle task for classes.dex CRC calculation

查看:150
本文介绍了用于class.dex CRC计算的Gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Gradle任务,该任务计算 classes.dex CRC,然后将结果值写入资源字符串.该值将在运行时检查,以确定APK是否已被篡改.问题是,从Gradle插件1.4.+开始,无法再访问dex任务.相反,我们应该使用Transform API.我在Android环境中很少找到有关Gradle任务的文档,所以我会问几个问题:

I'd like to create a Gradle task that computes the classes.dex CRC, then writes the resulting value into a resource string. This value will be checked at runtime to determine whether the APK has been tampered or not. The problem is that beginning with Gradle plugin 1.4.+ it is not possible to access the dex task anymore. Instead, we should use Transform API. I found very little documentation about Gradle tasks in the Android environment, so I would ask a few questions:

  1. 处理 classes.dex 文件的Gradle任务是什么?
  2. Transform应该如何处理此任务?
  1. What's the Gradle task that deals with the classes.dex file?
  2. How should the Transform work with this task?

我已经看到很多关于此参数的线程,但是这些线程都没有有效的解决方案.预先感谢!

I've seen lots of threads about this argument, but none of these have a working solution. Thanks in advance!

推荐答案

根据Xavier Ducrohet:

According to Xavier Ducrohet:

您必须构建两次.classes.dex包含从res编译生成的R.class.因此,在您计算CRC32时,将其放入为时已晚.

You have to build twice. classes.dex contains R.class which is generate from the res compilation. So by the time you're computing the CRC32 it's too late to put it in.

通常,您实际上不应该在任务执行期间修改模型.实际上,Gradle将引入任务并行化,这实际上将需要在任务运行时不接触模型.因此,我们将通过使其无法执行来(尝试)解决此问题.我刚刚提交了> https://code.google.com/p/android/issues/detail?id = 82574

In general you really shouldn't be modifying the model during task execution. In fact, Gradle will introduce task parallelization that really will require to not touch the model when the task are running. So we're going to (try to) fix this by making it impossible to do this. I just filed > https://code.google.com/p/android/issues/detail?id=82574

因此,我将执行以下操作:-在项目的评估阶段,读取包含CRC的文件并将其设置为资源.像这样(使用番石榴):

So I would do the following: - in the evaluation phase of your project, read a file that contains the CRC and set it as a resources. Something like this (using Guava):

android.applicationVariants.all {变体->variant.resValue"string","CRC",com.google.common.io.Files.toString(file("$ buildDir/intermediates/checksum/$ variant.dirName/classes.crc32"),Charsets.UTF_8)}

  • 设置一个任务来创建包含CRC32的文件.

android.applicationVariants.all {变体->variant,outputs.each {//在此处创建任务.它依赖于dex任务,并使output.packageApplication任务依赖于它.}}

注意:这还不够.您知道需要做的是确保如果新计算的CRC32与当前文件不同,则构建会中断,从而迫使您进行第二次构建.这样,您有两种情况:-CRC32文件丢失或内容不正确.您计算出新的CRC32,将其放入文件中,并导致构建强制失败,并使用此新值再次构建.-CRC32已经有效,这意味着资源包含正确的值,任务不再执行任何操作,构建继续进行.

Note: this is not enough. What you know need to do is ensure that if the newly computed CRC32 is different than the current file, the build breaks, forcing you to build a 2nd time. This way you have the two cases: - CRC32 file is missing or the content is incorrect. You compute the new CRC32, put it in the file and fail the build forcing to build again with this new value. - CRC32 is already valid, which mean the resource contains the right value, the task does nothing more and the build continues.

https://groups.google.com/d/msg/adt-dev/W2aYLBSeGUE/fzOqyH8YibQJ

这篇关于用于class.dex CRC计算的Gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆